Esempio n. 1
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

}
Esempio n. 2
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
}
Esempio n. 3
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
}