本文整理汇总了Golang中net/http.ReadRequest函数的典型用法代码示例。如果您正苦于以下问题:Golang ReadRequest函数的具体用法?Golang ReadRequest怎么用?Golang ReadRequest使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ReadRequest函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: readRequest
// Read next request from connection.
func (c *conn) readRequest() (r *request, w *response, err error) {
if c.hijacked {
return nil, nil, ErrHijacked
}
c.lr.N = int64(c.server.maxHeaderBytes()) + 4096 /* bufio slop */
var req *http.Request
if req, err = http.ReadRequest(c.buf.Reader); err != nil {
if c.lr.N == 0 {
return nil, nil, errTooLarge
}
return nil, nil, err
}
c.lr.N = noLimit
req.RemoteAddr = c.remoteAddr
w = new(response)
w.conn = c
r = new(request)
r.Request = req
r.w = w
w.reqWantsHttp10KeepAlive = r.wantsHttp10KeepAlive()
w.reqMethod = r.Method
w.reqProtoAtLeast10 = r.ProtoAtLeast(1, 0)
w.reqProtoAtLeast11 = r.ProtoAtLeast(1, 1)
w.reqExpectsContinue = r.expectsContinue()
w.reqContentLength = r.ContentLength
w.header = make(http.Header)
w.contentLength = -1
return r, w, nil
}
示例2: newRequest
// newRequest is a helper function to create a new request with a method and url.
// The request returned is a 'server' request as opposed to a 'client' one through
// simulated write onto the wire and read off of the wire.
// The differences between requests are detailed in the net/http package.
func newRequest(method, url string) *http.Request {
req, err := http.NewRequest(method, url, nil)
if err != nil {
panic(err)
}
// extract the escaped original host+path from url
// http://localhost/path/here?v=1#frag -> //localhost/path/here
opaque := ""
if i := len(req.URL.Scheme); i > 0 {
opaque = url[i+1:]
}
if i := strings.LastIndex(opaque, "?"); i > -1 {
opaque = opaque[:i]
}
if i := strings.LastIndex(opaque, "#"); i > -1 {
opaque = opaque[:i]
}
// Escaped host+path workaround as detailed in https://golang.org/pkg/net/url/#URL
// for < 1.5 client side workaround
req.URL.Opaque = opaque
// Simulate writing to wire
var buff bytes.Buffer
req.Write(&buff)
ioreader := bufio.NewReader(&buff)
// Parse request off of 'wire'
req, err = http.ReadRequest(ioreader)
if err != nil {
panic(err)
}
return req
}
示例3: req
func req(t *testing.T, v string) *http.Request {
req, err := http.ReadRequest(bufio.NewReader(strings.NewReader(v)))
if err != nil {
t.Fatal(err)
}
return req
}
示例4: BenchmarkSignatureSign
func BenchmarkSignatureSign(b *testing.B) {
b.StopTimer()
secret := "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"
access := "AKIDEXAMPLE"
signature := NewSignature(secret, access, USEast, "service")
b.StartTimer()
for i := 0; i < b.N; i++ {
b.StopTimer()
rawRequest := []byte(`POST / HTTP/1.1
Content-Type:application/x-www-form-urlencoded
Date:Mon, 09 Sep 2011 23:36:00 GMT
Host:host.foo.com
foo=bar`)
reader := bufio.NewReader(bytes.NewBuffer(rawRequest))
request, err := http.ReadRequest(reader)
if err != nil {
b.Fatal(err)
}
delete(request.Header, "User-Agent")
var body *bytes.Reader
if i := bytes.Index(rawRequest, []byte("\n\n")); i != -1 {
body = bytes.NewReader(rawRequest[i+2:])
request.Body = ioutil.NopCloser(body)
}
b.StartTimer()
_ = signature.Sign(request, body, nil)
}
}
示例5: handleConnection
func (this *HttpProxyServer) handleConnection(conn *hub.TCPConn) {
defer conn.Close()
reader := bufio.NewReader(conn)
request, err := http.ReadRequest(reader)
if err != nil {
log.Warning("Failed to read http request: ", err)
return
}
log.Info("Request to Method [", request.Method, "] Host [", request.Host, "] with URL [", request.URL, "]")
defaultPort := v2net.Port(80)
if strings.ToLower(request.URL.Scheme) == "https" {
defaultPort = v2net.Port(443)
}
host := request.Host
if len(host) == 0 {
host = request.URL.Host
}
dest, err := parseHost(host, defaultPort)
if err != nil {
log.Warning("Malformed proxy host (", host, "): ", err)
return
}
if strings.ToUpper(request.Method) == "CONNECT" {
this.handleConnect(request, dest, reader, conn)
} else {
this.handlePlainHTTP(request, dest, reader, conn)
}
}
示例6: TestHybiServerHandshakeHybiBadVersion
func TestHybiServerHandshakeHybiBadVersion(t *testing.T) {
config := new(Config)
handshaker := &hybiServerHandshaker{Config: config}
br := bufio.NewReader(strings.NewReader(`GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Origin: http://example.com
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 9
`))
req, err := http.ReadRequest(br)
if err != nil {
t.Fatal("request", err)
}
code, err := handshaker.ReadHandshake(br, req)
if err != ErrBadWebSocketVersion {
t.Errorf("handshake expected err %q but got %q", ErrBadWebSocketVersion, err)
}
if code != http.StatusBadRequest {
t.Errorf("status expected %q but got %q", http.StatusBadRequest, code)
}
}
示例7: Serve
// Serve parse data and then send to mika server.
func (h *Relay) Serve() {
bf := bufio.NewReader(h.conn)
req, err := http.ReadRequest(bf)
if err != nil {
utils.Errorf("Read request error %s", err)
return
}
// TODO Set http protocol flag
mikaConn, err := mika.DailWithRawAddrHTTP("tcp", h.ssServer, utils.ToAddr(req.URL.Host), h.cipher)
if err != nil {
return
}
defer func() {
if !h.closed {
err := mikaConn.Close()
utils.Errorf("Close connection error %v\n", err)
}
}()
if req.Method == "CONNECT" {
_HTTPSHandler(h.conn)
} else {
_HTTPHandler(mikaConn, req)
}
go protocols.Pipe(h.conn, mikaConn)
protocols.Pipe(mikaConn, h.conn)
h.closed = true
}
示例8: handleConnection
// handleConnection is spawned once per connection from a client, and exits when the client is
// done sending requests.
func handleConnection(conn net.Conn) {
defer conn.Close()
reader := bufio.NewReader(conn)
for {
req, err := http.ReadRequest(reader)
if err != nil {
if err != io.EOF {
log.Printf("Failed to read request: %s", err)
}
return
}
// Connect to a backend and send the request along.
if be, err := net.Dial("tcp", "127.0.0.1:8081"); err == nil {
be_reader := bufio.NewReader(be)
if err := req.Write(be); err == nil {
if resp, err := http.ReadResponse(be_reader, req); err == nil {
FixHttp10Response(resp, req)
if err := resp.Write(conn); err == nil {
log.Printf("proxied %s: got %d", req.URL.Path, resp.StatusCode)
}
if resp.Close {
return
}
}
}
}
}
}
示例9: ServeConn
// ServeConn copies a response from c for every of the rw coonection's request.
func (srv *Server) ServeConn(rw net.Conn, c []Connection) {
var (
n int64
err error
req *http.Request
r = bufio.NewReader(rw)
rem = tcpaddrnil(rw.RemoteAddr())
)
for i := 0; ; i++ {
if req, err = http.ReadRequest(r); err != nil {
break
}
n, err = io.Copy(ioutil.Discard, req.Body)
req.Body.Close()
if i >= len(c) {
write500(rw, errNoResponse)
srv.Reply(rem, srv.src, n, errNoResponse)
continue
}
srv.Reply(rem, srv.src, n, err)
if err != nil {
write500(rw, err)
continue
}
if c[i].Res != nil {
_, err = io.Copy(rw, bytes.NewBuffer(c[i].Res))
srv.Reply(srv.src, rem, int64(len(c[i].Res)), err)
}
}
if err != nil && err != io.EOF {
srv.Reply(rem, srv.src, 0, err)
}
rw.Close()
srv.wg.Done()
}
示例10: TestHopByHopHeadersStrip
func TestHopByHopHeadersStrip(t *testing.T) {
assert := assert.On(t)
rawRequest := `GET /pkg/net/http/ HTTP/1.1
Host: golang.org
Connection: keep-alive,Foo, Bar
Foo: foo
Bar: bar
Proxy-Connection: keep-alive
Proxy-Authenticate: abc
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; de-de) AppleWebKit/523.10.3 (KHTML, like Gecko) Version/3.0.4 Safari/523.10
Accept-Encoding: gzip
Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7
Cache-Control: no-cache
Accept-Language: de,en;q=0.7,en-us;q=0.3
`
b := bufio.NewReader(strings.NewReader(rawRequest))
req, err := http.ReadRequest(b)
assert.Error(err).IsNil()
assert.String(req.Header.Get("Foo")).Equals("foo")
assert.String(req.Header.Get("Bar")).Equals("bar")
assert.String(req.Header.Get("Connection")).Equals("keep-alive,Foo, Bar")
assert.String(req.Header.Get("Proxy-Connection")).Equals("keep-alive")
assert.String(req.Header.Get("Proxy-Authenticate")).Equals("abc")
StripHopByHopHeaders(req)
assert.String(req.Header.Get("Connection")).Equals("close")
assert.String(req.Header.Get("Foo")).Equals("")
assert.String(req.Header.Get("Bar")).Equals("")
assert.String(req.Header.Get("Proxy-Connection")).Equals("")
assert.String(req.Header.Get("Proxy-Authenticate")).Equals("")
}
示例11: getAWSSuiteReq
func getAWSSuiteReq(reqFileName string) (*sign4.ReusableRequest, error) {
readBytes, err := ioutil.ReadFile(reqFileName)
//fmt.Printf("Read: \n%v\n", string(readBytes))
if err != nil {
return nil, err
}
// fix lowercase http, which causes issues in the parser
if reqStr := string(readBytes); strings.Contains(reqStr, "http/1.1") {
readBytes = []byte(strings.Replace(reqStr, "http/1.1", "HTTP/1.1", 1))
}
buff := new(bytes.Buffer)
_, err = buff.Write(readBytes)
if err != nil {
return nil, err
}
hreq, err := http.ReadRequest(bufio.NewReader(buff))
if err != nil {
return nil, err
}
req, err := sign4.NewReusableRequestFromRequest(hreq)
if err != nil {
return nil, err
}
// add a blank user agent if one doesn't exist
_, ok := req.Header["User-Agent"]
if !ok {
req.Header.Set("User-Agent", "")
}
return req, nil
}
示例12: req
func req(s string) *http.Request {
req, err := http.ReadRequest(bufio.NewReader(strings.NewReader(s)))
if err != nil {
panic(fmt.Sprintf("bad request in test: %q (error: %v)", req, err))
}
return req
}
示例13: handleConnection
func (this *Server) handleConnection(conn internet.Connection) {
defer conn.Close()
timedReader := v2net.NewTimeOutReader(this.config.Timeout, conn)
reader := bufio.NewReaderSize(timedReader, 2048)
request, err := http.ReadRequest(reader)
if err != nil {
if err != io.EOF {
log.Warning("HTTP: Failed to read http request: ", err)
}
return
}
log.Info("HTTP: Request to Method [", request.Method, "] Host [", request.Host, "] with URL [", request.URL, "]")
defaultPort := v2net.Port(80)
if strings.ToLower(request.URL.Scheme) == "https" {
defaultPort = v2net.Port(443)
}
host := request.Host
if len(host) == 0 {
host = request.URL.Host
}
dest, err := parseHost(host, defaultPort)
if err != nil {
log.Warning("HTTP: Malformed proxy host (", host, "): ", err)
return
}
log.Access(conn.RemoteAddr(), request.URL, log.AccessAccepted, "")
if strings.ToUpper(request.Method) == "CONNECT" {
this.handleConnect(request, dest, reader, conn)
} else {
this.handlePlainHTTP(request, dest, reader, conn)
}
}
示例14: MustRequestFromString
func MustRequestFromString(reqString string) (req *http.Request) {
req, err := http.ReadRequest(bufio.NewReader(bytes.NewReader([]byte(reqString))))
if err != nil {
panic(err)
}
return req
}
示例15: handleConnection
func handleConnection(conn net.Conn) {
defer conn.Close()
reader := bufio.NewReader(conn)
for {
req, err := http.ReadRequest(reader)
if err != nil {
if err != io.EOF {
log.Printf("Failed to read request: %s", err)
}
return
}
be, err := getBackend()
if err != nil {
return
}
if err := req.Write(be.Writer); err == nil {
be.Writer.Flush()
if resp, err := http.ReadResponse(be.Reader, req); err == nil {
bytes := updateStats(req, resp)
resp.Header.Set("X-Bytes", strconv.FormatInt(bytes, 10))
FixHttp10Response(resp, req)
if err := resp.Write(conn); err == nil {
log.Printf("proxied %s: got %d", req.URL.Path, resp.StatusCode)
bytes, _ := httputil.DumpRequest(req, false)
log.Printf("Dump header: %s", string(bytes))
}
if resp.Close {
return
}
}
}
go queueBackend(be)
}
}