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 request, err := http.NewRequest("GET", url_.String(), nil)
145 request.Header.Set("User-Agent", origRequest.UserAgent())
146 err = client.Write(request)
150 var proxyResponse *http.Response
151 proxyResponse, err = client.Read(request)
152 if err != nil && err != http.ErrPersistEOF {
155 _, err = io.Copy(response, proxyResponse.Body)
159 func downloadHandler(response http.ResponseWriter, request *http.Request) {
160 valid, fullPath := paths.IsValid(request.FormValue("path"))
162 info, _ := os.Lstat(fullPath) // Error is already checked by |valid|.
163 if info.IsDirectory() {
164 http.Error(response, "Path is a directory", http.StatusBadRequest)
166 http.ServeFile(response, request, fullPath)
169 http.NotFound(response, request)
173 func errorResponse(response http.ResponseWriter, message string) {
174 message = strings.Replace(message, gConfig.JailRoot, "/", -1)
175 data := map[string]interface{}{
179 json_data, err := json.Marshal(data)
181 response.Header().Set("Content-Type", "text/json")
183 io.WriteString(response, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}")
185 response.Write(json_data)
189 func okResponse(response http.ResponseWriter, data interface{}) {
190 response.Header().Set("Content-Type", "text/json")
191 json_data, err := json.Marshal(data)
193 errorResponse(response, "Internal encoding error")
195 response.Write(json_data)
199 func RunBackEnd(config *config.Configuration) {
200 mux := http.NewServeMux()
201 mux.HandleFunc("/", indexHandler)
202 mux.Handle("/fe/", http.StripPrefix("/fe/", http.FileServer(http.Dir(kFrontEndFiles))))
203 mux.HandleFunc("/service", serviceHandler)
204 mux.HandleFunc("/download", downloadHandler)
205 mux.HandleFunc("/proxy", proxyHandler)
209 error := http.ListenAndServe(fmt.Sprintf(":%d", config.Port), mux)
210 fmt.Printf("error %v", error)