]>
src.bluestatic.org Git - armadillo.git/blob - src/server.go
2 // Armadillo File Manager
3 // Copyright (c) 2010-2011, 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.
25 var dir
, file
= path
.Split(path
.Clean(os
.Getenv("_")))
26 var kFrontEndFiles
string = path
.Join(dir
, "fe")
27 var gConfig
*config
.Configuration
= nil
29 func indexHandler(response http
.ResponseWriter
, request
*http
.Request
) {
30 var fileName
string = "index.html"
31 if strings
.HasPrefix(request
.URL
.Path
, "/_@/") {
32 fileName
= "service_request.html"
34 fd
, err
:= os
.Open(path
.Join(kFrontEndFiles
, fileName
), os
.O_RDONLY
, 0)
36 fmt
.Print("Error opening file ", err
.String(), "\n")
42 func serviceHandler(response http
.ResponseWriter
, request
*http
.Request
) {
43 if request
.Method
!= "POST" {
44 io
.WriteString(response
, "Error: Not a POST request")
48 switch request
.FormValue("action") {
50 files
, err
:= paths
.List(request
.FormValue("path"))
52 errorResponse(response
, err
.String())
54 okResponse(response
, files
)
57 err
:= paths
.Remove(request
.FormValue("path"))
59 errorResponse(response
, err
.String())
61 data
:= map[string] int {
64 okResponse(response
, data
)
67 source
:= request
.FormValue("source")
68 target
:= request
.FormValue("target")
69 err
:= paths
.Move(source
, target
)
71 errorResponse(response
, err
.String())
73 data
:= map[string] string {
76 okResponse(response
, data
)
79 errorResponse(response
, "Unhandled action")
83 func proxyHandler(response http
.ResponseWriter
, request
*http
.Request
) {
84 rawURL
:= request
.FormValue("url")
89 var validURL
bool = false
90 for i
:= range gConfig
.ProxyURLs
{
91 allowedURL
:= gConfig
.ProxyURLs
[i
]
92 validURL
= validURL || strings
.HasPrefix(rawURL
, allowedURL
)
96 errorResponse(response
, "URL is not in proxy whitelist")
100 url
, err
:= http
.ParseURL(rawURL
)
102 errorResponse(response
, err
.String())
105 err
= performProxy(url
, response
, request
)
107 errorResponse(response
, err
.String())
111 func performProxy(url
*http
.URL
, response http
.ResponseWriter
, origRequest
*http
.Request
) os
.Error
{
112 conn
, err
:= net
.Dial("tcp", "", url
.Host
+ ":http")
116 client
:= http
.NewClientConn(conn
, nil)
117 var request http
.Request
119 request
.Method
= "GET"
120 request
.UserAgent
= origRequest
.UserAgent
121 err
= client
.Write(&request
)
125 var proxyResponse
*http
.Response
126 proxyResponse
, err
= client
.Read()
127 if err
!= nil && err
!= http
.ErrPersistEOF
{
130 _
, err
= io
.Copy(response
, proxyResponse
.Body
)
134 func errorResponse(response http
.ResponseWriter
, message
string) {
135 message
= strings
.Replace(message
, gConfig
.JailRoot
, "/", -1)
136 data
:= map[string] string {
140 json_data
, err
:= json
.Marshal(data
)
142 response
.SetHeader("Content-Type", "text/json")
144 io
.WriteString(response
, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}")
146 response
.Write(json_data
)
150 func okResponse(response http
.ResponseWriter
, data
interface{}) {
151 response
.SetHeader("Content-Type", "text/json")
152 json_data
, err
:= json
.Marshal(data
)
154 errorResponse(response
, "Internal encoding error")
156 response
.Write(json_data
)
160 func RunBackEnd(config
*config
.Configuration
) {
161 mux
:= http
.NewServeMux()
162 mux
.HandleFunc("/", indexHandler
)
163 mux
.Handle("/fe/", http
.FileServer(kFrontEndFiles
, "/fe/"))
164 mux
.HandleFunc("/service", serviceHandler
)
165 mux
.HandleFunc("/proxy", proxyHandler
)
169 error
:= http
.ListenAndServe(fmt
.Sprintf(":%d", config
.Port
), mux
)
170 fmt
.Printf("error %v", error
)