How does one parse HTTP headers with libcurl?

2019-04-20 02:23发布

问题:

I've been looking around and am quite surprised that there seems to be no means by which one can parse headers generically in libcurl (which seems to be the canonical C library for http these days).

The closest thing I've found was a mailing list post where someone suggested someone else search through the mailing list archives.

The only facility that is provided by libcurl via setopt is CURLOPT_HEADERFUNCTION which will feed the header responses a single line at a time.

This seems entirely too primitive considering headers can span multiple lines. Ideally this should be done once correctly (preferably by the library itself) and not left for the application developers to do continually reinvent.

Edit:

An example of the naïve thing not working, see the following gist with a libcurl code example and a properly formed http response that it can't parse: https://gist.github.com/762954

回答1:

Been over a year, so I think I'll close this as "manually." Or:

If you're having cURL problems, I feel bad for you son,

You've got multi-line headers and must parse each one.



回答2:

libcurl reads the entire header and sends it as a single complete line to the callback.

"Continued" HTTP header lines are not allowed in the HTTP 1.1 RFC 7230 family, and they were virtually extinct even before that.