Ejemplo n.º 1
0
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))
				}
			}
		}
	}
}
Ejemplo n.º 2
0
func (self *HTTPClient) defaultRequest() (req *http.Request, err os.Error) {
	var h http.Header = map[string][]string{}
	req = new(http.Request)
	req.Method = self.method
	if self.contentType != "" {
		headers := map[string]string{"Content-Type": self.contentType}
		for k, v := range headers {
			h.Add(k, v)
		}
	}
	//Request should have a Header, otherwise the
	//transport will not work.
	req.Header = h

	req.ProtoMajor = 1
	req.ProtoMinor = 1
	if self.cookie.Name != "" && self.cookie.Value != "" {
		req.AddCookie(&http.Cookie{Name: self.cookie.Name, Value: self.cookie.Value})
	}

	if req.URL, err = url.Parse(self.addr); err != nil {
		return
	}
	return

}
Ejemplo n.º 3
0
func convert(host string, r *fakeHttpRequest) (*http.Request, os.Error) {
	header := http.Header{}
	header.Add("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1")
	header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
	theurl, err := url.ParseRequest(r.URL)
	if err != nil {
		log.Println("Error in url: ", err)
		return nil, err
	}

	out := &http.Request{
		Method:     r.Method,
		URL:        theurl,
		Proto:      "HTTP/1.1",
		ProtoMajor: 1,
		ProtoMinor: 1,
		Header:     header,
		//		Body: byteReadCloser([]byte(r.Body)),
		ContentLength:    int64(len(r.Body)),
		TransferEncoding: []string{},
		Close:            true,
		Host:             host,
		Form:             nil,
		MultipartForm:    nil,
		Trailer:          nil,
		RemoteAddr:       "fake:req",
		TLS:              nil,
	}
	if len(r.Body) == 0 {
		out.Body = nil
	} else {
		out.Body = byteReadCloser([]byte(r.Body))
	}
	return out, nil
}
Ejemplo n.º 4
0
// 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
}
Ejemplo n.º 5
0
//read the fcgi parameters contained in data, and store them in storage
func readFcgiParams(data []byte, storage http.Header) {
	for idx := 0; len(data) > idx; {
		keySize, shift := readFcgiParamSize(data, idx)
		idx += shift
		valSize, shift := readFcgiParamSize(data, idx)
		idx += shift
		key := data[idx : idx+keySize]
		idx += keySize
		val := data[idx : idx+valSize]
		idx += valSize
		storage.Set(string(key), string(val))
	}
}
Ejemplo n.º 6
0
func setBucketRequest(c Client, name string, props Properties) (req *http.Request, err os.Error) {
	hdrs := http.Header{}
	hdrs.Set("Content-Type", "application/json")

	req = c.request("PUT", c.bucketPath(name), hdrs, nil)

	ob, err := json.Marshal(map[string]interface{}{"props": props})

	if err == nil {
		req.ContentLength = int64(len(ob))
		req.Body = ioutil.NopCloser(bytes.NewBuffer(ob))
	}
	return
}
Ejemplo n.º 7
0
func OutputJSONObject(writer io.Writer, obj jsonhelper.JSONObject, lastModified *time.Time, etag string, statusCode int, headers http.Header) (int, http.Header, os.Error) {
	if statusCode == 0 {
		statusCode = 200
	}
	if headers == nil {
		headers = make(http.Header)
	}
	//headers.Set("Content-Type", wm.MIME_TYPE_JSON)
	if lastModified != nil {
		headers.Set("Last-Modified", lastModified.Format(http.TimeFormat))
	}
	if len(etag) > 0 {
		headers.Set("ETag", etag)
	}
	m := jsonhelper.NewJSONObject()
	w := json.NewEncoder(writer)
	m.Set("status", "success")
	m.Set("result", obj)
	w.Encode(m)
	return statusCode, headers, nil
}
Ejemplo n.º 8
0
func Yadis(url string) (io.Reader, os.Error) {
	fmt.Printf("Search: %s\n", url)
	var headers http.Header
	headers.Add("Accept", "application/xrds+xml")

	r, err := get(url, headers)
	if err != nil || r == nil {
		fmt.Printf("Yadis: Error in GET\n")
		return nil, err
	}

	// If it is an XRDS document, parse it and return URI
	content := r.Header.Get("Content-Type")
	if content != "" && strings.HasPrefix(content, "application/xrds+xml") {
		fmt.Printf("Document XRDS found\n")
		return r.Body, nil
	}

	// If it is an HTML doc search for meta tags
	content = r.Header.Get("Content-Type")
	if content != "" && content == "text/html" {
		fmt.Printf("Document HTML found\n")
		url, err := searchHTMLMeta(r.Body)
		if err != nil {
			return nil, err
		}
		return Yadis(url)
	}

	// If the response contain an X-XRDS-Location header
	xrds := r.Header.Get("X-Xrds-Location")
	if xrds != "" {
		return Yadis(xrds)
	}

	// If nothing is found try to parse it as a XRDS doc
	return nil, nil
}
Ejemplo n.º 9
0
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
}