func TestWriteHeader(t *testing.T) { for level := zlib.NoCompression; level <= zlib.BestCompression; level++ { r := NewHeaderReader() w := NewHeaderWriter(level) for i := 0; i < 100; i++ { b := new(bytes.Buffer) gold := http.Header{ "Url": []string{"http://www.google.com/"}, "Method": []string{"get"}, "Version": []string{"http/1.1"}, } w.WriteHeader(b, gold) h, err := r.Decode(b.Bytes()) if err != nil { t.Errorf("(level=%d i=%d) Error: %v", level, i, err) return } if len(h) != len(gold) { t.Errorf("(level=%d i=%d) Header count = %d (expected %d)", level, i, len(h), len(gold)) } for k, _ := range h { if h.Get(k) != gold.Get(k) { t.Errorf("(level=%d i=%d) %s: %q != %q", level, i, k, h.Get(k), gold.Get(k)) } } } } }
// for hdrs, only include headers listed as optional from 'http://wiki.basho.com/HTTP-Fetch-Object.html' func getMultiItemRequest(c Client, bucket, key string, hdrs http.Header, parms http.Values) (req *http.Request) { if hdrs == nil { hdrs = http.Header{} } if hdrs.Get("Accept") == "" { hdrs.Set("Accept", "multipart/mixed") } req = c.request("GET", c.keyPath(bucket, key), hdrs, parms) return }
func newRequestCgi(headers http.Header, body io.Reader) *Request { var httpheader = make(http.Header) for header, value := range headers { if strings.HasPrefix(header, "Http_") { newHeader := header[5:] newHeader = strings.Replace(newHeader, "_", "-", -1) newHeader = http.CanonicalHeaderKey(newHeader) httpheader[newHeader] = value } } host := httpheader.Get("Host") method := headers.Get("REQUEST_METHOD") path := headers.Get("REQUEST_URI") port := headers.Get("SERVER_PORT") proto := headers.Get("SERVER_PROTOCOL") rawurl := "http://" + host + ":" + port + path url_, _ := url.Parse(rawurl) useragent := headers.Get("USER_AGENT") remoteAddr := headers.Get("REMOTE_ADDR") remotePort, _ := strconv.Atoi(headers.Get("REMOTE_PORT")) if method == "POST" { if ctype, ok := headers["CONTENT_TYPE"]; ok { httpheader["Content-Type"] = ctype } if clength, ok := headers["CONTENT_LENGTH"]; ok { httpheader["Content-Length"] = clength } } //read the cookies cookies := readCookies(httpheader) req := Request{ Method: method, RawURL: rawurl, URL: url_, Proto: proto, Host: host, UserAgent: useragent, Body: body, Headers: httpheader, RemoteAddr: remoteAddr, RemotePort: remotePort, Cookie: cookies, } return &req }