2 // Armadillo File Manager
3 // Copyright (c) 2010, Robert Sesek <http://www.bluestatic.org>
5 // This program is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free Software
7 // Foundation, either version 3 of the License, or any later version.
26 var dir, file = path.Split(path.Clean(os.Getenv("_")))
27 var kFrontEndFiles string = path.Join(dir, "fe")
28 var gConfig *config.Configuration = nil
30 func indexHandler(response http.ResponseWriter, request *http.Request) {
31 fd, err := os.Open(path.Join(kFrontEndFiles, "index.html"), os.O_RDONLY, 0)
33 fmt.Print("Error opening file ", err.String(), "\n")
39 func serviceHandler(response http.ResponseWriter, request *http.Request) {
40 if request.Method != "POST" {
41 io.WriteString(response, "Error: Not a POST request")
45 switch request.FormValue("action") {
47 files, err := paths.List(request.FormValue("path"))
49 errorResponse(response, err.String())
51 okResponse(response, files)
54 err := paths.Remove(request.FormValue("path"))
56 errorResponse(response, err.String())
58 data := map[string] int {
61 okResponse(response, data)
64 source := request.FormValue("source")
65 target := request.FormValue("target")
66 err := paths.Move(source, target)
68 errorResponse(response, err.String())
70 data := map[string] string {
73 okResponse(response, data)
76 newPath, err := tv_rename.RenameEpisode(request.FormValue("path"))
78 errorResponse(response, err.String())
80 data := map[string] interface{} {
84 okResponse(response, data)
87 fmt.Printf("Invalid action: '%s'\n", request.FormValue("action"))
88 errorResponse(response, "Unhandled action")
92 func proxyHandler(response http.ResponseWriter, request *http.Request) {
93 rawURL := request.FormValue("url")
98 var validURL bool = false
99 for i := range gConfig.ProxyURLs {
100 allowedURL := gConfig.ProxyURLs[i]
101 validURL = validURL || strings.HasPrefix(rawURL, allowedURL)
105 errorResponse(response, "URL is not in proxy whitelist")
109 url, err := http.ParseURL(rawURL)
111 errorResponse(response, err.String())
114 err = performProxy(url, response, request)
116 errorResponse(response, err.String())
120 func performProxy(url *http.URL, response http.ResponseWriter, origRequest *http.Request) os.Error {
121 conn, err := net.Dial("tcp", "", url.Host + ":http")
125 client := http.NewClientConn(conn, nil)
126 var request http.Request
128 request.Method = "GET"
129 request.UserAgent = origRequest.UserAgent
130 err = client.Write(&request)
134 var proxyResponse *http.Response
135 proxyResponse, err = client.Read()
136 if err != nil && err != http.ErrPersistEOF {
139 _, err = io.Copy(response, proxyResponse.Body)
143 func errorResponse(response http.ResponseWriter, message string) {
144 message = strings.Replace(message, gConfig.JailRoot, "/", -1)
145 data := map[string] string {
149 json_data, err := json.Marshal(data)
151 response.SetHeader("Content-Type", "text/json")
153 io.WriteString(response, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}")
155 response.Write(json_data)
159 func okResponse(response http.ResponseWriter, data interface{}) {
160 response.SetHeader("Content-Type", "text/json")
161 json_data, err := json.Marshal(data)
163 errorResponse(response, "Internal encoding error")
165 response.Write(json_data)
169 func RunBackEnd(config *config.Configuration) {
170 mux := http.NewServeMux()
171 mux.HandleFunc("/", indexHandler)
172 mux.Handle("/fe/", http.FileServer(kFrontEndFiles, "/fe/"))
173 mux.HandleFunc("/service", serviceHandler)
174 mux.HandleFunc("/proxy", proxyHandler)
178 error := http.ListenAndServe(fmt.Sprintf(":%d", config.Port), mux)
179 fmt.Printf("error %v", error)