|
|
@ -21,39 +21,34 @@ |
|
|
|
* |
|
|
|
* |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package main |
|
|
|
package main |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
|
|
|
|
"binnit/paste" |
|
|
|
|
|
|
|
"flag" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
|
|
|
|
"html" |
|
|
|
|
|
|
|
"io" |
|
|
|
"log" |
|
|
|
"log" |
|
|
|
"net/http" |
|
|
|
"net/http" |
|
|
|
"os" |
|
|
|
"os" |
|
|
|
"path/filepath" |
|
|
|
"path/filepath" |
|
|
|
"time" |
|
|
|
"time" |
|
|
|
"io" |
|
|
|
|
|
|
|
"binnit/paste" |
|
|
|
|
|
|
|
"flag" |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var conf_file = flag.String("c", "./binnit.cfg", "Configuration file for binnit") |
|
|
|
var conf_file = flag.String("c", "./binnit.cfg", "Configuration file for binnit") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var p_conf = Config{ |
|
|
|
var p_conf = Config{ |
|
|
|
server_name: "localhost", |
|
|
|
server_name: "localhost", |
|
|
|
bind_addr: "0.0.0.0", |
|
|
|
bind_addr: "0.0.0.0", |
|
|
|
bind_port: "8000", |
|
|
|
bind_port: "8000", |
|
|
|
paste_dir: "./pastes", |
|
|
|
paste_dir: "./pastes", |
|
|
|
templ_dir: "./tmpl", |
|
|
|
templ_dir: "./tmpl", |
|
|
|
max_size: 4096, |
|
|
|
max_size: 4096, |
|
|
|
log_file: "./binnit.log", |
|
|
|
log_file: "./binnit.log", |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func min(a, b int) int { |
|
|
|
|
|
|
|
|
|
|
|
func min (a, b int) int { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if a > b { |
|
|
|
if a > b { |
|
|
|
return b |
|
|
|
return b |
|
|
@ -76,12 +71,16 @@ func handle_get_paste(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
|
|
|
|
|
|
// The default is to serve index.html
|
|
|
|
// The default is to serve index.html
|
|
|
|
if (orig_name == "/") || (orig_name == "/index.html") { |
|
|
|
if (orig_name == "/") || (orig_name == "/index.html") { |
|
|
|
http.ServeFile(w, r, p_conf.templ_dir + "/index.html") |
|
|
|
http.ServeFile(w, r, p_conf.templ_dir+"/index.html") |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
// otherwise, if the requested paste exists, we serve it...
|
|
|
|
// otherwise, if the requested paste exists, we serve it...
|
|
|
|
|
|
|
|
|
|
|
|
title, date, content, err := paste.Retrieve(paste_name) |
|
|
|
title, date, content, err := paste.Retrieve(paste_name) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
title = html.EscapeString(title) |
|
|
|
|
|
|
|
date = html.EscapeString(date) |
|
|
|
|
|
|
|
content = html.EscapeString(content) |
|
|
|
|
|
|
|
|
|
|
|
if err == nil { |
|
|
|
if err == nil { |
|
|
|
s, err := prepare_paste_page(title, date, content, p_conf.templ_dir) |
|
|
|
s, err := prepare_paste_page(title, date, content, p_conf.templ_dir) |
|
|
|
if err == nil { |
|
|
|
if err == nil { |
|
|
@ -101,10 +100,12 @@ func handle_get_paste(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
|
|
|
|
|
|
func handle_put_paste(w http.ResponseWriter, r *http.Request) { |
|
|
|
func handle_put_paste(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err1 := r.ParseForm() |
|
|
|
|
|
|
|
err2 := r.ParseMultipartForm(int64(2 * p_conf.max_size)) |
|
|
|
|
|
|
|
|
|
|
|
if err := r.ParseForm(); err != nil { |
|
|
|
if err1 != nil && err2 != nil { |
|
|
|
// Invalid POST -- let's serve the default file
|
|
|
|
// Invalid POST -- let's serve the default file
|
|
|
|
http.ServeFile(w, r, p_conf.templ_dir + "/index.html") |
|
|
|
http.ServeFile(w, r, p_conf.templ_dir+"/index.html") |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
req_body := r.PostForm |
|
|
|
req_body := r.PostForm |
|
|
|
|
|
|
|
|
|
|
@ -121,14 +122,15 @@ func handle_put_paste(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
|
|
|
|
|
|
ID, err := paste.Store(title, date, content, p_conf.paste_dir) |
|
|
|
ID, err := paste.Store(title, date, content, p_conf.paste_dir) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.Printf(" title: %s\npaste: %s\n", title, content) |
|
|
|
log.Printf(" ID: %s; err: %s\n", ID, err) |
|
|
|
log.Printf(" ID: %s; err: %s\n", ID, err) |
|
|
|
|
|
|
|
|
|
|
|
if err == nil { |
|
|
|
if err == nil { |
|
|
|
hostname := p_conf.server_name |
|
|
|
hostname := p_conf.server_name |
|
|
|
if show := req_body.Get("show"); show != "1" { |
|
|
|
if show := req_body.Get("show"); show != "1" { |
|
|
|
fmt.Fprintf(w, "http://%s/%s", hostname, ID) |
|
|
|
fmt.Fprintf(w, "http://%s/%s\n", hostname, ID) |
|
|
|
return |
|
|
|
return |
|
|
|
} else{ |
|
|
|
} else { |
|
|
|
fmt.Fprintf(w, "<html><body>Link: <a href='http://%s/%s'>http://%s/%s</a></body></html>", |
|
|
|
fmt.Fprintf(w, "<html><body>Link: <a href='http://%s/%s'>http://%s/%s</a></body></html>", |
|
|
|
hostname, ID, hostname, ID) |
|
|
|
hostname, ID, hostname, ID) |
|
|
|
return |
|
|
|
return |
|
|
@ -139,7 +141,6 @@ func handle_put_paste(w http.ResponseWriter, r *http.Request) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func req_handler(w http.ResponseWriter, r *http.Request) { |
|
|
|
func req_handler(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
|
|
|
|
|
|
switch r.Method { |
|
|
|
switch r.Method { |
|
|
@ -152,23 +153,19 @@ func req_handler(w http.ResponseWriter, r *http.Request) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func main() { |
|
|
|
func main() { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
flag.Parse() |
|
|
|
flag.Parse() |
|
|
|
|
|
|
|
|
|
|
|
parse_config(*conf_file, &p_conf) |
|
|
|
parse_config(*conf_file, &p_conf) |
|
|
|
|
|
|
|
|
|
|
|
f, err := os.OpenFile(p_conf.log_file, os.O_APPEND | os.O_CREATE | os.O_RDWR, 0600) |
|
|
|
f, err := os.OpenFile(p_conf.log_file, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0600) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
fmt.Fprintf(os.Stderr, "Error opening log_file: %s. Exiting\n", p_conf.log_file) |
|
|
|
fmt.Fprintf(os.Stderr, "Error opening log_file: %s. Exiting\n", p_conf.log_file) |
|
|
|
os.Exit(1) |
|
|
|
os.Exit(1) |
|
|
|
} |
|
|
|
} |
|
|
|
defer f.Close() |
|
|
|
defer f.Close() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.SetOutput(io.Writer(f)) |
|
|
|
log.SetOutput(io.Writer(f)) |
|
|
|
log.SetPrefix("[binnit]: ") |
|
|
|
log.SetPrefix("[binnit]: ") |
|
|
|
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds) |
|
|
|
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds) |
|
|
@ -176,7 +173,7 @@ func main() { |
|
|
|
log.Println("Binnit version 0.1 -- Starting ") |
|
|
|
log.Println("Binnit version 0.1 -- Starting ") |
|
|
|
log.Printf(" + Config file: %s\n", *conf_file) |
|
|
|
log.Printf(" + Config file: %s\n", *conf_file) |
|
|
|
log.Printf(" + Serving pastes on: %s\n", p_conf.server_name) |
|
|
|
log.Printf(" + Serving pastes on: %s\n", p_conf.server_name) |
|
|
|
log.Printf(" + listening on: %s:%s\n", p_conf.bind_addr, p_conf.bind_port ) |
|
|
|
log.Printf(" + listening on: %s:%s\n", p_conf.bind_addr, p_conf.bind_port) |
|
|
|
log.Printf(" + paste_dir: %s\n", p_conf.paste_dir) |
|
|
|
log.Printf(" + paste_dir: %s\n", p_conf.paste_dir) |
|
|
|
log.Printf(" + templ_dir: %s\n", p_conf.templ_dir) |
|
|
|
log.Printf(" + templ_dir: %s\n", p_conf.templ_dir) |
|
|
|
log.Printf(" + max_size: %d\n", p_conf.max_size) |
|
|
|
log.Printf(" + max_size: %d\n", p_conf.max_size) |
|
|
@ -184,5 +181,5 @@ func main() { |
|
|
|
// FIXME: create paste_dir if it does not exist
|
|
|
|
// FIXME: create paste_dir if it does not exist
|
|
|
|
|
|
|
|
|
|
|
|
http.HandleFunc("/", req_handler) |
|
|
|
http.HandleFunc("/", req_handler) |
|
|
|
log.Fatal(http.ListenAndServe(p_conf.bind_addr + ":" + p_conf.bind_port, nil)) |
|
|
|
log.Fatal(http.ListenAndServe(p_conf.bind_addr+":"+p_conf.bind_port, nil)) |
|
|
|
} |
|
|
|
} |
|
|
|