func runTest(test testRecord, j int, webaddr string, t *testing.T) { var response *http.Response var err os.Error defer func() { done <- j }() url := "http://" + webaddr + test.URL if response, _, err = http.Get(url); err != nil { t.Error(err) } if response.StatusCode != test.StatusCode { t.Error(j, webaddr, test.URL, "Response had wrong status code:", response.StatusCode) } if len(test.BodyPrefix) > 0 { prefix := make([]byte, len(test.BodyPrefix)) if n, err := response.Body.Read(prefix); err == nil { p := string(prefix[0:n]) if p != test.BodyPrefix { t.Error(j, webaddr, test.URL, "Bad body, expected prefix:", test.BodyPrefix, "got:", p) } } else { t.Error(j, webaddr, test.URL, "Error reading response.Body:", err) } } if test.Headers != nil { for _, hdr := range test.Headers { if v := response.GetHeader(hdr.Key); v != hdr.Val { t.Error(j, webaddr, test.URL, "Header value in response:", strconv.Quote(v), "did not match", strconv.Quote(hdr.Val)) } } } }
func rateLimitStats(resp *http.Response) { if resp == nil { return } curr := time.Seconds() reset, _ := strconv.Atoi64(resp.GetHeader("X-RateLimit-Reset")) remaining, _ := strconv.Atoi64(resp.GetHeader("X-RateLimit-Remaining")) if remaining < 1 && reset-curr > 0 { log.Printf("Twitter API limits exceeded. Sleeping for %d seconds.\n", reset-curr) time.Sleep((reset - curr) * 1e9) } }
func (srv *Server) handler(c net.Conn) { startTime := time.Nanoseconds() defer srv.connectionFinished(c) buf, err := bufio.NewReaderSize(c, 8192) if err != nil { Error("%s Read buffer fail: %v", srv.serverLogPrefix(), err) return } var req *http.Request // no keepalive (for now) reqCount := 0 keepAlive := true for err == nil && keepAlive { if req, err = http.ReadRequest(buf); err == nil { if req.Header.Get("Connection") != "Keep-Alive" { keepAlive = false } request := newRequest(req, c, startTime) reqCount++ var res *http.Response pssInit := new(PipelineStageStat) pssInit.Name = "server.Init" pssInit.StartTime = startTime pssInit.EndTime = time.Nanoseconds() request.appendPipelineStage(pssInit) // execute the pipeline if res = srv.Pipeline.execute(request); res == nil { res = SimpleResponse(req, 404, nil, "Not Found") } // cleanup request.startPipelineStage("server.ResponseWrite") req.Body.Close() wbuf := bufio.NewWriter(c) res.Write(wbuf) wbuf.Flush() if res.Body != nil { res.Body.Close() } request.finishPipelineStage() request.finishRequest() srv.requestFinished(request) } else { // EOF is socket closed if err != io.ErrUnexpectedEOF { Error("%s %v ERROR reading request: %v", srv.serverLogPrefix(), c.RemoteAddr(), err) } } } //Debug("%s Processed %v requests on connection %v", srv.serverLogPrefix(), reqCount, c.RemoteAddr()) }
func SimpleResponse(req *http.Request, status int, headers http.Header, body string) *http.Response { res := new(http.Response) body_rdr := (*fixedResBody)(strings.NewReader(body)) res.StatusCode = status res.ProtoMajor = 1 res.ProtoMinor = 1 res.ContentLength = int64((*strings.Reader)(body_rdr).Len()) res.Request = req res.Header = make(map[string][]string) res.Body = body_rdr if headers != nil { res.Header = headers } return res }
func (p *MockResponseWriter) String() string { resp := new(http.Response) resp.StatusCode = p.StatusCode resp.Proto = "HTTP/1.1" resp.ProtoMajor = 1 resp.ProtoMinor = 1 resp.Header = p.Headers resp.Body = ioutil.NopCloser(bytes.NewBuffer(p.Buffer.Bytes())) resp.ContentLength = int64(p.Buffer.Len()) if p.Headers.Get("Transfer-Encoding") != "" { resp.TransferEncoding = []string{p.Headers.Get("Transfer-Encoding")} } else { resp.TransferEncoding = nil } resp.Close = p.Headers.Get("Connection") == "close" resp.Trailer = make(http.Header) resp.Request = p.Request b, _ := http.DumpResponse(resp, true) return string(b) }