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")
39 response.Header().Set("Content-Type", "text/html")
43 func serviceHandler(response http.ResponseWriter, request *http.Request) {
44 if request.Method != "POST" {
45 io.WriteString(response, "Error: Not a POST request")
49 switch request.FormValue("action") {
51 files, err := paths.List(request.FormValue("path"))
53 errorResponse(response, err.String())
55 okResponse(response, files)
58 err := paths.Remove(request.FormValue("path"))
60 errorResponse(response, err.String())
62 data := map[string]int{
65 okResponse(response, data)
68 source := request.FormValue("source")
69 target := request.FormValue("target")
70 err := paths.Move(source, target)
72 errorResponse(response, err.String())
74 data := map[string]interface{}{
78 okResponse(response, data)
81 path := request.FormValue("path")
82 err := paths.MakeDir(path)
84 errorResponse(response, err.String())
86 data := map[string]interface{}{
90 okResponse(response, data)
93 newPath, err := tv_rename.RenameEpisode(request.FormValue("path"))
95 errorResponse(response, err.String())
97 data := map[string]interface{}{
101 okResponse(response, data)
104 fmt.Printf("Invalid action: '%s'\n", request.FormValue("action"))
105 errorResponse(response, "Unhandled action")
109 func proxyHandler(response http.ResponseWriter, request *http.Request) {
110 rawURL := request.FormValue("url")
115 var validURL bool = false
116 for i := range gConfig.ProxyURLs {
117 allowedURL := gConfig.ProxyURLs[i]
118 validURL = validURL || strings.HasPrefix(rawURL, allowedURL)
122 errorResponse(response, "URL is not in proxy whitelist")
126 url_, err := url.Parse(rawURL)
128 errorResponse(response, err.String())
131 err = performProxy(url_, response, request)
133 errorResponse(response, err.String())
137 func performProxy(url_ *url.URL, response http.ResponseWriter, origRequest *http.Request) os.Error {
138 conn, err := net.Dial("tcp", url_.Host+":http")
142 client := http.NewClientConn(conn, nil)
143 request, err := http.NewRequest("GET", url_.String(), nil)
147 request.Header.Set("User-Agent", origRequest.UserAgent())
148 err = client.Write(request)
152 var proxyResponse *http.Response
153 proxyResponse, err = client.Read(request)
154 if err != nil && err != http.ErrPersistEOF {
157 _, err = io.Copy(response, proxyResponse.Body)
161 func downloadHandler(response http.ResponseWriter, request *http.Request) {
162 valid, fullPath := paths.IsValid(request.FormValue("path"))
164 info, _ := os.Lstat(fullPath) // Error is already checked by |valid|.
165 if info.IsDirectory() {
166 http.Error(response, "Path is a directory", http.StatusBadRequest)
168 http.ServeFile(response, request, fullPath)
171 http.NotFound(response, request)
175 func errorResponse(response http.ResponseWriter, message string) {
176 message = strings.Replace(message, gConfig.JailRoot, "/", -1)
177 data := map[string]interface{}{
181 json_data, err := json.Marshal(data)
183 response.Header().Set("Content-Type", "text/json")
185 io.WriteString(response, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}")
187 response.Write(json_data)
191 func okResponse(response http.ResponseWriter, data interface{}) {
192 response.Header().Set("Content-Type", "text/json")
193 json_data, err := json.Marshal(data)
195 errorResponse(response, "Internal encoding error")
197 response.Write(json_data)
201 func RunBackEnd(config *config.Configuration) {
202 mux := http.NewServeMux()
203 mux.HandleFunc("/", indexHandler)
204 mux.Handle("/fe/", http.StripPrefix("/fe/", http.FileServer(http.Dir(kFrontEndFiles))))
205 mux.HandleFunc("/service", serviceHandler)
206 mux.HandleFunc("/download", downloadHandler)
207 mux.HandleFunc("/proxy", proxyHandler)
211 error := http.ListenAndServe(fmt.Sprintf(":%d", config.Port), mux)
212 fmt.Printf("error %v", error)