2013-02-02 15:01:01 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-04-20 01:21:40 -07:00
|
|
|
"io/ioutil"
|
2013-02-02 15:01:01 -08:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2013-04-20 01:21:40 -07:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2013-02-02 15:01:01 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
form url.Values
|
|
|
|
header http.Header
|
|
|
|
cookies []*http.Cookie
|
2013-04-20 01:21:40 -07:00
|
|
|
writer http.ResponseWriter
|
|
|
|
request http.Request
|
|
|
|
exit_error bool
|
2013-02-02 15:01:01 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
func initServer() {
|
2013-04-20 01:21:40 -07:00
|
|
|
if config.Port == 0 {
|
|
|
|
config.Port = 80
|
2013-02-02 15:01:01 -08:00
|
|
|
}
|
2013-04-20 01:21:40 -07:00
|
|
|
listener,err := net.Listen("tcp", config.Domain+":"+strconv.Itoa(config.Port))
|
2013-02-02 15:01:01 -08:00
|
|
|
if(err != nil) {
|
|
|
|
error_log.Write(err.Error())
|
2013-04-20 01:21:40 -07:00
|
|
|
fmt.Printf("Failed listening on "+config.Domain+":%d, see log for details",config.Port)
|
2013-02-02 15:01:01 -08:00
|
|
|
os.Exit(2)
|
|
|
|
}
|
2013-04-20 01:21:40 -07:00
|
|
|
http.Handle("/", makeHandler(fileHandle))
|
|
|
|
http.Handle("/manage",makeHandler(callManageFunction))
|
|
|
|
http.Handle("/post",makeHandler(makePost))
|
|
|
|
//http.Handle("/util",makeHandler(utilHandler))
|
2013-02-02 15:01:01 -08:00
|
|
|
http.Serve(listener, nil)
|
|
|
|
}
|
|
|
|
|
2013-04-20 01:21:40 -07:00
|
|
|
func fileHandle(w http.ResponseWriter, r *http.Request) {
|
|
|
|
request = *r
|
|
|
|
writer = w
|
|
|
|
cookies = request.Cookies()
|
|
|
|
request.ParseForm()
|
|
|
|
form = request.Form
|
|
|
|
request_url := request.URL.Path
|
|
|
|
|
|
|
|
filepath := path.Join(config.DocumentRoot, request_url)
|
|
|
|
results,err := os.Stat(filepath)
|
2013-06-09 11:57:11 -07:00
|
|
|
restricted := false // if true, user doesn't have permission to view the file, because read-banned, etc
|
2013-04-20 01:21:40 -07:00
|
|
|
|
2013-02-02 15:01:01 -08:00
|
|
|
if err == nil {
|
2013-04-20 01:21:40 -07:00
|
|
|
//the file exists, or there is a folder here
|
|
|
|
if results.IsDir() {
|
|
|
|
found_index := false
|
|
|
|
newpath := ""
|
|
|
|
|
|
|
|
//check to see if one of the specified index pages exists
|
|
|
|
for i := 0; i < len(config.FirstPage); i++ {
|
|
|
|
newpath = path.Join(filepath,config.FirstPage[i])
|
|
|
|
_,err := os.Stat(newpath)
|
|
|
|
if err == nil {
|
|
|
|
serveFile(w, newpath)
|
|
|
|
found_index = true
|
|
|
|
break
|
2013-02-02 15:01:01 -08:00
|
|
|
}
|
|
|
|
}
|
2013-04-20 01:21:40 -07:00
|
|
|
|
|
|
|
if !found_index {
|
|
|
|
error404()
|
|
|
|
}
|
2013-02-02 15:01:01 -08:00
|
|
|
} else {
|
2013-04-20 01:21:40 -07:00
|
|
|
//the file exists, and is not a folder
|
|
|
|
//writer.Header().Add("Cache-Control", fmt.Sprintf("max-age=%d, public, must-revalidate, proxy-revalidate", 500))
|
2013-06-09 11:57:11 -07:00
|
|
|
if request_url == path.Join(config.SiteWebfolder+"javascript/manage.js") {
|
|
|
|
if getStaffRank() == 0 {
|
|
|
|
// we aren't logged in and tried to access manage.js
|
|
|
|
restricted = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !restricted {
|
|
|
|
serveFile(w, filepath)
|
|
|
|
}
|
2013-02-02 15:01:01 -08:00
|
|
|
}
|
2013-04-20 01:21:40 -07:00
|
|
|
} else {
|
|
|
|
//there is nothing at the requested address
|
|
|
|
error404()
|
2013-02-02 15:01:01 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-20 01:21:40 -07:00
|
|
|
func makeHandler(fn func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
//defer serverError()
|
|
|
|
if !exit_error {
|
|
|
|
fn(w, r)
|
|
|
|
exit_error = false
|
|
|
|
} else {
|
|
|
|
exit_error = false
|
|
|
|
}
|
2013-02-02 15:01:01 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-02 14:04:34 -07:00
|
|
|
func exitWithErrorPage(w http.ResponseWriter, err string) {
|
2013-04-20 01:21:40 -07:00
|
|
|
error_page_bytes,_ := ioutil.ReadFile("templates/error.html")
|
|
|
|
error_page := string(error_page_bytes)
|
|
|
|
error_page = strings.Replace(error_page,"{ERRORTEXT}", err,-1)
|
2013-06-02 14:04:34 -07:00
|
|
|
fmt.Fprintf(w,error_page)
|
2013-04-20 01:21:40 -07:00
|
|
|
exit_error = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func redirect(location string) {
|
2013-06-02 14:04:34 -07:00
|
|
|
http.Redirect(writer,&request,location,http.StatusFound)
|
2013-04-20 01:21:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func error404() {
|
|
|
|
http.ServeFile(writer, &request, path.Join(config.DocumentRoot, "/error/404.html"))
|
|
|
|
error_log.Write("Error: 404 Not Found from " + request.RemoteAddr + " @ " + request.RequestURI)
|
|
|
|
}
|
|
|
|
|
|
|
|
func serverError() {
|
|
|
|
if _, ok := recover().(error); ok {
|
|
|
|
//something went wrong, now we need to throw a 500
|
|
|
|
http.ServeFile(writer,&request, path.Join(config.DocumentRoot, "/error/500.html"))
|
|
|
|
error_log.Write("Error: 500 Internal Server error from " + request.RemoteAddr + " @ " + request.RequestURI)
|
|
|
|
return
|
2013-02-02 15:01:01 -08:00
|
|
|
}
|
2013-04-20 01:21:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func serveFile(w http.ResponseWriter, filepath string) {
|
|
|
|
http.ServeFile(w, &request, filepath)
|
|
|
|
access_log.Write("Success: 200 from " + request.RemoteAddr + " @ " + request.RequestURI)
|
2013-06-04 18:04:56 -07:00
|
|
|
}
|