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