lwip-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lwip-users] http post using lwip?


From: Bas Prins
Subject: Re: [lwip-users] http post using lwip?
Date: Mon, 4 Jul 2022 10:24:42 +0200

Hi all,
I think i have "something" up for review. The thing is, there are a couple of ways I can approach this. I think the most important one is, whether to have one function for httpc_create_request_string, or make dedicated ones for http_get, http_post, etc. I think it's a matter of taste.

This is what I have now:
static int
httpc_create_request_string(const httpc_connection_t *settings, const char* server_name, int server_port, const char* uri,
const httpc_request_info_t* request_info, int use_host, char *buffer, size_t buffer_size)
{
LWIP_ASSERT("request_info != NULL", request_info != NULL);

if (request_info->request_type == HTTPC_GET) {
if (settings->use_proxy) {
LWIP_ASSERT("server_name != NULL", server_name != NULL);
if (server_port != HTTP_DEFAULT_PORT) {
return snprintf(buffer, buffer_size, HTTPC_REQ_11_PROXY_PORT_FORMAT(server_name, server_port, uri, server_name));
} else {
return snprintf(buffer, buffer_size, HTTPC_REQ_11_PROXY_FORMAT(server_name, uri, server_name));
}
} else if (use_host) {
LWIP_ASSERT("server_name != NULL", server_name != NULL);
return snprintf(buffer, buffer_size, HTTPC_REQ_11_HOST_FORMAT(uri, server_name));
} else {
return snprintf(buffer, buffer_size, HTTPC_REQ_11_FORMAT(uri));
}
} else if (request_info->request_type == HTTPC_POST) {
if (settings->use_proxy) {
LWIP_ASSERT("server_name != NULL", server_name != NULL);
if (server_port != HTTP_DEFAULT_PORT) {
return snprintf(buffer, buffer_size, HTTPC_POST_REQ_11_PROXY_PORT_FORMAT(
server_name,
server_port,
uri,
server_name,
get_application_type(request_info->application_type),
strlen(request_info->payload),
request_info->payload));
} else {
return snprintf(buffer, buffer_size, HTTPC_POST_REQ_11_PROXY_FORMAT(
server_name,
uri,
server_name,
get_application_type(request_info->application_type),
strlen(request_info->payload),
request_info->payload));
}
} else if (use_host) {
LWIP_ASSERT("server_name != NULL", server_name != NULL);
return snprintf(buffer, buffer_size, HTTPC_POST_REQ_11_HOST_FORMAT(
uri,
server_name,
get_application_type(request_info->application_type),
strlen(request_info->payload),
request_info->payload));
} else {
return snprintf(buffer, buffer_size, HTTPC_POST_REQ_11_FORMAT(
uri,
get_application_type(request_info->application_type),
strlen(request_info->payload),
request_info->payload));
}
} else {
LWIP_ASSERT("request_type not supported", 0);
}
}

The http_client.h is changed as follows:

// Intended left the existing prototypes of httpc_get UNCHANGED
err_t httpc_get_file(const ip_addr_t* server_addr, u16_t port, const char* uri, const httpc_connection_t *settings,
altcp_recv_fn recv_fn, void* callback_arg, httpc_state_t **connection);
err_t httpc_get_file_dns(const char* server_name, u16_t port, const char* uri, const httpc_connection_t *settings,
altcp_recv_fn recv_fn, void* callback_arg, httpc_state_t **connection);

// Added httpc_post prototypes (and can add put/delete as well)
err_t httpc_post_file(const ip_addr_t* server_addr, u16_t port, const char* uri, httpc_request_info_t* post_info,
const httpc_connection_t *settings, altcp_recv_fn recv_fn, void* callback_arg, httpc_state_t **connection);
err_t httpc_post_file_dns(const char* server_name, u16_t port, const char* uri, httpc_request_info_t* post_info,
const httpc_connection_t *settings, altcp_recv_fn recv_fn, void* callback_arg, httpc_state_t **connection);



It works, I just tested it on my imx board.

Some discussion points:
- I created an enum for the application type of the post message. For now, I only support json (=application/json) (since that's what I need ;-)). Should I add more? I don't know what the most interesting ones are to add.
- Are the application types always supported by POST/PUT? Like application/json will be OK for both POST and PUT. Are there others that are only allowed for POST / PUT?
- The "to be sent data" is incorporated in the header. This will potentially make the header pretty large. Is that OK? Or should there be a different mechanism in place to make lwip send larger files to the server in chunks? Or will lwip deal with that in the lwip core?
- Or should I just make a pull request and let you guys nuke it with comments? :)

Best regards, Bas



Op za 2 jul. 2022 om 08:47 schreef Bas Prins <bas.prins3@gmail.com>:
Hi!
Thx a lot. Was't that certain I just needed the extra confirmation that it was as simple as extending the client. Will give this a go after my holidays. I will figure out how to make a pull request after.

Thanks! 

Op za 2 jul. 2022 00:34 schreef Grant Edwards <grant.b.edwards@gmail.com>:
On 2022-07-01, Simon Goldschmidt <goldsimon@gmx.de> wrote:

>>I'd really appreciate some feedback on this. Would it make sense to
>>extend the http_client.c in such a way so that I can choose whether
>>to create HTTP_GET or HTTP_POST requests?
>
> Yes.
>

>>err_t httpc_get_file_dns(const char* server_name, u16_t port, const
>>char* uri, const httpc_connection_t *settings, altcp_recv_fn
>>recv_fn, void* callback_arg, httpc_state_t **connection);
>>
>>For example add parameters to that signature to specify the request
>>type?  GET/POST + optional post data ?
>
> No, that function is called GET. Add a function called POST and add
> the new code there.

Might as well add PUT at the same time?

Maybe DELETE also?

--
Grant



_______________________________________________
lwip-users mailing list
lwip-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/lwip-users

reply via email to

[Prev in Thread] Current Thread [Next in Thread]