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")
40 func serviceHandler(response http.ResponseWriter, request *http.Request) {
41 if request.Method != "POST" {
42 io.WriteString(response, "Error: Not a POST request")
46 switch request.FormValue("action") {
48 files, err := paths.List(request.FormValue("path"))
50 errorResponse(response, err.String())
52 okResponse(response, files)
55 err := paths.Remove(request.FormValue("path"))
57 errorResponse(response, err.String())
59 data := map[string]int{
62 okResponse(response, data)
65 source := request.FormValue("source")
66 target := request.FormValue("target")
67 err := paths.Move(source, target)
69 errorResponse(response, err.String())
71 data := map[string]interface{}{
75 okResponse(response, data)
78 path := request.FormValue("path")
79 err := paths.MakeDir(path)
81 errorResponse(response, err.String())
83 data := map[string]interface{}{
87 okResponse(response, data)
90 newPath, err := tv_rename.RenameEpisode(request.FormValue("path"))
92 errorResponse(response, err.String())
94 data := map[string]interface{}{
98 okResponse(response, data)
101 fmt.Printf("Invalid action: '%s'\n", request.FormValue("action"))
102 errorResponse(response, "Unhandled action")
106 func proxyHandler(response http.ResponseWriter, request *http.Request) {
107 rawURL := request.FormValue("url")
112 var validURL bool = false
113 for i := range gConfig.ProxyURLs {
114 allowedURL := gConfig.ProxyURLs[i]
115 validURL = validURL || strings.HasPrefix(rawURL, allowedURL)
119 errorResponse(response, "URL is not in proxy whitelist")
123 url_, err := url.Parse(rawURL)
125 errorResponse(response, err.String())
128 err = performProxy(url_, response, request)
130 errorResponse(response, err.String())
134 func performProxy(url_ *url.URL, response http.ResponseWriter, origRequest *http.Request) os.Error {
135 conn, err := net.Dial("tcp", url_.Host+":http")
139 client := http.NewClientConn(conn, nil)
140 var request http.Request
142 request.Method = "GET"
143 request.Header.Set("User-Agent", origRequest.UserAgent())
144 err = client.Write(&request)
148 var proxyResponse *http.Response
149 proxyResponse, err = client.Read(&request)
150 if err != nil && err != http.ErrPersistEOF {
153 _, err = io.Copy(response, proxyResponse.Body)
157 func downloadHandler(response http.ResponseWriter, request *http.Request) {
158 valid, fullPath := paths.IsValid(request.FormValue("path"))
160 info, _ := os.Lstat(fullPath) // Error is already checked by |valid|.
161 if info.IsDirectory() {
162 http.Error(response, "Path is a directory", http.StatusBadRequest)
164 http.ServeFile(response, request, fullPath)
167 http.NotFound(response, request)
171 func errorResponse(response http.ResponseWriter, message string) {
172 message = strings.Replace(message, gConfig.JailRoot, "/", -1)
173 data := map[string]interface{}{
177 json_data, err := json.Marshal(data)
179 response.Header().Set("Content-Type", "text/json")
181 io.WriteString(response, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}")
183 response.Write(json_data)
187 func okResponse(response http.ResponseWriter, data interface{}) {
188 response.Header().Set("Content-Type", "text/json")
189 json_data, err := json.Marshal(data)
191 errorResponse(response, "Internal encoding error")
193 response.Write(json_data)
197 func RunBackEnd(config *config.Configuration) {
198 mux := http.NewServeMux()
199 mux.HandleFunc("/", indexHandler)
200 mux.Handle("/fe/", http.StripPrefix("/fe/", http.FileServer(http.Dir(kFrontEndFiles))))
201 mux.HandleFunc("/service", serviceHandler)
202 mux.HandleFunc("/download", downloadHandler)
203 mux.HandleFunc("/proxy", proxyHandler)
207 error := http.ListenAndServe(fmt.Sprintf(":%d", config.Port), mux)
208 fmt.Printf("error %v", error)