Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(option): disable redirect option #39

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@ func (s *SuperAgent) AddCookies(cookies []*http.Cookie) *SuperAgent {
s.Cookies = append(s.Cookies, cookies...)
return s
}

func shallowCopyCookies(old []*http.Cookie) []*http.Cookie {
if old == nil {
return nil
}
newData := make([]*http.Cookie, len(old))
copy(newData, old)
return newData
}
19 changes: 18 additions & 1 deletion curl.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package gorequest

import "moul.io/http2curl"
import (
"net/http"

"moul.io/http2curl"
)

// SetCurlCommand enable the curlcommand mode which display a CURL command line.
func (s *SuperAgent) SetCurlCommand(enable bool) *SuperAgent {
Expand All @@ -11,6 +15,7 @@ func (s *SuperAgent) SetCurlCommand(enable bool) *SuperAgent {
// AsCurlCommand returns a string representing the runnable `curl' command
// version of the request.
func (s *SuperAgent) AsCurlCommand() (string, error) {
// FIXME: why here MakeRequest again?
req, err := s.MakeRequest()
if err != nil {
return "", err
Expand All @@ -21,3 +26,15 @@ func (s *SuperAgent) AsCurlCommand() (string, error) {
}
return cmd.String(), nil
}

func (s *SuperAgent) logCurlCommand(req *http.Request) {
if s.CurlCommand {
curl, err := http2curl.GetCurlCommand(req)
s.logger.SetPrefix("[curl] ")
if err != nil {
s.logger.Println("Error:", err)
} else {
s.logger.Printf("CURL command line: %s", curl)
}
}
}
35 changes: 35 additions & 0 deletions debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package gorequest

import (
"net/http"
"net/http/httputil"
)

// SetDebug enable the debug mode which logs request/response detail.
func (s *SuperAgent) SetDebug(enable bool) *SuperAgent {
s.Debug = enable
return s
}

func (s *SuperAgent) debuggingRequest(req *http.Request) {
if s.Debug {
dump, err := httputil.DumpRequest(req, true)
s.logger.SetPrefix("[http] ")
if err != nil {
s.logger.Println("Error:", err)
} else {
s.logger.Printf("HTTP Request: %s", BytesToString(dump))
}
}
}

func (s *SuperAgent) debuggingResponse(resp *http.Response) {
if s.Debug {
dump, err := httputil.DumpResponse(resp, true)
if nil != err {
s.logger.Println("Error:", err)
} else {
s.logger.Printf("HTTP Response: %s", BytesToString(dump))
}
}
}
50 changes: 3 additions & 47 deletions gorequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"net/http"
"net/http/cookiejar"
"net/http/httptrace"
"net/http/httputil"
"net/textproto"
"net/url"
"os"
Expand All @@ -24,8 +23,6 @@ import (

"github.com/wklken/gorequest/internal/json"
"golang.org/x/net/publicsuffix"
"gopkg.in/h2non/gock.v1"
"moul.io/http2curl"
)

type (
Expand Down Expand Up @@ -154,24 +151,6 @@ func (s *SuperAgent) Context(ctx context.Context) *SuperAgent {
return s
}

func (s *SuperAgent) HttpTrace(trace *httptrace.ClientTrace) *SuperAgent {
s.trace = trace
return s
}

// Mock will enable gock, http mocking for net/http
func (s *SuperAgent) Mock() *SuperAgent {
gock.InterceptClient(s.Client)
s.isMock = true
return s
}

// SetDebug enable the debug mode which logs request/response detail.
func (s *SuperAgent) SetDebug(enable bool) *SuperAgent {
s.Debug = enable
return s
}

// SetDoNotClearSuperAgent enable the DoNotClear mode for not clearing super agent and reuse for the next request.
func (s *SuperAgent) SetDoNotClearSuperAgent(enable bool) *SuperAgent {
s.DoNotClearSuperAgent = enable
Expand Down Expand Up @@ -730,26 +709,10 @@ func (s *SuperAgent) getResponseBytes() (Response, []byte, []error) {
}

// Log details of this request
if s.Debug {
dump, err := httputil.DumpRequest(req, true)
s.logger.SetPrefix("[http] ")
if err != nil {
s.logger.Println("Error:", err)
} else {
s.logger.Printf("HTTP Request: %s", BytesToString(dump))
}
}
s.debuggingRequest(req)

// Display CURL command line
if s.CurlCommand {
curl, err := http2curl.GetCurlCommand(req)
s.logger.SetPrefix("[curl] ")
if err != nil {
s.logger.Println("Error:", err)
} else {
s.logger.Printf("CURL command line: %s", curl)
}
}
s.logCurlCommand(req)

startTime := time.Now()
// stats collect the requestBytes
Expand All @@ -767,14 +730,7 @@ func (s *SuperAgent) getResponseBytes() (Response, []byte, []error) {
s.Stats.RequestDuration = time.Since(startTime)

// Log details of this response
if s.Debug {
dump, err := httputil.DumpResponse(resp, true)
if nil != err {
s.logger.Println("Error:", err)
} else {
s.logger.Printf("HTTP Response: %s", BytesToString(dump))
}
}
s.debuggingResponse(resp)

body, err := ioutil.ReadAll(resp.Body)
// Reset resp.Body so it can be use again
Expand Down
10 changes: 10 additions & 0 deletions mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gorequest

import "gopkg.in/h2non/gock.v1"

// Mock will enable gock, http mocking for net/http
func (s *SuperAgent) Mock() *SuperAgent {
gock.InterceptClient(s.Client)
s.isMock = true
return s
}
8 changes: 8 additions & 0 deletions redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ func (s *SuperAgent) RedirectPolicy(policy func(req Request, via []Request) erro
}
return s
}

// DisableRedirect will disable the redirect of status code 3xx.
func (s *SuperAgent) DisableRedirect() *SuperAgent {
s.Client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
return s
}
10 changes: 10 additions & 0 deletions retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,13 @@ func (s *SuperAgent) shouldRetry(resp Response, hasError bool) bool {
}
return false
}

// just need to change the array pointer?
func copyRetryable(old superAgentRetryable) superAgentRetryable {
newRetryable := old
newRetryable.RetryStatus = make([]int, len(old.RetryStatus))
for i := range old.RetryStatus {
newRetryable.RetryStatus[i] = old.RetryStatus[i]
}
return newRetryable
}
5 changes: 5 additions & 0 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ type Stats struct {

RequestDuration time.Duration
}

func copyStats(old Stats) Stats {
newStats := old
return newStats
}
8 changes: 8 additions & 0 deletions trace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package gorequest

import "net/http/httptrace"

func (s *SuperAgent) HttpTrace(trace *httptrace.ClientTrace) *SuperAgent {
s.trace = trace
return s
}
27 changes: 1 addition & 26 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io"
"mime/multipart"
"net/http"
"net/textproto"
"net/url"
"reflect"
Expand All @@ -25,26 +24,11 @@ func cloneMapArray(old map[string][]string) map[string][]string {
return newMap
}

// just need to change the array pointer?
func copyRetryable(old superAgentRetryable) superAgentRetryable {
newRetryable := old
newRetryable.RetryStatus = make([]int, len(old.RetryStatus))
for i := range old.RetryStatus {
newRetryable.RetryStatus[i] = old.RetryStatus[i]
}
return newRetryable
}

func copyStats(old Stats) Stats {
newStats := old
return newStats
}

func shallowCopyData(old map[string]interface{}) map[string]interface{} {
if old == nil {
return nil
}
newData := make(map[string]interface{})
newData := make(map[string]interface{}, len(old))
for k, val := range old {
newData[k] = val
}
Expand All @@ -69,15 +53,6 @@ func shallowCopyFileArray(old []File) []File {
return newData
}

func shallowCopyCookies(old []*http.Cookie) []*http.Cookie {
if old == nil {
return nil
}
newData := make([]*http.Cookie, len(old))
copy(newData, old)
return newData
}

func shallowCopyErrors(old []error) []error {
if old == nil {
return nil
Expand Down