本文整理汇总了Golang中net/http/httputil.DumpRequest函数的典型用法代码示例。如果您正苦于以下问题:Golang DumpRequest函数的具体用法?Golang DumpRequest怎么用?Golang DumpRequest使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了DumpRequest函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: AddDebugHandlers
func (s *Service) AddDebugHandlers() {
out := s.Config.Logger
if s.Config.LogLevel == 0 {
return
}
s.Handlers.Sign.PushBack(func(r *Request) {
dumpedBody, _ := httputil.DumpRequest(r.HTTPRequest, true)
fmt.Fprintf(out, "=> [%s] %s.%s(%+v)\n", r.Time,
r.Service.ServiceName, r.Operation.Name, r.Params)
fmt.Fprintf(out, "---[ REQUEST PRE-SIGN ]------------------------------\n")
fmt.Fprintf(out, "%s\n", string(dumpedBody))
fmt.Fprintf(out, "-----------------------------------------------------\n")
})
s.Handlers.Send.PushFront(func(r *Request) {
dumpedBody, _ := httputil.DumpRequest(r.HTTPRequest, true)
fmt.Fprintf(out, "---[ REQUEST POST-SIGN ]-----------------------------\n")
fmt.Fprintf(out, "%s\n", string(dumpedBody))
fmt.Fprintf(out, "-----------------------------------------------------\n")
})
s.Handlers.Send.PushBack(func(r *Request) {
fmt.Fprintf(out, "---[ RESPONSE ]--------------------------------------\n")
if r.HTTPResponse != nil {
dumpedBody, _ := httputil.DumpResponse(r.HTTPResponse, true)
fmt.Fprintf(out, "%s\n", string(dumpedBody))
} else if r.Error != nil {
fmt.Fprintf(out, "%s\n", r.Error)
}
fmt.Fprintf(out, "-----------------------------------------------------\n")
})
}
示例2: CmaMiddleware
// CmaMiddleware is a middleware to cma
func CmaMiddleware(h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" && r.URL.Path == "/ping/cma/create" {
dumpHead, _ := httputil.DumpRequest(r, false)
dumpBody, _ := httputil.DumpRequest(r, true)
dumpBody = dumpBody[len(dumpHead):]
cma := models.CmaJSON{}
if err := json.NewDecoder(bytes.NewReader(dumpBody)).Decode(&cma); err != nil {
log.Println(err)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
return
}
if cma.Identifier != "publicacao" {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadGateway)
return
}
}
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
示例3: Apply
func (f *forwarder) Apply(w http.ResponseWriter, req *http.Request, next filters.Next) error {
op := ops.Begin("proxy_http")
defer op.End()
// Create a copy of the request suitable for our needs
reqClone, err := f.cloneRequest(req, req.URL)
if err != nil {
return op.FailIf(filters.Fail("Error forwarding from %v to %v: %v", req.RemoteAddr, req.Host, err))
}
f.Rewriter.Rewrite(reqClone)
if log.IsTraceEnabled() {
reqStr, _ := httputil.DumpRequest(req, false)
log.Tracef("Forwarder Middleware received request:\n%s", reqStr)
reqStr2, _ := httputil.DumpRequest(reqClone, false)
log.Tracef("Forwarder Middleware forwarding rewritten request:\n%s", reqStr2)
}
// Forward the request and get a response
start := time.Now().UTC()
response, err := f.RoundTripper.RoundTrip(reqClone)
if err != nil {
return op.FailIf(filters.Fail("Error forwarding from %v to %v: %v", req.RemoteAddr, req.Host, err))
}
log.Debugf("Round trip: %v, code: %v, duration: %v",
reqClone.URL, response.StatusCode, time.Now().UTC().Sub(start))
if log.IsTraceEnabled() {
respStr, _ := httputil.DumpResponse(response, true)
log.Tracef("Forward Middleware received response:\n%s", respStr)
}
// Forward the response to the origin
copyHeadersForForwarding(w.Header(), response.Header)
w.WriteHeader(response.StatusCode)
// It became nil in a Co-Advisor test though the doc says it will never be nil
if response.Body != nil {
buf := buffers.Get()
defer buffers.Put(buf)
_, err = io.CopyBuffer(w, response.Body, buf)
if err != nil {
log.Debug(err)
}
response.Body.Close()
}
return filters.Stop()
}
示例4: ServeHTTP
func (f *Forwarder) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// Create a copy of the request suitable for our needs
reqClone, err := f.cloneRequest(req, req.URL)
if err != nil {
log.Errorf("Error forwarding to %v, error: %v", req.Host, err)
f.errHandler.ServeHTTP(w, req, err)
return
}
f.rewriter.Rewrite(reqClone)
if log.IsTraceEnabled() {
reqStr, _ := httputil.DumpRequest(req, false)
log.Tracef("Forwarder Middleware received request:\n%s", reqStr)
reqStr2, _ := httputil.DumpRequest(reqClone, false)
log.Tracef("Forwarder Middleware forwarding rewritten request:\n%s", reqStr2)
}
// Forward the request and get a response
start := time.Now().UTC()
response, err := f.roundTripper.RoundTrip(reqClone)
if err != nil {
log.Debugf("Error forwarding to %v, error: %v", req.Host, err)
f.errHandler.ServeHTTP(w, req, err)
return
}
log.Debugf("Round trip: %v, code: %v, duration: %v",
req.URL, response.StatusCode, time.Now().UTC().Sub(start))
if log.IsTraceEnabled() {
respStr, _ := httputil.DumpResponse(response, true)
log.Tracef("Forward Middleware received response:\n%s", respStr)
}
// Forward the response to the origin
copyHeadersForForwarding(w.Header(), response.Header)
w.WriteHeader(response.StatusCode)
// It became nil in a Co-Advisor test though the doc says it will never be nil
if response.Body != nil {
_, err = io.Copy(w, response.Body)
if err != nil {
log.Debug(err)
}
response.Body.Close()
}
}
示例5: readRequests
func (h *Http) readRequests(tee *conn.Tee, lastTxn chan *HttpTxn, connCtx interface{}) {
defer close(lastTxn)
for {
req, err := http.ReadRequest(tee.WriteBuffer())
if err != nil {
// no more requests to be read, we're done
break
}
// make sure we read the body of the request so that
// we don't block the writer
_, err = httputil.DumpRequest(req, true)
h.reqMeter.Mark(1)
if err != nil {
tee.Warn("Failed to extract request body: %v", err)
}
// golang's ReadRequest/DumpRequestOut is broken. Fix up the request so it works later
req.URL.Scheme = "http"
req.URL.Host = req.Host
txn := &HttpTxn{Start: time.Now(), ConnUserCtx: connCtx}
txn.Req = &HttpRequest{Request: req}
txn.Req.BodyBytes, txn.Req.Body, err = extractBody(req.Body)
lastTxn <- txn
h.Txns.In() <- txn
}
}
示例6: loggingMiddleware
func loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rb, _ := httputil.DumpRequest(r, false)
log.Println(string(rb))
next.ServeHTTP(w, r)
})
}
示例7: PrintDump
// PrintDump prints dump of request, optionally writing it in the response
func PrintDump(w http.ResponseWriter, r *http.Request, write bool) {
dump, _ := httputil.DumpRequest(r, true)
log.Printf("%v", string(dump))
if write == true {
w.Write(dump)
}
}
示例8: handler
func (i *HTTPInput) handler(w http.ResponseWriter, r *http.Request) {
buf, _ := httputil.DumpRequest(r, true)
i.data <- buf
http.Error(w, http.StatusText(200), 200)
}
示例9: ExampleDumpRequest
func ExampleDumpRequest() {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
dump, err := httputil.DumpRequest(r, true)
if err != nil {
http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "%q", dump)
}))
defer ts.Close()
const body = "Go is a general-purpose language designed with systems programming in mind."
req, err := http.NewRequest("POST", ts.URL, strings.NewReader(body))
if err != nil {
log.Fatal(err)
}
req.Host = "www.example.org"
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", b)
// Output:
// "POST / HTTP/1.1\r\nHost: www.example.org\r\nAccept-Encoding: gzip\r\nContent-Length: 75\r\nUser-Agent: Go-http-client/1.1\r\n\r\nGo is a general-purpose language designed with systems programming in mind."
}
示例10: WriteTo
func (m *Meta) WriteTo(w io.Writer) (nr int64, err error) {
if m.req != nil {
fprintf(&nr, &err, w, "Type: request\r\n")
} else if m.resp != nil {
fprintf(&nr, &err, w, "Type: response\r\n")
}
fprintf(&nr, &err, w, "ReceivedAt: %v\r\n", m.t)
fprintf(&nr, &err, w, "Session: %d\r\n", m.sess)
fprintf(&nr, &err, w, "From: %v\r\n", m.from)
if m.err != nil {
// note the empty response
fprintf(&nr, &err, w, "Error: %v\r\n\r\n\r\n\r\n", m.err)
} else if m.req != nil {
fprintf(&nr, &err, w, "\r\n")
buf, err2 := httputil.DumpRequest(m.req, false)
if err2 != nil {
return nr, err2
}
write(&nr, &err, w, buf)
} else if m.resp != nil {
fprintf(&nr, &err, w, "\r\n")
buf, err2 := httputil.DumpResponse(m.resp, false)
if err2 != nil {
return nr, err2
}
write(&nr, &err, w, buf)
}
return
}
示例11: ServeHTTP
func (*myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// output request
request, err := httputil.DumpRequest(r, false)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(request))
// create client to get site
tr := &http.Transport{}
//client := http.Client{}
r.RequestURI = "" // pay attention : it is very important
//response, err := client.Do(r)
response, err := tr.RoundTrip(r)
if err != nil {
log.Panic(err, "not get response")
}
defer response.Body.Close()
data, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Panic(err, "not get Body")
}
fmt.Println("Get Body")
// save Header
for i, j := range response.Header {
for _, k := range j {
w.Header().Add(i, k)
}
}
w.WriteHeader(response.StatusCode)
w.Write(data)
}
示例12: Load
// Load loads issues using given configuration
func (c *Client) Load() (*Issues, error) {
req, err := c.Build()
if err != nil {
return nil, err
}
b, err := httputil.DumpRequest(req, false)
if err != nil {
return nil, err
}
log.Printf("%s", b)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
b, err = httputil.DumpResponse(resp, true)
if err != nil {
return nil, err
}
log.Printf("%s", b)
if resp.StatusCode != 200 {
return nil, fmt.Errorf("%s: failed to retrieve issues", resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
data := &Issues{}
err = json.Unmarshal(body, data)
if err != nil {
return nil, err
}
return data, nil
}
示例13: isBanned
func isBanned(r *http.Request) bool {
ctx := req2ctx(r)
cdb := complaintdb.NewDB(ctx)
u := user.Current(cdb.Ctx())
userWhitelist := map[string]int{
"[email protected]": 1,
"[email protected]": 1,
"[email protected]": 1,
"[email protected]": 1,
}
reqBytes, _ := httputil.DumpRequest(r, true)
cdb.Infof("remoteAddr: '%v'", r.RemoteAddr)
cdb.Infof("user: '%v' (%s)", u, u.Email)
cdb.Infof("inbound IP determined as: '%v'", getIP(r))
cdb.Infof("HTTP req:-\n%s", string(reqBytes))
if strings.HasPrefix(r.UserAgent(), "python") {
cdb.Infof("User-Agent rejected")
return true
}
if _, exists := userWhitelist[u.Email]; !exists {
cdb.Infof("user not found in whitelist")
return true
}
return false
}
示例14: TestHTTPOutputChunkedEncoding
func TestHTTPOutputChunkedEncoding(t *testing.T) {
wg := new(sync.WaitGroup)
quit := make(chan int)
input := NewTestInput()
headers := HTTPHeaders{HTTPHeader{"User-Agent", "Gor"}}
methods := HTTPMethods{"GET", "PUT", "POST"}
listener := startHTTP(func(req *http.Request) {
defer req.Body.Close()
body, _ := ioutil.ReadAll(req.Body)
if string(body) != "Wikipedia in\r\n\r\nchunks." {
buf, _ := httputil.DumpRequest(req, true)
t.Error("Wrong POST body:", buf, body, []byte("Wikipedia in\r\n\r\nchunks."))
}
wg.Done()
})
output := NewHTTPOutput(listener.Addr().String(), headers, methods, HTTPUrlRegexp{}, HTTPHeaderFilters{}, HTTPHeaderHashFilters{}, "", UrlRewriteMap{}, 0)
Plugins.Inputs = []io.Reader{input}
Plugins.Outputs = []io.Writer{output}
go Start(quit)
wg.Add(1)
input.EmitChunkedPOST()
wg.Wait()
close(quit)
}
示例15: defaultErrorHandler
// The default error handler
func defaultErrorHandler(ctx *Context, data ...map[string]interface{}) {
var renderData map[string]interface{}
if len(data) == 0 {
renderData = make(map[string]interface{})
renderData["Code"] = ctx.statusCode
renderData["Title"] = http.StatusText(ctx.statusCode)
renderData["Message"] = http.StatusText(ctx.statusCode)
} else {
renderData = data[0]
}
if _, ok := renderData["Code"]; !ok {
renderData["Code"] = ctx.statusCode
}
if _, ok := renderData["Title"]; !ok {
renderData["Title"] = http.StatusText(ctx.statusCode)
}
if _, ok := renderData["Message"]; !ok {
renderData["Message"] = http.StatusText(ctx.statusCode)
}
httpRequest, _ := httputil.DumpRequest(ctx.Request, true)
renderData["HTTPRequest"] = string(httpRequest)
var buf bytes.Buffer
tmpl.Parse(errorTemplate)
tmpl.Execute(&buf, renderData)
ctx.Send(&buf)
}