[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [check/master] Dry out querystring parsing.



commit c656794133794c1acc9a0e7a74f0cd380c42a110
Author: Arlo Breault <arlolra@xxxxxxxxx>
Date:   Wed Oct 2 19:18:47 2013 -0700

    Dry out querystring parsing.
---
 handlers.go |   20 ++------------------
 utils.go    |   15 +++++++++++++++
 2 files changed, 17 insertions(+), 18 deletions(-)

diff --git a/handlers.go b/handlers.go
index 574f28f..39bb584 100644
--- a/handlers.go
+++ b/handlers.go
@@ -5,7 +5,6 @@ import (
 	"html/template"
 	"net"
 	"net/http"
-	"strconv"
 	"time"
 )
 
@@ -116,23 +115,8 @@ func BulkHandler(Layout *template.Template, Exits *Exits) func(http.ResponseWrit
 			return
 		}
 
-		port_str := q.Get("port")
-		port, err := strconv.Atoi(port_str)
-		if err != nil {
-			port = 80
-			port_str = ""
-		} else {
-			port_str = "&port=" + port_str
-		}
-
-		n_str := q.Get("n")
-		n, err := strconv.Atoi(n_str)
-		if err != nil {
-			n = 16
-			n_str = ""
-		} else {
-			n_str = "&n=" + n_str
-		}
+		port, port_str := GetQS(q, "port", 80)
+		n, n_str := GetQS(q, "n", 16)
 
 		str := fmt.Sprintf("# This is a list of all Tor exit nodes from the past %d hours that can contact %s on port %d #\n", n, ip, port)
 		str += fmt.Sprintf("# You can update this list by visiting https://check.torproject.org/cgi-bin/TorBulkExitList.py?ip=%s%s%s #\n", ip, port_str, n_str)
diff --git a/utils.go b/utils.go
index 46e00b2..eb8a66d 100644
--- a/utils.go
+++ b/utils.go
@@ -2,6 +2,7 @@ package main
 
 import (
 	"encoding/json"
+	"fmt"
 	"github.com/samuel/go-gettext/gettext"
 	"html/template"
 	"io"
@@ -9,6 +10,8 @@ import (
 	"log"
 	"net/http"
 	"os"
+	"strconv"
+	"net/url"
 )
 
 func UpToDate(r *http.Request) bool {
@@ -33,6 +36,18 @@ func Lang(r *http.Request) string {
 	return lang
 }
 
+func GetQS(q url.Values, param string, deflt int) (num int, str string) {
+	str = q.Get(param)
+	num, err := strconv.Atoi(str)
+	if err != nil {
+		num = deflt
+		str = ""
+	} else {
+		str = fmt.Sprintf("&%s=%s", param, str)
+	}
+	return
+}
+
 func FuncMap(domain *gettext.Domain) template.FuncMap {
 	return template.FuncMap{
 		"UnEscaped": func(x string) interface{} {

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits