diff --git a/main.go b/main.go index 3899284..0566d1a 100644 --- a/main.go +++ b/main.go @@ -75,6 +75,7 @@ func main() { func query(w http.ResponseWriter, r *http.Request) { w.Header().Set("Server", "SQLiteQueryServer v"+version) + // w.Header().Set("X-Content-Type-Options", "nosniff") // prevent browsers from doing MIME-type sniffing if r.URL.Path != "/query" { http.Error(w, helpMessage, http.StatusNotFound) @@ -85,18 +86,18 @@ func query(w http.ResponseWriter, r *http.Request) { return } - wFlusher, ok := w.(http.Flusher) - if !ok { - http.Error(w, - fmt.Sprintf("Error creating a stream writer.\n\n%s", helpMessage), http.StatusInternalServerError) - return - } + // wFlusher, ok := w.(http.Flusher) + // if !ok { + // http.Error(w, + // fmt.Sprintf("Error creating a stream writer.\n\n%s", helpMessage), http.StatusInternalServerError) + // return + // } w.Header().Set("Content-Type", "application/json") outpoutEncoder := json.NewEncoder(w) // start printing the outer array fmt.Fprintf(w, "[") - wFlusher.Flush() + // wFlusher.Flush() reqCsvReader := csv.NewReader(r.Body) reqCsvReader.ReuseRecord = true @@ -116,7 +117,7 @@ func query(w http.ResponseWriter, r *http.Request) { if !isFirstQuery { // print comma between queries results fmt.Fprintf(w, ",") - wFlusher.Flush() + // wFlusher.Flush() } isFirstQuery = false @@ -147,14 +148,14 @@ func query(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, `"headers":`) outpoutEncoder.Encode(cols) fmt.Fprintf(w, `,"out":[`) // start printing the out rows array - wFlusher.Flush() + // wFlusher.Flush() isFirstRow := true for rows.Next() { if !isFirstRow { // print comma between rows fmt.Fprintf(w, ",") - wFlusher.Flush() + // wFlusher.Flush() } isFirstRow = false @@ -184,33 +185,12 @@ func query(w http.ResponseWriter, r *http.Request) { // finish printing a query result fmt.Fprintf(w, "]}") - wFlusher.Flush() + // wFlusher.Flush() } // finish printing the outer array fmt.Fprintf(w, "]\n") - wFlusher.Flush() -} - -func countParams() int { - rows, err := queryStmt.Query() - if err != nil { - regex := regexp.MustCompile(`sql: expected (\d+) arguments, got 0`) - regexSubmatches := regex.FindAllStringSubmatch(err.Error(), 1) - if len(regexSubmatches) != 1 || len(regexSubmatches[0]) != 2 { - // this is weird, return best guess - return strings.Count(queryString, "?") - } - count, err := strconv.Atoi(regexSubmatches[0][1]) - if err != nil { - // this is weirder because the regex is \d+ - // return best guess - return strings.Count(queryString, "?") - } - return count - } - rows.Close() - return 0 + // wFlusher.Flush() } func buildHelpMessage() { @@ -273,3 +253,24 @@ func buildHelpMessage() { - Element #1 is the result of query #1, Element #2 is the result of query #2, and so forth. `, serverPort) } + +func countParams() int { + rows, err := queryStmt.Query() + if err != nil { + regex := regexp.MustCompile(`sql: expected (\d+) arguments, got 0`) + regexSubmatches := regex.FindAllStringSubmatch(err.Error(), 1) + if len(regexSubmatches) != 1 || len(regexSubmatches[0]) != 2 { + // this is weird, return best guess + return strings.Count(queryString, "?") + } + count, err := strconv.Atoi(regexSubmatches[0][1]) + if err != nil { + // this is weirder because the regex is \d+ + // return best guess + return strings.Count(queryString, "?") + } + return count + } + rows.Close() + return 0 +}