// ReadResponse reads and returns an HTTP response from r.
// The req parameter optionally specifies the Request that corresponds
// to this Response. If nil, a GET request is assumed.
// Clients must call resp.Body.Close when finished reading resp.Body.
// After that call, clients can inspect resp.Trailer to find key/value
// pairs included in the response trailer.
ReadResponse reads and returns an HTTP response from r.
The req parameter optionally specifies the Request that corresponds
to this Response. If nil, a GET request is assumed.
Clients must call resp.Body.Close when finished reading resp.Body.
After that call, clients can inspect resp.Trailer to find key/value
pairs included in the response trailer.
// ReadResponse reads and returns an HTTP response from r . // The req parameter optionally specifies the Request that corresponds // to this Response . If nil a GET request is assumed . // Clients must call resp . Body . Close when finished reading resp . Body . // After that call clients can inspect resp . Trailer to find key / value // pairs included in the response trailer .
func ReadResponse(r *bufio.Reader, req *http.Request) (*http.Response, error) {
	tp := textproto.NewReader(r)
	resp := &http.Response{
		Request: req,
	}
	line, err := tp.ReadLine()
	if err != nil {
		if err == io.EOF {
			err = io.ErrUnexpectedEOF
		}
		return nil, err
	}
	if i := strings.IndexByte(line, ' '); i == -1 {
		return nil, badStringError("malformed HTTP response", line)
	} else {
		resp.Proto = line[:i]
		resp.Status = strings.TrimLeft(line[i+1:], " ")
	}
	statusCode := resp.Status
	if i := strings.IndexByte(resp.Status, ' '); i != -1 {
		statusCode = resp.Status[:i]
	}
	if len(statusCode) != 3 {
		return nil, badStringError("malformed HTTP status code", statusCode)
	}
	resp.StatusCode, err = strconv.Atoi(statusCode)
	if err != nil || resp.StatusCode < 0 {
		return nil, badStringError("malformed HTTP status code", statusCode)
	}
	var ok bool
	if resp.ProtoMajor, resp.ProtoMinor, ok = http.ParseHTTPVersion(resp.Proto); !ok {
		return nil, badStringError("malformed HTTP version", resp.Proto)
	}
	mimeHeader, err := tp.ReadMIMEHeader()
	if err != nil {
		if err == io.EOF {
			err = io.ErrUnexpectedEOF
		}
		return nil, err
	}
	resp.Header = http.Header(mimeHeader)
	fixPragmaCacheControl(resp.Header)
	err = readTransfer(resp, r)
	if err != nil {
		return nil, err
	}
	return resp, nil
}
func ReadResponse ( r * bufio . Reader , req * http . Request ) ( * http . Response , error ) { tp := textproto . NewReader ( r ) resp := & http . Response { Request : req , } line , err := tp . ReadLine ( ) if err != nil { if err == io . EOF { err = io . ErrUnexpectedEOF } return nil , err } if i := strings . IndexByte ( line , ' ' ) ; i == - 1 { return nil , badStringError ( " " , line ) } else { resp . Proto = line [ : i ] resp . Status = strings . TrimLeft ( line [ i + 1 : ] , " " ) } statusCode := resp . Status if i := strings . IndexByte ( resp . Status , ' ' ) ; i != - 1 { statusCode = resp . Status [ : i ] } if len ( statusCode ) != 3 { return nil , badStringError ( " " , statusCode ) } resp . StatusCode , err = strconv . Atoi ( statusCode ) if err != nil || resp . StatusCode < 0 { return nil , badStringError ( " " , statusCode ) } var ok bool if resp . ProtoMajor , resp . ProtoMinor , ok = http . ParseHTTPVersion ( resp . Proto ) ; ! ok { return nil , badStringError ( " " , resp . Proto ) } mimeHeader , err := tp . ReadMIMEHeader ( ) if err != nil { if err == io . EOF { err = io . ErrUnexpectedEOF } return nil , err } resp . Header = http . Header ( mimeHeader ) fixPragmaCacheControl ( resp . Header ) err = readTransfer ( resp , r ) if err != nil { return nil , err } return resp , nil }
ReadResponse reads and returns an HTTP response from r.
The req parameter optionally specifies the Request that corresponds
to this Response.
ReadResponse reads and returns an HTTP response from r . The req parameter optionally specifies the Request that corresponds to this Response .
// Parse the first line of the response. // Parse the response headers.