]> src.bluestatic.org Git - armadillo.git/blob - src/server.go
Error out if a filename can't be parsed in the TVRenamer.
[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 )
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(connection *http.Conn, request *http.Request) {
30 fd, err := os.Open(path.Join(kFrontEndFiles, "index.html"), os.O_RDONLY, 0)
31 if err != nil {
32 fmt.Print("Error opening file ", err.String(), "\n")
33 return
34 }
35 io.Copy(connection, fd)
36 }
37
38 func serviceHandler(connection *http.Conn, request *http.Request) {
39 if request.Method != "POST" {
40 io.WriteString(connection, "Error: Not a POST request")
41 return
42 }
43
44 switch request.FormValue("action") {
45 case "list":
46 files, err := paths.List(request.FormValue("path"))
47 if err != nil {
48 errorResponse(connection, err.String())
49 } else {
50 okResponse(connection, files)
51 }
52 case "remove":
53 err := paths.Remove(request.FormValue("path"))
54 if err != nil {
55 errorResponse(connection, err.String())
56 } else {
57 response := map[string] int {
58 "error" : 0,
59 }
60 okResponse(connection, response)
61 }
62 case "move":
63 source := request.FormValue("source")
64 target := request.FormValue("target")
65 err := paths.Move(source, target)
66 if err != nil {
67 errorResponse(connection, err.String())
68 } else {
69 response := map[string] string {
70 "path" : target,
71 }
72 okResponse(connection, response)
73 }
74 default:
75 errorResponse(connection, "Unhandled action")
76 }
77 }
78
79 func proxyHandler(connection *http.Conn, request *http.Request) {
80 rawURL := request.FormValue("url")
81 if len(rawURL) < 1 {
82 return
83 }
84
85 var validURL bool = false
86 for i := range gConfig.ProxyURLs {
87 allowedURL := gConfig.ProxyURLs[i]
88 validURL = validURL || strings.HasPrefix(rawURL, allowedURL)
89 }
90
91 if !validURL {
92 errorResponse(connection, "URL is not in proxy whitelist")
93 return
94 }
95
96 url, err := http.ParseURL(rawURL)
97 if err != nil {
98 errorResponse(connection, err.String())
99 return
100 }
101 err = performProxy(url, connection, request)
102 if err != nil {
103 errorResponse(connection, err.String())
104 }
105 }
106
107 func performProxy(url *http.URL, response *http.Conn, origRequest *http.Request) os.Error {
108 conn, err := net.Dial("tcp", "", url.Host + ":http")
109 if err != nil {
110 return err
111 }
112 client := http.NewClientConn(conn, nil)
113 var request http.Request
114 request.URL = url
115 request.Method = "GET"
116 request.UserAgent = origRequest.UserAgent
117 err = client.Write(&request)
118 if err != nil {
119 return err
120 }
121 var proxyResponse *http.Response
122 proxyResponse, err = client.Read()
123 if err != nil {
124 return err
125 }
126 _, err = io.Copy(response, proxyResponse.Body)
127 return err
128 }
129
130 func errorResponse(connection *http.Conn, message string) {
131 message = strings.Replace(message, gConfig.JailRoot, "/", -1)
132 response := map[string] string {
133 "error" : "-1",
134 "message" : message,
135 }
136 json_data, err := json.Marshal(response)
137
138 connection.SetHeader("Content-Type", "text/json")
139 if err != nil {
140 io.WriteString(connection, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}")
141 } else {
142 connection.Write(json_data)
143 }
144 }
145
146 func okResponse(connection *http.Conn, data interface{}) {
147 connection.SetHeader("Content-Type", "text/json")
148 json_data, err := json.Marshal(data)
149 if err != nil {
150 errorResponse(connection, "Internal encoding error")
151 } else {
152 connection.Write(json_data)
153 }
154 }
155
156 func RunBackEnd(config *config.Configuration) {
157 mux := http.NewServeMux()
158 mux.HandleFunc("/", indexHandler)
159 mux.Handle("/fe/", http.FileServer(kFrontEndFiles, "/fe/"))
160 mux.HandleFunc("/service", serviceHandler)
161 mux.HandleFunc("/proxy", proxyHandler)
162
163 gConfig = config
164
165 error := http.ListenAndServe(fmt.Sprintf(":%d", config.Port), mux)
166 fmt.Printf("error %v", error)
167 }