]> src.bluestatic.org Git - armadillo.git/blob - src/server.go
Try to get the regex to work
[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"), os.O_RDONLY, 0)
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] string {
71 "path" : target,
72 }
73 okResponse(response, data)
74 }
75 case "tv_rename":
76 newPath, err := tv_rename.RenameEpisode(request.FormValue("path"))
77 if err != nil {
78 errorResponse(response, err.String())
79 } else {
80 data := map[string] interface{} {
81 "path" : *newPath,
82 "error" : 0,
83 }
84 okResponse(response, data)
85 }
86 default:
87 fmt.Printf("Invalid action: '%s'\n", request.FormValue("action"))
88 errorResponse(response, "Unhandled action")
89 }
90 }
91
92 func proxyHandler(response http.ResponseWriter, request *http.Request) {
93 rawURL := request.FormValue("url")
94 if len(rawURL) < 1 {
95 return
96 }
97
98 var validURL bool = false
99 for i := range gConfig.ProxyURLs {
100 allowedURL := gConfig.ProxyURLs[i]
101 validURL = validURL || strings.HasPrefix(rawURL, allowedURL)
102 }
103
104 if !validURL {
105 errorResponse(response, "URL is not in proxy whitelist")
106 return
107 }
108
109 url, err := http.ParseURL(rawURL)
110 if err != nil {
111 errorResponse(response, err.String())
112 return
113 }
114 err = performProxy(url, response, request)
115 if err != nil {
116 errorResponse(response, err.String())
117 }
118 }
119
120 func performProxy(url *http.URL, response http.ResponseWriter, origRequest *http.Request) os.Error {
121 conn, err := net.Dial("tcp", "", url.Host + ":http")
122 if err != nil {
123 return err
124 }
125 client := http.NewClientConn(conn, nil)
126 var request http.Request
127 request.URL = url
128 request.Method = "GET"
129 request.UserAgent = origRequest.UserAgent
130 err = client.Write(&request)
131 if err != nil {
132 return err
133 }
134 var proxyResponse *http.Response
135 proxyResponse, err = client.Read()
136 if err != nil && err != http.ErrPersistEOF {
137 return err
138 }
139 _, err = io.Copy(response, proxyResponse.Body)
140 return err
141 }
142
143 func errorResponse(response http.ResponseWriter, message string) {
144 message = strings.Replace(message, gConfig.JailRoot, "/", -1)
145 data := map[string] string {
146 "error" : "-1",
147 "message" : message,
148 }
149 json_data, err := json.Marshal(data)
150
151 response.SetHeader("Content-Type", "text/json")
152 if err != nil {
153 io.WriteString(response, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}")
154 } else {
155 response.Write(json_data)
156 }
157 }
158
159 func okResponse(response http.ResponseWriter, data interface{}) {
160 response.SetHeader("Content-Type", "text/json")
161 json_data, err := json.Marshal(data)
162 if err != nil {
163 errorResponse(response, "Internal encoding error")
164 } else {
165 response.Write(json_data)
166 }
167 }
168
169 func RunBackEnd(config *config.Configuration) {
170 mux := http.NewServeMux()
171 mux.HandleFunc("/", indexHandler)
172 mux.Handle("/fe/", http.FileServer(kFrontEndFiles, "/fe/"))
173 mux.HandleFunc("/service", serviceHandler)
174 mux.HandleFunc("/proxy", proxyHandler)
175
176 gConfig = config
177
178 error := http.ListenAndServe(fmt.Sprintf(":%d", config.Port), mux)
179 fmt.Printf("error %v", error)
180 }