howerj / httpc

HTTP client for embedded use - supports redirects and resume.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Separate HTTP status codes from function return values

denravonska opened this issue · comments

Right now the return values from the coexist with the status code returned by the server.

enum {
	HTTPC_ERROR = -1, /* negated HTTP error codes also returned */
	HTTPC_OK    =  0, /* all operations completed successfully */
	HTTPC_YIELD =  1, /* call again later - the operation has not finished */
	HTTPC_REUSE =  2, /* operation complete, connection not closed */
};

This can make it tricky in cases where you want both fields. For example, when doing a GET request while using socket reuse the httpc_get call will return HTTPC_REUSE instead of 200 or, in my case, 206.

It could be a good idea to separate these two to make return value handling more intuitive. I don't know what the best option would be. Perhaps passing a struct to the requests, which would also solve #5 .

struct httpc_response_t {
   int16_t status_code;
   size_t content_length;
}

I'll take a look and see what I can come up with, the content length isn't always available though.

I've pushed some changes that put the response code in the options structure, I'll probably do the same for the content length

That works for me, thanks! If it becomes too cluttered the response content could maybe go into a response struct inside options, just to keep things tidy.