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