2 // Armadillo File Manager
3 // Copyright (c) 2010-2011, 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.
27 var dir, file = path.Split(path.Clean(os.Getenv("_")))
28 var kFrontEndFiles string = path.Join(dir, "fe")
29 var gConfig *config.Configuration = nil
31 func indexHandler(response http.ResponseWriter, request *http.Request) {
32 fd, err := os.Open(path.Join(kFrontEndFiles, "index.html"))
34 fmt.Print("Error opening file ", err.String(), "\n")
37 response.Header().Set("Content-Type", "text/html")
41 func serviceHandler(response http.ResponseWriter, request *http.Request) {
42 if request.Method != "POST" {
43 io.WriteString(response, "Error: Not a POST request")
47 switch request.FormValue("action") {
49 files, err := paths.List(request.FormValue("path"))
51 errorResponse(response, err.String())
53 okResponse(response, files)
56 err := paths.Remove(request.FormValue("path"))
58 errorResponse(response, err.String())
60 data := map[string]int{
63 okResponse(response, data)
66 source := request.FormValue("source")
67 target := request.FormValue("target")
68 err := paths.Move(source, target)
70 errorResponse(response, err.String())
72 data := map[string]interface{}{
76 okResponse(response, data)
79 path := request.FormValue("path")
80 err := paths.MakeDir(path)
82 errorResponse(response, err.String())
84 data := map[string]interface{}{
88 okResponse(response, data)
91 newPath, err := tv_rename.RenameEpisode(request.FormValue("path"))
93 errorResponse(response, err.String())
95 data := map[string]interface{}{
99 okResponse(response, data)
102 fmt.Printf("Invalid action: '%s'\n", request.FormValue("action"))
103 errorResponse(response, "Unhandled action")
107 func proxyHandler(response http.ResponseWriter, request *http.Request) {
108 rawURL := request.FormValue("url")
113 var validURL bool = false
114 for i := range gConfig.ProxyURLs {
115 allowedURL := gConfig.ProxyURLs[i]
116 validURL = validURL || strings.HasPrefix(rawURL, allowedURL)
120 errorResponse(response, "URL is not in proxy whitelist")
124 url_, err := url.Parse(rawURL)
126 errorResponse(response, err.String())
129 err = performProxy(url_, response, request)
131 errorResponse(response, err.String())
135 func performProxy(url_ *url.URL, response http.ResponseWriter, origRequest *http.Request) os.Error {
136 conn, err := net.Dial("tcp", url_.Host+":http")
140 client := http.NewClientConn(conn, nil)
141 var request http.Request
143 request.Method = "GET"
144 request.Header.Set("User-Agent", origRequest.UserAgent())
145 err = client.Write(&request)
149 var proxyResponse *http.Response
150 proxyResponse, err = client.Read(&request)
151 if err != nil && err != http.ErrPersistEOF {
154 _, err = io.Copy(response, proxyResponse.Body)
158 func downloadHandler(response http.ResponseWriter, request *http.Request) {
159 valid, fullPath := paths.IsValid(request.FormValue("path"))
161 info, _ := os.Lstat(fullPath) // Error is already checked by |valid|.
162 if info.IsDirectory() {
163 http.Error(response, "Path is a directory", http.StatusBadRequest)
165 http.ServeFile(response, request, fullPath)
168 http.NotFound(response, request)
172 func errorResponse(response http.ResponseWriter, message string) {
173 message = strings.Replace(message, gConfig.JailRoot, "/", -1)
174 data := map[string]interface{}{
178 json_data, err := json.Marshal(data)
180 response.Header().Set("Content-Type", "text/json")
182 io.WriteString(response, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}")
184 response.Write(json_data)
188 func okResponse(response http.ResponseWriter, data interface{}) {
189 response.Header().Set("Content-Type", "text/json")
190 json_data, err := json.Marshal(data)
192 errorResponse(response, "Internal encoding error")
194 response.Write(json_data)
198 func RunBackEnd(config *config.Configuration) {
199 mux := http.NewServeMux()
200 mux.HandleFunc("/", indexHandler)
201 mux.Handle("/fe/", http.StripPrefix("/fe/", http.FileServer(http.Dir(kFrontEndFiles))))
202 mux.HandleFunc("/service", serviceHandler)
203 mux.HandleFunc("/download", downloadHandler)
204 mux.HandleFunc("/proxy", proxyHandler)
208 error := http.ListenAndServe(fmt.Sprintf(":%d", config.Port), mux)
209 fmt.Printf("error %v", error)