webclient: Allow users to specify extra headers for proxy

My primary motivation at this point is to use it for basic proxy auth.
(specify "Proxy-Authorization" header)
But there can be other use cases for proxy-specific extra headers.

If/when we want to support other non-trivial auth methods, probably
a callback-based mechanism will be necessary. But at this point,
this serves my purpose well.
This commit is contained in:
YAMAMOTO Takashi 2022-06-06 16:00:37 +09:00 committed by Xiang Xiao
parent 068905d07b
commit 159ca00752
2 changed files with 38 additions and 3 deletions

View File

@ -360,6 +360,10 @@ struct webclient_context
* unix_socket_path - If not NULL, the path to an AF_LOCAL socket.
* headers - An array of pointers to the extra headers.
* nheaders - The number of elements in the "headers" array.
* proxy_headers - An array of pointers to the extra headers for
* the proxy connection.
* proxy_nheaders - The number of elements in the "headers" array for
* the proxy connection.
* bodylen - The size of the request body.
* timeout_sec - The timeout in second.
* This is not meant to cover the entire transaction.
@ -384,6 +388,10 @@ struct webclient_context
#endif
FAR const char * FAR const *headers;
unsigned int nheaders;
FAR const char * FAR const *proxy_headers;
unsigned int proxy_nheaders;
size_t bodylen;
unsigned int timeout_sec;

View File

@ -1337,6 +1337,8 @@ int webclient_perform(FAR struct webclient_context *ctx)
tunnel->tunnel_target_host = ws->target.hostname;
tunnel->tunnel_target_port = ws->target.port;
tunnel->proxy = ctx->proxy;
tunnel->proxy_headers = ctx->proxy_headers;
tunnel->proxy_nheaders = ctx->proxy_nheaders;
/* Inherit some configurations.
*
@ -1700,10 +1702,35 @@ int webclient_perform(FAR struct webclient_context *ctx)
dest = append(dest, ep, ws->target.hostname);
dest = append(dest, ep, g_httpcrnl);
for (i = 0; i < ctx->nheaders; i++)
/* Append user-specified headers.
*
* - For non-proxied request,
* only send "headers".
*
* - For proxied request, (traditional http proxy)
* send both of "headers" and "proxy_headers".
*
* - For tunneling request, (WEBCLIENT_FLAG_TUNNEL)
* only send "proxy_headers".
*/
if ((ctx->flags & WEBCLIENT_FLAG_TUNNEL) == 0)
{
dest = append(dest, ep, ctx->headers[i]);
dest = append(dest, ep, g_httpcrnl);
for (i = 0; i < ctx->nheaders; i++)
{
dest = append(dest, ep, ctx->headers[i]);
dest = append(dest, ep, g_httpcrnl);
}
}
if ((ctx->flags & WEBCLIENT_FLAG_TUNNEL) != 0 ||
ctx->proxy != NULL)
{
for (i = 0; i < ctx->proxy_nheaders; i++)
{
dest = append(dest, ep, ctx->proxy_headers[i]);
dest = append(dest, ep, g_httpcrnl);
}
}
if (ctx->bodylen)