func (conn *streamConn) connect() (*http.Response, os.Error) { if conn.stale { return nil, os.NewError("Stale connection") } tcpConn, err := net.Dial("tcp", "", conn.url.Host+":80") if err != nil { return nil, err } conn.clientConn = http.NewClientConn(tcpConn, nil) var req http.Request req.URL = conn.url req.Method = "GET" req.Header = map[string]string{} req.Header["Authorization"] = "Basic " + conn.authData if conn.postData != "" { req.Method = "POST" req.Body = nopCloser{bytes.NewBufferString(conn.postData)} req.ContentLength = int64(len(conn.postData)) req.Header["Content-Type"] = "application/x-www-form-urlencoded" } err = conn.clientConn.Write(&req) if err != nil { return nil, err } resp, err := conn.clientConn.Read() if err != nil { return nil, err } return resp, nil }
func getResponse(rawUrl string, req *http.Request) (*http.ClientConn, *http.Response, os.Error) { url, err := url.Parse(rawUrl) if url.Scheme == "" { rawUrl = "http://" + rawUrl url, err = url.Parse(rawUrl) } if err != nil { return nil, nil, err } req.URL = url if debugprint { dump, err := http.DumpRequest(req, true) if err != nil { println(err.String()) } print(string(dump)) } conn, err := newConn(url) if err != nil { println(err.String()) return nil, nil, err } resp, err := conn.Do(req) if err != nil { if err != http.ErrPersistEOF { return nil, nil, err } } return conn, resp, nil }
func Get(url *http.URL) (r *http.Response, err os.Error) { var req http.Request req.URL = url if r, err = send(&req); err != nil { return } return }
func (c *Consumer) httpExecute( method string, url string, body string, oauthParams *OrderedParams) (*http.Response, os.Error) { if c.debug { fmt.Println("httpExecute(method: " + method + ", url: " + url) } var req http.Request req.Method = method req.Header = http.Header{} req.Body = newStringReadCloser(body) parsedurl, err := http.ParseURL(url) if err != nil { return nil, os.NewError("ParseUrl: " + err.String()) } req.URL = parsedurl oauthHdr := "OAuth " for pos, key := range oauthParams.Keys() { if pos > 0 { oauthHdr += "," } oauthHdr += key + "=\"" + oauthParams.Get(key) + "\"" } if c.debug { fmt.Println("AUTH-HDR: " + oauthHdr) } req.Header.Add("Authorization", oauthHdr) resp, err := c.HttpClient.Do(&req) if err != nil { return nil, os.NewError("Do: " + err.String()) } debugHeader := "" for k, vals := range req.Header { for _, val := range vals { debugHeader += "[key: " + k + ", val: " + val + "]" } } if resp.StatusCode != http.StatusOK { bytes, _ := ioutil.ReadAll(resp.Body) resp.Body.Close() return nil, os.NewError("HTTP response is not 200/OK as expected. Actual response: \n" + "\tResponse Status: '" + resp.Status + "'\n" + "\tResponse Code: " + strconv.Itoa(resp.StatusCode) + "\n" + "\tResponse Body: " + string(bytes) + "\n" + "\tRequst Headers: " + debugHeader) } return resp, err }
func nmcPOST(body *bytes.Buffer) (response *http.Response, err os.Error) { client := http.Client{} var req http.Request req.Method = "POST" req.ProtoMajor = 1 req.ProtoMinor = 1 req.Close = true req.Body = ioutil.NopCloser(body) req.Header = http.Header{ "Content-Type": []string{"text/plain"}, "Content-Length": []string{strconv.Itoa(body.Len())}, } req.ContentLength = int64(body.Len()) req.URL = options.nmcURL return client.Do(&req) }
func (conn *streamConn) connect() (*http.Response, os.Error) { if conn.stale { return nil, os.NewError("Stale connection") } var tcpConn net.Conn var err os.Error if proxy := os.Getenv("HTTP_PROXY"); len(proxy) > 0 { proxy_url, _ := url.Parse(proxy) tcpConn, err = net.Dial("tcp", proxy_url.Host) } else { tcpConn, err = net.Dial("tcp", conn.url.Host+":443") } if err != nil { return nil, err } cf := &tls.Config{Rand: rand.Reader, Time: time.Nanoseconds} ssl := tls.Client(tcpConn, cf) conn.clientConn = http.NewClientConn(ssl, nil) var req http.Request req.URL = conn.url req.Method = "GET" req.Header = http.Header{} req.Header.Set("Authorization", "Basic "+conn.authData) if conn.postData != "" { req.Method = "POST" req.Body = nopCloser{bytes.NewBufferString(conn.postData)} req.ContentLength = int64(len(conn.postData)) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") } resp, err := conn.clientConn.Do(&req) if err != nil { return nil, err } return resp, nil }
func main() { url, err := http.ParseURL("http://bbs.golang-china.org/") if err != nil { log.Exit(err) } tcpConn, err := net.Dial("tcp", "", url.Host+":80") if err != nil { log.Exit(err) } clientConn := http.NewClientConn(tcpConn, nil) var req http.Request req.URL = url req.Method = "GET" req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.11 (KHTML, like Gecko) Chrome/9.0.570.0 Safari/534.11" req.Header = map[string]string{} req.Header["Connection"] = "keep-alive" err = clientConn.Write(&req) if err != nil { log.Exit(err) } resp, err := clientConn.Read() if err != nil { log.Exit(err) } defer resp.Body.Close() log.Println("Http Response: " + resp.Status) body, _ := ioutil.ReadAll(resp.Body) log.Println(string(body)) }
// Downloads url and returns whatever result was. // This function WILL NOT follow redirects. func (w *Worker) Download(url *http.URL) (result *FetchResult) { w.cl_lk.Lock() client := w.clients[url.Host] is_new_client := client == nil if client == nil { client = new(Client) client.IOTimeout = w.IOTimeout w.clients[url.Host] = client } w.cl_lk.Unlock() req := new(http.Request) req.URL = url req.Header = make(map[string]string, 10) req.UserAgent = "HeroshiBot/0.3 (+http://temoto.github.com/heroshi/; [email protected])" result = client.FetchWithTimeout(req, w.FetchTimeout) if is_new_client { go w.staleClient(url.Host, w.KeepAlive) } return result }
// RequestFromMap creates an http.Request from CGI variables. // The returned Request's Body field is not populated. func RequestFromMap(params map[string]string) (*http.Request, os.Error) { r := new(http.Request) r.Method = params["REQUEST_METHOD"] if r.Method == "" { return nil, os.NewError("cgi: no REQUEST_METHOD in environment") } r.Proto = params["SERVER_PROTOCOL"] var ok bool r.ProtoMajor, r.ProtoMinor, ok = http.ParseHTTPVersion(r.Proto) if !ok { return nil, os.NewError("cgi: invalid SERVER_PROTOCOL version") } r.Close = true r.Trailer = http.Header{} r.Header = http.Header{} r.Host = params["HTTP_HOST"] r.Referer = params["HTTP_REFERER"] r.UserAgent = params["HTTP_USER_AGENT"] if lenstr := params["CONTENT_LENGTH"]; lenstr != "" { clen, err := strconv.Atoi64(lenstr) if err != nil { return nil, os.NewError("cgi: bad CONTENT_LENGTH in environment: " + lenstr) } r.ContentLength = clen } if ct := params["CONTENT_TYPE"]; ct != "" { r.Header.Set("Content-Type", ct) } // Copy "HTTP_FOO_BAR" variables to "Foo-Bar" Headers for k, v := range params { if !strings.HasPrefix(k, "HTTP_") || skipHeader[k] { continue } r.Header.Add(strings.Replace(k[5:], "_", "-", -1), v) } // TODO: cookies. parsing them isn't exported, though. if r.Host != "" { // Hostname is provided, so we can reasonably construct a URL, // even if we have to assume 'http' for the scheme. r.RawURL = "http://" + r.Host + params["REQUEST_URI"] url, err := http.ParseURL(r.RawURL) if err != nil { return nil, os.NewError("cgi: failed to parse host and REQUEST_URI into a URL: " + r.RawURL) } r.URL = url } // Fallback logic if we don't have a Host header or the URL // failed to parse if r.URL == nil { r.RawURL = params["REQUEST_URI"] url, err := http.ParseURL(r.RawURL) if err != nil { return nil, os.NewError("cgi: failed to parse REQUEST_URI into a URL: " + r.RawURL) } r.URL = url } // There's apparently a de-facto standard for this. // http://docstore.mik.ua/orelly/linux/cgi/ch03_02.htm#ch03-35636 if s := params["HTTPS"]; s == "on" || s == "ON" || s == "1" { r.TLS = &tls.ConnectionState{HandshakeComplete: true} } // Request.RemoteAddr has its port set by Go's standard http // server, so we do here too. We don't have one, though, so we // use a dummy one. r.RemoteAddr = net.JoinHostPort(params["REMOTE_ADDR"], "0") return r, nil }
func requestFromEnvironment(env map[string]string) (*http.Request, os.Error) { r := new(http.Request) r.Method = env["REQUEST_METHOD"] if r.Method == "" { return nil, os.NewError("cgi: no REQUEST_METHOD in environment") } r.Close = true r.Trailer = http.Header{} r.Header = http.Header{} r.Host = env["HTTP_HOST"] r.Referer = env["HTTP_REFERER"] r.UserAgent = env["HTTP_USER_AGENT"] // CGI doesn't allow chunked requests, so these should all be accurate: r.Proto = "HTTP/1.0" r.ProtoMajor = 1 r.ProtoMinor = 0 r.TransferEncoding = nil if lenstr := env["CONTENT_LENGTH"]; lenstr != "" { clen, err := strconv.Atoi64(lenstr) if err != nil { return nil, os.NewError("cgi: bad CONTENT_LENGTH in environment: " + lenstr) } r.ContentLength = clen r.Body = ioutil.NopCloser(io.LimitReader(os.Stdin, clen)) } // Copy "HTTP_FOO_BAR" variables to "Foo-Bar" Headers for k, v := range env { if !strings.HasPrefix(k, "HTTP_") || skipHeader[k] { continue } r.Header.Add(strings.Replace(k[5:], "_", "-", -1), v) } // TODO: cookies. parsing them isn't exported, though. if r.Host != "" { // Hostname is provided, so we can reasonably construct a URL, // even if we have to assume 'http' for the scheme. r.RawURL = "http://" + r.Host + env["REQUEST_URI"] url, err := http.ParseURL(r.RawURL) if err != nil { return nil, os.NewError("cgi: failed to parse host and REQUEST_URI into a URL: " + r.RawURL) } r.URL = url } // Fallback logic if we don't have a Host header or the URL // failed to parse if r.URL == nil { r.RawURL = env["REQUEST_URI"] url, err := http.ParseURL(r.RawURL) if err != nil { return nil, os.NewError("cgi: failed to parse REQUEST_URI into a URL: " + r.RawURL) } r.URL = url } return r, nil }