github.com/ooni/oohttp@v0.7.2/stdlibwrapper.go (about) 1 package http 2 3 import "net/http" 4 5 // StdlibTransport is an adapter for integrating net/http dependend code. 6 // It looks like an http.RoundTripper but uses this fork internally. 7 type StdlibTransport struct { 8 *Transport 9 } 10 11 // RoundTrip implements the http.RoundTripper interface. 12 func (txp *StdlibTransport) RoundTrip(stdReq *http.Request) (*http.Response, error) { 13 req := &Request{ 14 Method: stdReq.Method, 15 URL: stdReq.URL, 16 Proto: stdReq.Proto, 17 ProtoMajor: stdReq.ProtoMajor, 18 ProtoMinor: stdReq.ProtoMinor, 19 Header: Header(stdReq.Header), 20 Body: stdReq.Body, 21 GetBody: stdReq.GetBody, 22 ContentLength: stdReq.ContentLength, 23 TransferEncoding: stdReq.TransferEncoding, 24 Close: stdReq.Close, 25 Host: stdReq.Host, 26 Form: stdReq.Form, 27 PostForm: stdReq.PostForm, 28 MultipartForm: stdReq.MultipartForm, 29 Trailer: Header(stdReq.Trailer), 30 RemoteAddr: stdReq.RemoteAddr, 31 RequestURI: stdReq.RequestURI, 32 TLS: stdReq.TLS, 33 Cancel: stdReq.Cancel, 34 Response: nil, // cannot assign this field 35 ctx: stdReq.Context(), 36 } 37 38 // http.NoBody is a global var with oohttp.NoBody being its analogue 39 // this guards against undefined content length in case when stdReq.Body == http.NoBody 40 if req.Body == http.NoBody { 41 req.Body = NoBody 42 } 43 44 resp, err := txp.Transport.RoundTrip(req) 45 if err != nil { 46 return nil, err 47 } 48 stdResp := &http.Response{ 49 Status: resp.Status, 50 StatusCode: resp.StatusCode, 51 Proto: resp.Proto, 52 ProtoMinor: resp.ProtoMinor, 53 ProtoMajor: resp.ProtoMajor, 54 Header: http.Header(resp.Header), 55 Body: resp.Body, 56 ContentLength: resp.ContentLength, 57 TransferEncoding: resp.TransferEncoding, 58 Close: resp.Close, 59 Uncompressed: resp.Uncompressed, 60 Trailer: http.Header(resp.Trailer), 61 Request: stdReq, 62 TLS: resp.TLS, 63 } 64 return stdResp, nil 65 }