Start implementing the proxy service handler.
[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 "os"
18 "path"
19 "strings"
20 "./config"
21 "./paths"
22 )
23
24 var dir, file = path.Split(path.Clean(os.Getenv("_")))
25 var kFrontEndFiles string = path.Join(dir, "fe")
26 var gConfig *config.Configuration = nil
27
28 func indexHandler(connection *http.Conn, request *http.Request) {
29 fd, err := os.Open(path.Join(kFrontEndFiles, "index.html"), os.O_RDONLY, 0)
30 if err != nil {
31 fmt.Print("Error opening file ", err.String(), "\n")
32 return
33 }
34 io.Copy(connection, fd)
35 }
36
37 func serviceHandler(connection *http.Conn, request *http.Request) {
38 if request.Method != "POST" {
39 io.WriteString(connection, "Error: Not a POST request")
40 return
41 }
42
43 switch request.FormValue("action") {
44 case "list":
45 files, err := paths.List(request.FormValue("path"))
46 if err != nil {
47 errorResponse(connection, err.String())
48 } else {
49 okResponse(connection, files)
50 }
51 case "remove":
52 err := paths.Remove(request.FormValue("path"))
53 if err != nil {
54 errorResponse(connection, err.String())
55 } else {
56 response := map[string] int {
57 "error" : 0,
58 }
59 okResponse(connection, response)
60 }
61 case "move":
62 source := request.FormValue("source")
63 target := request.FormValue("target")
64 err := paths.Move(source, target)
65 if err != nil {
66 errorResponse(connection, err.String())
67 } else {
68 response := map[string] string {
69 "path" : target,
70 }
71 okResponse(connection, response)
72 }
73 default:
74 errorResponse(connection, "Unhandled action")
75 }
76 }
77
78 func proxyHandler(connection *http.Conn, request *http.Request) {
79 url := request.FormValue("url")
80 if len(url) < 1 {
81 return
82 }
83
84 for i := range gConfig.ProxyURLs {
85 allowedURL := gConfig.ProxyURLs[i]
86 io.WriteString(connection, allowedURL)
87 }
88 }
89
90 func errorResponse(connection *http.Conn, message string) {
91 message = strings.Replace(message, paths.JailRoot, "/", -1)
92 response := map[string] string {
93 "error" : "-1",
94 "message" : message,
95 }
96 json_data, err := json.Marshal(response)
97
98 connection.SetHeader("Content-Type", "text/json")
99 if err != nil {
100 io.WriteString(connection, "{\"error\":\"-9\",\"message\":\"Internal encoding error\"}")
101 } else {
102 connection.Write(json_data)
103 }
104 }
105
106 func okResponse(connection *http.Conn, data interface{}) {
107 connection.SetHeader("Content-Type", "text/json")
108 json_data, err := json.Marshal(data)
109 if err != nil {
110 errorResponse(connection, "Internal encoding error")
111 } else {
112 connection.Write(json_data)
113 }
114 }
115
116 func RunFrontEnd(config *config.Configuration) {
117 mux := http.NewServeMux()
118 mux.HandleFunc("/", indexHandler)
119 mux.Handle("/fe/", http.FileServer(kFrontEndFiles, "/fe/"))
120 mux.HandleFunc("/service", serviceHandler)
121 mux.HandleFunc("/proxy", proxyHandler)
122
123 gConfig = config
124
125 error := http.ListenAndServe(fmt.Sprintf(":%d", config.Port), mux)
126 fmt.Printf("error %v", error)
127 }