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.
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"))
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]interface{}{
74 okResponse(response, data)
77 path := request.FormValue("path")
78 err := paths.MakeDir(path)
80 errorResponse(response, err.String())
82 data := map[string]interface{}{
86 okResponse(response, data)
89 newPath, err := tv_rename.RenameEpisode(request.FormValue("path"))
91 errorResponse(response, err.String())
93 data := map[string]interface{}{
97 okResponse(response, data)
100 fmt.Printf("Invalid action: '%s'\n", request.FormValue("action"))
101 errorResponse(response, "Unhandled action")
105 func proxyHandler(response http.ResponseWriter, request *http.Request) {
106 rawURL := request.FormValue("url")
111 var validURL bool = false
112 for i := range gConfig.ProxyURLs {
113 allowedURL := gConfig.ProxyURLs[i]
114 validURL = validURL || strings.HasPrefix(rawURL, allowedURL)
118 errorResponse(response, "URL is not in proxy whitelist")
122 url, err := http.ParseURL(rawURL)
124 errorResponse(response, err.String())
127 err = performProxy(url, response, request)
129 errorResponse(response, err.String())
133 func performProxy(url *http.URL, response http.ResponseWriter, origRequest *http.Request) os.Error {
134 conn, err := net.Dial("tcp", url.Host+":http")
138 client := http.NewClientConn(conn, nil)
139 var request http.Request
141 request.Method = "GET"
142 request.UserAgent = origRequest.UserAgent
143 err = client.Write(&request)
147 var proxyResponse *http.Response
148 proxyResponse, err = client.Read(&request)
149 if err != nil && err != http.ErrPersistEOF {
152 _, err = io.Copy(response, proxyResponse.Body)
156 func downloadHandler(response http.ResponseWriter, request *http.Request) {
157 valid, fullPath := paths.IsValid(request.FormValue("path"))
159 info, _ := os.Lstat(fullPath) // Error is already checked by |valid|.
160 if info.IsDirectory() {
161 http.Error(response, "Path is a directory", http.StatusBadRequest)
163 http.ServeFile(response, request, fullPath)
166 http.NotFound(response, request)
170 func errorResponse(response http.ResponseWriter, message string) {
171 message = strings.Replace(message, gConfig.JailRoot, "/", -1)
172 data := map[string]interface{}{
176 json_data, err := json.Marshal(data)
178 response.Header().Set("Content-Type", "text/json")
180 io.WriteString(response, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}")
182 response.Write(json_data)
186 func okResponse(response http.ResponseWriter, data interface{}) {
187 response.Header().Set("Content-Type", "text/json")
188 json_data, err := json.Marshal(data)
190 errorResponse(response, "Internal encoding error")
192 response.Write(json_data)
196 func RunBackEnd(config *config.Configuration) {
197 mux := http.NewServeMux()
198 mux.HandleFunc("/", indexHandler)
199 mux.Handle("/fe/", http.FileServer(kFrontEndFiles, "/fe/"))
200 mux.HandleFunc("/service", serviceHandler)
201 mux.HandleFunc("/download", downloadHandler)
202 mux.HandleFunc("/proxy", proxyHandler)
206 error := http.ListenAndServe(fmt.Sprintf(":%d", config.Port), mux)
207 fmt.Printf("error %v", error)