2 // Armadillo File Manager
3 // Copyright (c) 2010, 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"), os.O_RDONLY, 0)
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 newPath, err := tv_rename.RenameEpisode(request.FormValue("path"))
79 errorResponse(response, err.String())
81 data := map[string] interface{} {
85 okResponse(response, data)
88 fmt.Printf("Invalid action: '%s'\n", request.FormValue("action"))
89 errorResponse(response, "Unhandled action")
93 func proxyHandler(response http.ResponseWriter, request *http.Request) {
94 rawURL := request.FormValue("url")
99 var validURL bool = false
100 for i := range gConfig.ProxyURLs {
101 allowedURL := gConfig.ProxyURLs[i]
102 validURL = validURL || strings.HasPrefix(rawURL, allowedURL)
106 errorResponse(response, "URL is not in proxy whitelist")
110 url, err := http.ParseURL(rawURL)
112 errorResponse(response, err.String())
115 err = performProxy(url, response, request)
117 errorResponse(response, err.String())
121 func performProxy(url *http.URL, response http.ResponseWriter, origRequest *http.Request) os.Error {
122 conn, err := net.Dial("tcp", "", url.Host + ":http")
126 client := http.NewClientConn(conn, nil)
127 var request http.Request
129 request.Method = "GET"
130 request.UserAgent = origRequest.UserAgent
131 err = client.Write(&request)
135 var proxyResponse *http.Response
136 proxyResponse, err = client.Read()
137 if err != nil && err != http.ErrPersistEOF {
140 _, err = io.Copy(response, proxyResponse.Body)
144 func errorResponse(response http.ResponseWriter, message string) {
145 message = strings.Replace(message, gConfig.JailRoot, "/", -1)
146 data := map[string] interface{} {
150 json_data, err := json.Marshal(data)
152 response.SetHeader("Content-Type", "text/json")
154 io.WriteString(response, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}")
156 response.Write(json_data)
160 func okResponse(response http.ResponseWriter, data interface{}) {
161 response.SetHeader("Content-Type", "text/json")
162 json_data, err := json.Marshal(data)
164 errorResponse(response, "Internal encoding error")
166 response.Write(json_data)
170 func RunBackEnd(config *config.Configuration) {
171 mux := http.NewServeMux()
172 mux.HandleFunc("/", indexHandler)
173 mux.Handle("/fe/", http.FileServer(kFrontEndFiles, "/fe/"))
174 mux.HandleFunc("/service", serviceHandler)
175 mux.HandleFunc("/proxy", proxyHandler)
179 error := http.ListenAndServe(fmt.Sprintf(":%d", config.Port), mux)
180 fmt.Printf("error %v", error)