]>
src.bluestatic.org Git - armadillo.git/blob - src/server.go
2 // Armadillo File Manager
3 // Copyright (c) 2010, Robert Sesek <http://www.bluestatic.org>
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.
26 var dir
, file
= path
.Split(path
.Clean(os
.Getenv("_")))
27 var kFrontEndFiles
string = path
.Join(dir
, "fe")
28 var gConfig
*config
.Configuration
= nil
30 func indexHandler(response http
.ResponseWriter
, request
*http
.Request
) {
31 fd
, err
:= os
.Open(path
.Join(kFrontEndFiles
, "index.html"))
33 fmt
.Print("Error opening file ", err
.String(), "\n")
39 func serviceHandler(response http
.ResponseWriter
, request
*http
.Request
) {
40 if request
.Method
!= "POST" {
41 io
.WriteString(response
, "Error: Not a POST request")
45 switch request
.FormValue("action") {
47 files
, err
:= paths
.List(request
.FormValue("path"))
49 errorResponse(response
, err
.String())
51 okResponse(response
, files
)
54 err
:= paths
.Remove(request
.FormValue("path"))
56 errorResponse(response
, err
.String())
58 data
:= map[string]int{
61 okResponse(response
, data
)
64 source
:= request
.FormValue("source")
65 target
:= request
.FormValue("target")
66 err
:= paths
.Move(source
, target
)
68 errorResponse(response
, err
.String())
70 data
:= map[string]interface{}{
74 okResponse(response
, data
)
77 newPath
, err
:= tv_rename
.RenameEpisode(request
.FormValue("path"))
79 errorResponse(response
, err
.String())
81 data
:= map[string]interface{}{
85 okResponse(response
, data
)
88 fmt
.Printf("Invalid action: '%s'\n", request
.FormValue("action"))
89 errorResponse(response
, "Unhandled action")
93 func proxyHandler(response http
.ResponseWriter
, request
*http
.Request
) {
94 rawURL
:= request
.FormValue("url")
99 var validURL
bool = false
100 for i
:= range gConfig
.ProxyURLs
{
101 allowedURL
:= gConfig
.ProxyURLs
[i
]
102 validURL
= validURL || strings
.HasPrefix(rawURL
, allowedURL
)
106 errorResponse(response
, "URL is not in proxy whitelist")
110 url
, err
:= http
.ParseURL(rawURL
)
112 errorResponse(response
, err
.String())
115 err
= performProxy(url
, response
, request
)
117 errorResponse(response
, err
.String())
121 func performProxy(url
*http
.URL
, response http
.ResponseWriter
, origRequest
*http
.Request
) os
.Error
{
122 conn
, err
:= net
.Dial("tcp", url
.Host
+":http")
126 client
:= http
.NewClientConn(conn
, nil)
127 var request http
.Request
129 request
.Method
= "GET"
130 request
.UserAgent
= origRequest
.UserAgent
131 err
= client
.Write(&request
)
135 var proxyResponse
*http
.Response
136 proxyResponse
, err
= client
.Read(&request
)
137 if err
!= nil && err
!= http
.ErrPersistEOF
{
140 _
, err
= io
.Copy(response
, proxyResponse
.Body
)
144 func downloadHandler(response http
.ResponseWriter
, request
*http
.Request
) {
145 valid
, fullPath
:= paths
.IsValid(request
.FormValue("path"))
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
)
151 http
.ServeFile(response
, request
, fullPath
)
154 http
.NotFound(response
, request
)
158 func errorResponse(response http
.ResponseWriter
, message
string) {
159 message
= strings
.Replace(message
, gConfig
.JailRoot
, "/", -1)
160 data
:= map[string]interface{}{
164 json_data
, err
:= json
.Marshal(data
)
166 response
.Header().Set("Content-Type", "text/json")
168 io
.WriteString(response
, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}")
170 response
.Write(json_data
)
174 func okResponse(response http
.ResponseWriter
, data
interface{}) {
175 response
.Header().Set("Content-Type", "text/json")
176 json_data
, err
:= json
.Marshal(data
)
178 errorResponse(response
, "Internal encoding error")
180 response
.Write(json_data
)
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
)
194 error
:= http
.ListenAndServe(fmt
.Sprintf(":%d", config
.Port
), mux
)
195 fmt
.Printf("error %v", error
)