| 2809 | } |
| 2810 | |
| 2811 | func (h *ResponseHeader) parseFirstLine(buf []byte) (int, error) { |
| 2812 | bNext := buf |
| 2813 | var b []byte |
| 2814 | var err error |
| 2815 | for len(b) == 0 { |
| 2816 | if b, bNext, err = nextLine(bNext); err != nil { |
| 2817 | return 0, err |
| 2818 | } |
| 2819 | } |
| 2820 | |
| 2821 | // parse protocol |
| 2822 | n := bytes.IndexByte(b, ' ') |
| 2823 | if n < 0 { |
| 2824 | if h.secureErrorLogMessage { |
| 2825 | return 0, ErrResponseFirstLineMissingSpace |
| 2826 | } |
| 2827 | return 0, fmt.Errorf("cannot find whitespace in the first line of response %q", buf) |
| 2828 | } |
| 2829 | protoStr := b[:n] |
| 2830 | b = b[n+1:] |
| 2831 | for len(b) > 0 && b[0] == ' ' { |
| 2832 | b = b[1:] |
| 2833 | } |
| 2834 | |
| 2835 | // parse status code |
| 2836 | statusCode := b |
| 2837 | statusMessage := []byte(nil) |
| 2838 | if n = bytes.IndexByte(b, ' '); n >= 0 { |
| 2839 | statusCode = b[:n] |
| 2840 | statusMessage = b[n+1:] |
| 2841 | } |
| 2842 | if len(statusCode) != 3 { |
| 2843 | if h.secureErrorLogMessage { |
| 2844 | return 0, ErrUnexpectedStatusCodeChar |
| 2845 | } |
| 2846 | return 0, fmt.Errorf("invalid response status code %q. Response %q", statusCode, buf) |
| 2847 | } |
| 2848 | h.statusCode, n, err = parseUintBuf(statusCode) |
| 2849 | if err != nil || n != 3 { |
| 2850 | if h.secureErrorLogMessage { |
| 2851 | return 0, ErrUnexpectedStatusCodeChar |
| 2852 | } |
| 2853 | return 0, fmt.Errorf("invalid response status code %q. Response %q", statusCode, buf) |
| 2854 | } |
| 2855 | if !isHTTPVersion(protoStr) { |
| 2856 | if h.secureErrorLogMessage { |
| 2857 | return 0, fmt.Errorf("unsupported HTTP version %q", protoStr) |
| 2858 | } |
| 2859 | return 0, fmt.Errorf("unsupported HTTP version %q in %q", protoStr, buf) |
| 2860 | } |
| 2861 | h.noHTTP11 = !bytes.Equal(protoStr, strHTTP11) |
| 2862 | h.protocol = append(h.protocol[:0], protoStr...) |
| 2863 | if len(statusMessage) > 0 { |
| 2864 | h.SetStatusMessage(statusMessage) |
| 2865 | } |
| 2866 | |
| 2867 | return len(buf) - len(bNext), nil |
| 2868 | } |