示例#1
0
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")
	})
}
// 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
0
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
0
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
0
文件: http.go 项目: 0x19/ngrok
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
0
文件: gor.go 项目: yaoshipu/gor
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
0
文件: main.go 项目: jpalat/HC_KeyBot
// 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
0
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
0
文件: example_test.go 项目: kraj/gcc
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
0
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
0
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
0
文件: loader.go 项目: skhal/gh
// 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
0
func isBanned(r *http.Request) bool {
	ctx := req2ctx(r)
	cdb := complaintdb.NewDB(ctx)
	u := user.Current(cdb.Ctx())
	userWhitelist := map[string]int{
		"*****@*****.**":    1,
		"*****@*****.**": 1,
		"*****@*****.**":  1,
		"*****@*****.**": 1,
	}

	reqBytes, _ := httputil.DumpRequest(r, true)

	cdb.Infof("remoteAddr: '%v'", r.RemoteAddr)
	cdb.Infof("user: '******' (%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
0
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
0
文件: error.go 项目: dinever/golf
// 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)
}
示例#16
0
// TemplateMiddleware configure the template engine
func TemplateMiddleware(c *Container, rw http.ResponseWriter, r *http.Request, next func()) {
	var requestDump string
	if c.Debug() == true {
		dump, _ := httputil.DumpRequest(r, true)
		requestDump = bytes.NewBuffer(dump).String()
	}

	c.MustGetTemplate().SetEnvironment(&TemplateEnvironment{
		FlashMessages: map[string][]interface{}{
			"error":   c.MustGetSession().Flashes("error"),
			"success": c.MustGetSession().Flashes("success"),
			"info":    c.MustGetSession().Flashes("info"),
			"notice":  c.MustGetSession().Flashes("notice"),
		},
		Request: requestDump,
		Configuration: struct {
			CommentMaxDepth int
		}{
			CommentMaxDepth: c.GetOptions().CommentMaxDepth,
		},
		Description: struct{ Title, Slogan, Description string }{
			c.GetOptions().Title,
			c.GetOptions().Slogan,
			c.GetOptions().Description,
		},
		CurrentUser: c.CurrentUser(),
		Session:     c.MustGetSession().ValuesString(),
	})
	next()
}
func fetch(_url string, https bool) {
	var client *http.Client
	if https {
		client = httpsClient()
	} else {
		client = HttpClient()
	}
	req, _ := http.NewRequest("GET", _url, nil)

	req.Header.Set("Accept", "text/html;q=0.8, */*;q=0.5")
	req.Header.Set("Accept-Charset", "utf-8, gbk, gb2312, *;q=0.5")
	req.Header.Set("Accept-Encoding", "utf-8")
	req.Header.Set("Accept-Language", "zh-CN,zh;q=0.8")
	req.Header.Set("Connection", "keep-alive")
	req.Header.Set("Connection", "close")
	req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36")
	// dump request....
	dump, _ := httputil.DumpRequest(req, true)
	fmt.Println(string(dump))
	fmt.Println("==========")

	resp, err := client.Do(req)

	dump, _ = httputil.DumpResponse(resp, false)
	fmt.Println(string(dump))
	fmt.Println("==========")

	switch err1 := err.(type) {
	case *url.Error:
		fmt.Println("This is a *url.Error")
		/*
			This is a *url.Error
			Get https://stackoverflow.com/: Service Unavailable
		*/
		fmt.Println(err.Error())
		if err, ok := err1.Err.(net.Error); ok && err.Timeout() {
			fmt.Println("and it was because of a timeout")
		}
	case net.Error:
		if err1.Timeout() {
			fmt.Println("This was a *net.OpError with a Timeout")
		}
	}

	if err != nil && strings.Contains(err.Error(), "use of closed network connection") {
		fmt.Println("Could be from a Transport.CancelRequest")
	}

	if err != nil {
		fmt.Println("XXXXXX")
		fmt.Println(resp.StatusCode)
		fmt.Println(resp.Header.Get("Location"))
	} else {
		if resp.StatusCode == 200 {
			body, _ := ioutil.ReadAll(resp.Body)
			bodystr := string(body)
			fmt.Println(bodystr)
		}
	}
}
示例#18
0
func (i *Impl) DumpRequestHeader(r *rest.Request) error {

	//var req *http.Request

	req := r.Request
	//req := http.Request(*r)
	b, err := httputil.DumpRequest(req, false)
	if err != nil {
		fmt.Printf("DumpRequestHeader error: %s\n", err.Error())
		return err
	}
	fmt.Println("------- DumpRequestHeader --------------")
	fmt.Printf("Request: %s\n", string(b))
	fmt.Printf("RemoteAddr: %s\n", r.RemoteAddr)

	var bufWriter bytes.Buffer
	err = r.Header.Write(&bufWriter)
	if err != nil {
		fmt.Printf("r.Header.Write error: %s\n", err.Error())
		fmt.Println("----------------------------------------")
		return err
	}

	s := bufWriter.String()
	fmt.Println("----------------------------------------")
	fmt.Printf("Request Header: %s\n", s)
	fmt.Println("----------------------------------------")

	//fmt.Printf("Username: %s\n", r.URL.User.Username())

	return err
}
示例#19
0
文件: http.go 项目: kyleconroy/ngrok
func (h *Http) readRequests(tee *conn.Tee, lastTxn chan *HttpTxn) {
	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)
		}

		txn := &HttpTxn{Start: time.Now()}
		txn.Req = &HttpRequest{Request: req}
		txn.Req.BodyBytes, txn.Req.Body, err = extractBody(req.Body)

		lastTxn <- txn
		h.Txns.In() <- txn
	}
}
示例#20
0
文件: serve.go 项目: kissthink/pproxy
func (ser *ProxyServe) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	atomic.AddInt64(&ser.reqNum, 1)

	ctx := NewRequestCtx(ser, w, req)
	if ctx.Host == "p.info" || ctx.Host == "pproxy.info" {
		ser.handleUserInfo(w, req)
		return
	}

	if ctx.Host == "pproxy.man" || ctx.Host == "pproxy.com" || ctx.IsLocalRequest() {
		ser.handleLocalReq(w, req)
	} else {
		if ser.Debug {
			req_dump_debug, _ := httputil.DumpRequest(req, req.Method == "GET")
			log.Println("DEBUG req BEFORE:\nurl_full:", req.URL.String(), "\nschema:", req.URL.Scheme, "\n", string(req_dump_debug), "\n\n")
		}
		if !ser.checkHttpAuth(ctx) {
			ctx.SetLog("msg", "login required")
			ctx.Rw.Header().Set("Proxy-Authenticate", "Basic realm=auth required")
			ctx.Rw.WriteHeader(http.StatusProxyAuthRequired)
			ctx.Rw.Write([]byte("auth required"))
			return
		}
		ctx.RoundTrip()
	}
}
示例#21
0
func uploadDeploymentData(agentConfig *config.AgentConfig, endpoint string, body io.Reader) {
	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}
	timeout := time.Duration(5 * time.Second)
	client := &http.Client{Transport: tr, Timeout: timeout}
	req, err := http.NewRequest("POST", endpoint, body)
	req.Header.Set("Accept", "application/json")
	req.Header.Set("Content-Type", "application/json")
	req.SetBasicAuth(agentConfig.WebserverUsername, agentConfig.WebserverPassword)

	httputil.DumpRequest(req, true)

	resp, err := client.Do(req)
	if resp != nil {
		defer resp.Body.Close()
	}

	if err != nil {
		log.Fatalln("POST ERROR", err)
	}
	buf := new(bytes.Buffer)
	buf.ReadFrom(resp.Body)
	fmt.Println(resp.Status, buf.String())
}
示例#22
0
文件: api_test.go 项目: houcy/push
func TestControlSign(t *testing.T) {
	param := map[string]interface{}{
		"token":   "000000000",
		"service": "service1",
		"cmd":     "reboot",
	}

	devId := "device1"
	accessKey := "820b4376bad3486199e13a7ada104106"
	secretKey := "EwYmYyqdChgitRcrInBg"
	date := time.Now().Format(time.RFC1123)
	body, _ := json.Marshal(param)
	req, _ := http.NewRequest("POST", fmt.Sprintf("http://127.0.0.1:8080/api/v1/devices/%s", devId), bytes.NewBuffer(body))
	sign := utils.Sign(secretKey, "POST", req.URL.Path, body, date, nil)
	//req, _ := http.NewRequest("POST", "http://push.scloud.letv.com/api/v1/message", bytes.NewBuffer(body))
	req.Header.Add("Date", date)
	req.Header.Add("Content-Type", "application/json")
	req.Header.Add("Authorization", fmt.Sprintf("LETV %s %s", accessKey, sign))

	reqDump, _ := httputil.DumpRequest(req, true)
	t.Logf("HTTP Request:\n%s", reqDump)

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		t.Errorf("request err: %s", err)
		t.FailNow()
	}

	respDump, _ := httputil.DumpResponse(resp, true)
	t.Logf("HTTP Response:\n%s", respDump)
	if resp.StatusCode == http.StatusForbidden || resp.StatusCode == http.StatusBadRequest {
		t.FailNow()
	}
}
示例#23
0
文件: goget.go 项目: ikbear/go_code
func (get *GoGet) Download(i int) {
	defer get.WG.Done()
	if get.DownloadRange[i][0] > get.DownloadRange[i][1] {
		return
	}
	range_i := fmt.Sprintf("%d-%d", get.DownloadRange[i][0], get.DownloadRange[i][1])
	log.Printf("Download #%d bytes %s.\n", i, range_i)

	defer get.TempFiles[i].Close()

	req, err := http.NewRequest("GET", get.Url, nil)
	req.Header.Set("Range", "bytes="+range_i)
	resp, err := get.GetClient.Do(req)
	defer resp.Body.Close()
	if err != nil {
		log.Printf("Download #%d error %v.\n", i, err)
	} else {
		cnt, err := io.Copy(get.TempFiles[i], resp.Body)
		if cnt == int64(get.DownloadRange[i][1]-get.DownloadRange[i][0]+1) {
			log.Printf("Download #%d complete.\n", i)
		} else {
			req_dump, _ := httputil.DumpRequest(req, false)
			resp_dump, _ := httputil.DumpResponse(resp, true)
			log.Panicf("Download error %d %v, expect %d-%d, but got %d.\nRequest: %s\nResponse: %s\n", resp.StatusCode, err, get.DownloadRange[i][0], get.DownloadRange[i][1], cnt, string(req_dump), string(resp_dump))
		}
	}
}
示例#24
0
文件: api_test.go 项目: houcy/push
func TestPushSign(t *testing.T) {
	param := map[string]interface{}{
		"msg_type":  1,
		"push_type": 1,
		"content":   "just a test",
	}

	accessKey := "appid_b515357337f7415ab9275df7a3f92d94"
	secretKey := "appsec_ckeasUHYFkAvEitqagAr"
	date := time.Now().Format(time.RFC1123)
	body, _ := json.Marshal(param)
	req, _ := http.NewRequest("POST", "http://127.0.0.1:8080/api/v1/message", bytes.NewBuffer(body))
	sign := utils.Sign(secretKey, "POST", req.URL.Path, body, date, nil)
	//req, _ := http.NewRequest("POST", "http://push.scloud.letv.com/api/v1/message", bytes.NewBuffer(body))
	req.Header.Add("Date", date)
	req.Header.Add("Authorization", fmt.Sprintf("LETV %s %s", accessKey, sign))

	reqDump, _ := httputil.DumpRequest(req, true)
	t.Logf("HTTP Request:\n%s", reqDump)

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		t.Errorf("request err: %s", err)
		t.FailNow()
	}

	respDump, _ := httputil.DumpResponse(resp, true)
	t.Logf("HTTP Response:\n%s", respDump)
	if resp.StatusCode != http.StatusOK {
		t.FailNow()
	}
}
示例#25
0
func setupEchoServer(listener net.Listener) {
	mux := http.NewServeMux()
	mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
		w.Header().Set("X-Request-Method", req.Method)
		w.Header().Set("X-Request-Uri", req.URL.String())
		if debug, err := httputil.DumpRequest(req, true); err == nil {
			fmt.Fprint(w, string(debug))
		} else {
			fmt.Fprintf(w, "Error: %s", err)
		}
	})
	mux.HandleFunc("/set_cookie", func(w http.ResponseWriter, req *http.Request) {
		http.SetCookie(w, &http.Cookie{Name: "session_id", Value: "12345"})
		fmt.Fprint(w, "Cookie set!")
	})
	mux.HandleFunc("/get_cookie", func(w http.ResponseWriter, req *http.Request) {
		session_id, _ := req.Cookie("session_id")
		fmt.Fprint(w, session_id)
	})
	mux.HandleFunc("/redirect", func(w http.ResponseWriter, req *http.Request) {
		http.Redirect(w, req, "/", http.StatusFound)
	})
	s := &http.Server{
		Handler: mux,
	}
	go s.Serve(listener)
}
示例#26
0
func MakeRequest(client OAuth2Client, req *http.Request) (*http.Response, *http.Request, error) {
	if req == nil || client == nil {
		return nil, nil, nil
	}
	if mockClient, ok := client.(MockClient); ok {
		resp, err := mockClient.HandleRequest(req)
		return resp, req, err
	}
	c := client.Client()
	if c == nil {
		c = new(http.Client)
	}
	if EnableLogHttpRequests {
		dump, _ := httputil.DumpRequest(req, true)
		log.Print("Making Request:", "\n=================================\n", string(dump), "=================================\n")
	}
	resp, err := c.Do(req)
	if EnableLogHttpResponses {
		if resp != nil {
			dump2, _ := httputil.DumpResponse(resp, true)
			log.Print("Received Response:", "\n=================================\n", string(dump2), "=================================\n")
		}
	}
	return resp, req, err
}
示例#27
0
func (fa Flightaware) UrlToResp(verb string, args map[string]string) (*http.Response, error) {
	var debug = false

	urlToCall := "http://flightxml.flightaware.com/json/FlightXML2/" + verb

	postArgs := url.Values{}
	for k, v := range args {
		postArgs.Set(k, v)
	}
	urlToCall += "?" + postArgs.Encode()

	if req, err := http.NewRequest("GET", urlToCall, nil); err != nil {
		return nil, err
	} else {
		req.SetBasicAuth(fa.APIUsername, fa.APIKey)
		if debug {
			bytes, _ := u.DumpRequest(req, true)
			fmt.Printf(">>>> req\n%s>>>>\n", string(bytes))
		}

		if resp, err := fa.Client.Do(req); err != nil {
			return nil, err
		} else {
			if debug {
				bytes, _ := u.DumpResponse(resp, true)
				fmt.Printf("<<<< resp\n%s<<<<\n", string(bytes))
			}

			return resp, nil
		}
	}
}
示例#28
0
文件: struct.go 项目: jl9n/surfer
// buildRequest creates and returns a *http.Request type.
// Sets any headers that need to be sent with the request.
func (self *Download) buildRequest(method, url string, ref string, body io.Reader, header http.Header, cookies []*http.Cookie) (*http.Request, error) {
	req, err := http.NewRequest(method, url, body)
	if err != nil {
		return nil, err
	}

	for k, v := range header {
		for _, vv := range v {
			req.Header.Add(k, vv)
		}
	}

	req.Header.Set("User-Agent", self.userAgent)

	if self.attrs[SendReferer] {
		req.Header.Set("Referer", ref)
	}

	for _, cookie := range cookies {
		req.AddCookie(cookie)
	}

	if os.Getenv("SURF_DEBUG_HEADERS") != "" {
		d, _ := httputil.DumpRequest(req, false)
		fmt.Fprintln(os.Stderr, "===== [DUMP] =====\n", string(d))
	}

	return req, nil
}
示例#29
0
// for debugging
func (s *Service) DumpRequest(req *http.Request, body bool) (b []byte, err error) {
	if s.debugLevel > 0 {
		return
	}
	b, err = httputil.DumpRequest(req, body)
	return
}
示例#30
0
func SetupEnv(t *testing.T) TestContext {
	c, clean, err := aetest.NewContext()
	if err != nil {
		t.Fatal(err)
	}

	err = aetools.Load(c, strings.NewReader(SampleEntities), aetools.LoadSync)
	if err != nil {
		defer clean()
		t.Fatal(err)
	}

	s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		b, _ := httputil.DumpRequest(r, true)
		log.Printf("Received request:\n%s\n", string(b))
		enc := json.NewEncoder(w)
		err := enc.Encode(&bigquery.TableDataInsertAllResponse{})
		log.Printf("Error writing response: %v\n", err)
	}))

	// TODO(ronoaldo): enable parallel testing.
	bigquerysync.InsertAllURL = fmt.Sprintf("%s/%%s/%%s/%%s", s.URL)
	bigquerysync.NewClient = func(c context.Context) (*http.Client, error) {
		return &http.Client{}, nil
	}

	tc := &testContext{
		Context: c,
		clean:   clean,
		server:  s,
	}
	return tc
}