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 dial(host string) (conn *http.ClientConn) { var tcp net.Conn var err os.Error fmt.Fprintf(os.Stderr, "http-gonsole: establishing a TCP connection ...\n") proxy := os.Getenv("HTTP_PROXY") if len(proxy) > 0 { proxy_url, _ := http.ParseURL(proxy) tcp, err = net.Dial("tcp", proxy_url.Host) } else { tcp, err = net.Dial("tcp", host) } if err != nil { fmt.Fprintln(os.Stderr, "http-gonsole:", err) os.Exit(1) } if *useSSL { cf := &tls.Config{Rand: rand.Reader, Time: time.Nanoseconds} ssl := tls.Client(tcp, cf) conn = http.NewClientConn(ssl, nil) if len(proxy) > 0 { tcp.Write([]byte("CONNECT " + host + " HTTP/1.0\r\n\r\n")) b := make([]byte, 1024) tcp.Read(b) } } else { conn = http.NewClientConn(tcp, nil) } return }
// Returns a new HTTP connection; As with NewConn, netname is not // checked, and the behaviour of an HTTP client over a non stream // protocol is undefined - but probably interesting to watch. func (self *Endpoint) NewHTTPClientConn(netname, local string, r *bufio.Reader) (hc *http.ClientConn, err os.Error) { rawc, err := self.NewConn(netname, local) if err == nil { hc = http.NewClientConn(rawc, r) } return }
func ProxyServer(w http.ResponseWriter, req *http.Request) { log.Printf("%#v", req) addr, err := net.ResolveIPAddr(req.URL.Host) if err != nil { io.WriteString(w, err.String()) return } conn, err := net.DialTCP("tcp4", nil, &net.TCPAddr{IP: addr.IP, Port: 80}) if err != nil { io.WriteString(w, err.String()) return } defer conn.Close() client := http.NewClientConn(conn, nil) err = client.Write(req) if err != nil { io.WriteString(w, err.String()) return } resp, err := client.Read() if err != nil { io.WriteString(w, err.String()) return } for k, v := range resp.Header { w.SetHeader(k, v) } io.Copy(w, resp.Body) w.Flush() }
func dialHTTP(hoststring string, scheme string) (cc *http.ClientConn, err os.Error) { host, port, err := net.SplitHostPort(hoststring) if err != nil { return } if port == "" { switch scheme { case "http": port = "80" case "https": port = "80" case "riak": port = "8098" default: err = os.NewError("Unknown scheme") } } if err != nil { return } var c net.Conn switch scheme { case "https": c, err = tls.Dial("tcp", host+":"+port, nil) default: c, err = net.Dial("tcp", host+":"+port) } if err == nil { cc = http.NewClientConn(c, nil) } return }
// make a GET request with a fake user agent // this is definitely not for those undocumented Google APIs func getUserAgent(urlstr string) ([]byte, os.Error) { urlobj, err := url.Parse(urlstr) if err != nil { return nil, err } conn, err := net.Dial("tcp", urlobj.Host+":http") if err != nil { return nil, err } req, err := http.NewRequest("GET", urlstr, nil) if err != nil { return nil, err } req.Header.Set("User-Agent", "Mozilla/5.0") httpconn := http.NewClientConn(conn, nil) response, err := httpconn.Do(req) if err != nil { return nil, err } defer response.Body.Close() b, err := ioutil.ReadAll(response.Body) return b, err }
func newConn(url *url.URL) (*http.ClientConn, os.Error) { addr := url.Host //just set the default scheme to http if url.Scheme == "" { url.Scheme = "http" } if !hasPort(addr) { addr += ":" + url.Scheme } var conn net.Conn var err os.Error if url.Scheme == "http" { conn, err = net.Dial("tcp", addr) if err != nil { return nil, err } } else { // https conn, err = tls.Dial("tcp", addr, nil) if err != nil { return nil, err } h := url.Host if hasPort(h) { h = h[0:strings.LastIndex(h, ":")] } if err := conn.(*tls.Conn).VerifyHostname(h); err != nil { return nil, err } } return http.NewClientConn(conn, nil), nil }
func ProcessRequest(req *http.Request, address string, port string) (map[string]string, int, os.Error) { // Request is all set. Let's Make connection. results := map[string]string{"X-Riak-Vclock": "", "Body": ""} conn, err := net.Dial("tcp", "", fmt.Sprintf("%s:%s", address, port)) if err != nil { return results, 500, err } http_conn := http.NewClientConn(conn, nil) defer http_conn.Close() if err := http_conn.Write(req); err != nil { return results, 500, err } //Request sent. Time to get the response. resp, err := http_conn.Read() if err != nil { return results, 500, err } bytes, _ := ioutil.ReadAll(resp.Body) resp.Body.Close() results["X-Riak-Vclock"] = resp.Header["X-Riak-Vclock"] results["Body"] = string(bytes) return results, resp.StatusCode, nil }
func (self *Conn) dial() (err os.Error) { if self.c == nil { // Get the underlying connection (or redial) err = self.uc.Dial() if err == nil { self.c = http.NewClientConn(self.uc, nil) } } return }
func dial(addr string) (*http.ClientConn, os.Error) { if !hasPort(addr) { addr += ":http" } sock, err := net.Dial("tcp", "", addr) if err != nil { return nil, err } return http.NewClientConn(sock, nil), nil }
func oauthGet(accessToken string, urlString string) (*http.Response, os.Error) { url, _ := http.ParseURL(urlString) conn, _ := net.Dial("tcp", url.Host+":80") clientConn := http.NewClientConn(conn, nil) header := map[string][]string{"Authorization": {"OAuth " + accessToken}} request := http.Request{Method: "GET", URL: url, Header: header} clientConn.Write(&request) return clientConn.Read(&request) }
func HttpGet(url string) (*http.Response, os.Error) { var r *http.Response var err os.Error if proxy := os.Getenv("HTTP_PROXY"); len(proxy) > 0 { proxy_url, _ := http.ParseURL(proxy) tcp, _ := net.Dial("tcp", "", proxy_url.Host) conn := http.NewClientConn(tcp, nil) var req http.Request req.URL, _ = http.ParseURL(url) req.Method = "GET" err = conn.Write(&req) r, err = conn.Read() } else { r, _, err = http.Get(url) } return r, err }
// This function is launched async as a go routine. It tries to send a message // to a remote server and sends a bool to nextChannel to indicate whether this // has succeeded or not. It is not allowed to run this function multiple times in parallel // for the same FederationGateway. func (self *FederationGateway) sendFromQueue(req *WaveletUpdateRequest) { // No HTTP connection open yet? if self.connection == nil { con, err := net.Dial("tcp", "", fmt.Sprintf("%v:%v", self.manifest.HostName, self.manifest.Port)) if err != nil { // TODO: Good error handling log.Println("Failed connecting to ", self.manifest, err) self.nextChannel <- false return } self.connection = http.NewClientConn(con, nil) } // Build the HTTP request var hreq http.Request hreq.Host = self.manifest.Domain hreq.Header = make(map[string]string) hreq.RawURL = fmt.Sprintf("http://%v:%v/fed%v", self.manifest.HostName, self.manifest.Port, req.uri) hreq.Method = "PUT" hreq.Body = NewRequestBody(req.Data) hreq.ContentLength = int64(len(req.Data)) hreq.Header["Content-Type"] = req.MimeType log.Println("Sending WaveletUpdate to remote server ", hreq.RawURL) // Send the HTTP request self.connection.Write(&hreq) // Read the HTTP response hres, err := self.connection.Read() if err != nil { log.Println("Error reading HTTP response from ", self.manifest, err) // TODO: Better error handling self.connection.Close() self.connection = nil self.nextChannel <- false return } log.Println("After sending WaveletUpdate, status code is ", hres.StatusCode) // Success in sending the wavelet update? if hres.StatusCode == 200 { self.nextChannel <- true return } // Sending the wavelet update failed self.nextChannel <- false }
// Creates a client for the specified resource. // // The resource is the url to the base of the resource (i.e., // http://127.0.0.1:3000/snips/) func NewClient(resource string) (*Client, os.Error) { var client = new(Client) var err os.Error // setup host if client.resource, err = http.ParseURL(resource); err != nil { return nil, err } // Setup conn var tcpConn net.Conn if tcpConn, err = net.Dial("tcp", client.resource.Host); err != nil { return nil, err } client.conn = http.NewClientConn(tcpConn, nil) return client, nil }
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 GetManifest(host string, port uint16) *ServerManifest { log.Println("Discovery at ", host, port) // Make a TCP connection con, err := net.Dial("tcp", "", fmt.Sprintf("%v:%v", host, port)) if err != nil { log.Println("Failed connecting to ", host, port, err) return nil } // Make a HTTP connection hcon := http.NewClientConn(con, nil) // Build the HTTP request var hreq http.Request hreq.Host = host hreq.Method = "GET" hreq.RawURL = fmt.Sprintf("http://%v:%v/_manifest", host, port) log.Println("Sending request") // Send the HTTP request if err = hcon.Write(&hreq); err != nil { log.Println("Error sending GET request") hcon.Close() return nil } // Read the HTTP response _, err = hcon.Read() if err != nil { log.Println("Error reading HTTP response from ", host, err) hcon.Close() return nil } m := &ServerManifest{} // TODO: Parse the manifest // HACK START m.HostName = host m.Port = port // HACK END return m }
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)) }
func (client *Client) Connect(addr string) (err os.Error) { if !hasPort(addr) { addr += ":http" } var tcp_addr *net.TCPAddr tcp_addr, err = net.ResolveTCPAddr(addr) if err != nil { return err } client.NetConn, err = net.DialTCP("tcp", nil, tcp_addr) if err != nil { return err } client.NetConn.SetKeepAlive(true) client.NetConn.SetLinger(0) // NoDelay is true by default, that's what we need client.NetConn.SetTimeout(int64(client.IOTimeout)) client.HttpConn = http.NewClientConn(client.NetConn, nil) return nil }
func WriteResponse(c *http.Conn, req *http.Request) { PrintRequest(req) req = DeleteHopByHopHeader(req) PrintRequest(req) host := req.URL.Host if match, _ := regexp.MatchString(":[0-9]+$", host); !match { if req.URL.Scheme == "http" { host = host + ":80" } else if req.URL.Scheme == "https" { host = host + ":443" } } proxy := os.Getenv("HTTP_PROXY") if len(proxy) > 0 { proxy_url, _ := http.ParseURL(proxy) host = proxy_url.Host } log.Stdoutf("host:%s\n", host) tcp, err := net.Dial("tcp", "", host) if err != nil { log.Stdout(err) return } defer tcp.Close() conn := http.NewClientConn(tcp, nil) defer conn.Close() err = conn.Write(req) if err != nil { log.Stdout(err) return } response, err := conn.Read() if err != nil { log.Stdout(err) return } PrintResponse(response) /*** size := response.ContentLength if size > 0 { buf := make([]byte, size) io.ReadFull(response.Body, buf) c.Write(buf) } else if response.RequestMethod == "HEAD" { buf, _ := ioutil.ReadAll(response.Body) c.Write(buf) } ***/ err = response.Write(c) if err != nil { log.Stdout("ERROR:") log.Stdout(err) return } err = response.Body.Close() if err != nil { log.Stdout("ERROR:") log.Stdout(err) } }
func (self *FederationProxy) sendFromQueue() { if len(self.queue) == 0 { return } log.Println("Sending message from queue") // No HTTP connection open yet? if self.connection == nil { con, err := net.Dial("tcp", "", fmt.Sprintf("%v:%v", self.manifest.HostName, self.manifest.Port)) if err != nil { // TODO: Good error handling log.Println("Failed connecting to ", self.manifest, err) return } self.connection = http.NewClientConn(con, nil) } // Dequeue a message req := self.queue.At(0).(FederationRequest) // Build the HTTP request var hreq http.Request hreq.Host = self.manifest.Domain hreq.Header = make(map[string]string) switch req.(type) { case *PostRequest: preq := req.(*PostRequest) hreq.RawURL = fmt.Sprintf("http://%v:%v/fed/%v", self.manifest.HostName, self.manifest.Port, preq.URI.String()) hreq.Method = "POST" hreq.Body = NewRequestBody(preq.Data) hreq.ContentLength = int64(len(preq.Data)) hreq.Header["Content-Type"] = preq.MimeType case *GetRequest: greq := req.(*GetRequest) hreq.Method = "GET" hreq.RawURL = fmt.Sprintf("http://%v:%v/fed/%v", self.manifest.HostName, self.manifest.Port, greq.URI.String()) default: log.Println(req) panic("Unsupported kind of message forwarded internally to the federation proxy") } log.Println("Sending request to ", hreq.RawURL) // Send the HTTP request self.connection.Write(&hreq) // Read the HTTP response hres, err := self.connection.Read() if err != nil { log.Println("Error reading HTTP response from ", self.manifest, err) // TODO: Better error handling self.connection.Close() self.connection = nil return } // Success. Remove the request from the queue self.queue.Delete(0) // Send the result back to the client _, err = io.Copy(req.GetResponseWriter(), hres.Body) hres.Body.Close() if err != nil { log.Println("Error sending result of federated message back to the client") req.SendFinishSignal(false) } req.SendFinishSignal(true) }