Update package names and import paths
[armadillo.git] / server / server.go
1 //
2 // Armadillo File Manager
3 // Copyright (c) 2010-2011, Robert Sesek <http://www.bluestatic.org>
4 //
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.
8 //
9
10 package server
11
12 import (
13 "fmt"
14 "http"
15 "io"
16 "json"
17 "net"
18 "os"
19 "path"
20 "strings"
21 "url"
22 "../config"
23 )
24
25 var dir, file = path.Split(path.Clean(os.Getenv("_")))
26 var kFrontEndFiles string = path.Join(dir, "fe")
27 var gConfig *config.Configuration = nil
28
29 func indexHandler(response http.ResponseWriter, request *http.Request) {
30 fd, err := os.Open(path.Join(kFrontEndFiles, "index.html"))
31 if err != nil {
32 fmt.Print("Error opening file ", err.String(), "\n")
33 return
34 }
35 defer fd.Close()
36
37 response.Header().Set("Content-Type", "text/html")
38 io.Copy(response, fd)
39 }
40
41 func serviceHandler(response http.ResponseWriter, request *http.Request) {
42 if request.Method != "POST" {
43 io.WriteString(response, "Error: Not a POST request")
44 return
45 }
46
47 switch request.FormValue("action") {
48 case "list":
49 files, err := paths.List(request.FormValue("path"))
50 if err != nil {
51 errorResponse(response, err.String())
52 } else {
53 okResponse(response, files)
54 }
55 case "remove":
56 err := paths.Remove(request.FormValue("path"))
57 if err != nil {
58 errorResponse(response, err.String())
59 } else {
60 data := map[string]int{
61 "error": 0,
62 }
63 okResponse(response, data)
64 }
65 case "move":
66 source := request.FormValue("source")
67 target := request.FormValue("target")
68 err := paths.Move(source, target)
69 if err != nil {
70 errorResponse(response, err.String())
71 } else {
72 data := map[string]interface{}{
73 "path": target,
74 "error": 0,
75 }
76 okResponse(response, data)
77 }
78 case "mkdir":
79 path := request.FormValue("path")
80 err := paths.MakeDir(path)
81 if err != nil {
82 errorResponse(response, err.String())
83 } else {
84 data := map[string]interface{}{
85 "path": path,
86 "error": 0,
87 }
88 okResponse(response, data)
89 }
90 case "tv_rename":
91 newPath, err := tv_rename.RenameEpisode(request.FormValue("path"))
92 if err != nil {
93 errorResponse(response, err.String())
94 } else {
95 data := map[string]interface{}{
96 "path": *newPath,
97 "error": 0,
98 }
99 okResponse(response, data)
100 }
101 default:
102 fmt.Printf("Invalid action: '%s'\n", request.FormValue("action"))
103 errorResponse(response, "Unhandled action")
104 }
105 }
106
107 func proxyHandler(response http.ResponseWriter, request *http.Request) {
108 rawURL := request.FormValue("url")
109 if len(rawURL) < 1 {
110 return
111 }
112
113 var validURL bool = false
114 for i := range gConfig.ProxyURLs {
115 allowedURL := gConfig.ProxyURLs[i]
116 validURL = validURL || strings.HasPrefix(rawURL, allowedURL)
117 }
118
119 if !validURL {
120 errorResponse(response, "URL is not in proxy whitelist")
121 return
122 }
123
124 url_, err := url.Parse(rawURL)
125 if err != nil {
126 errorResponse(response, err.String())
127 return
128 }
129 err = performProxy(url_, response, request)
130 if err != nil {
131 errorResponse(response, err.String())
132 }
133 }
134
135 func performProxy(url_ *url.URL, response http.ResponseWriter, origRequest *http.Request) os.Error {
136 conn, err := net.Dial("tcp", url_.Host+":http")
137 if err != nil {
138 return err
139 }
140 client := http.NewClientConn(conn, nil)
141 request, err := http.NewRequest("GET", url_.String(), nil)
142 if err != nil {
143 return err
144 }
145 request.Header.Set("User-Agent", origRequest.UserAgent())
146 err = client.Write(request)
147 if err != nil {
148 return err
149 }
150 var proxyResponse *http.Response
151 proxyResponse, err = client.Read(request)
152 if err != nil && err != http.ErrPersistEOF {
153 return err
154 }
155 _, err = io.Copy(response, proxyResponse.Body)
156 return err
157 }
158
159 func downloadHandler(response http.ResponseWriter, request *http.Request) {
160 valid, fullPath := paths.IsValid(request.FormValue("path"))
161 if valid {
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)
165 } else {
166 http.ServeFile(response, request, fullPath)
167 }
168 } else {
169 http.NotFound(response, request)
170 }
171 }
172
173 func errorResponse(response http.ResponseWriter, message string) {
174 message = strings.Replace(message, gConfig.JailRoot, "/", -1)
175 data := map[string]interface{}{
176 "error": -1,
177 "message": message,
178 }
179 json_data, err := json.Marshal(data)
180
181 response.Header().Set("Content-Type", "text/json")
182 if err != nil {
183 io.WriteString(response, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}")
184 } else {
185 response.Write(json_data)
186 }
187 }
188
189 func okResponse(response http.ResponseWriter, data interface{}) {
190 response.Header().Set("Content-Type", "text/json")
191 json_data, err := json.Marshal(data)
192 if err != nil {
193 errorResponse(response, "Internal encoding error")
194 } else {
195 response.Write(json_data)
196 }
197 }
198
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)
206
207 gConfig = config
208
209 error := http.ListenAndServe(fmt.Sprintf(":%d", config.Port), mux)
210 fmt.Printf("error %v", error)
211 }