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