[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    Re: [PATCH v2] httpd: Pass custom HTTP headers to CGI scripts
From:       Alexander Vickberg <wickbergster () gmail ! com>
Date:       2019-03-30 18:40:54
Message-ID: CAAxSGfYeVtLCK6CjM2n-J+VXv10WDMtyj9_YCNCUBssY75tztw () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Hi Xabier,

Thank you for your comments.

> > diff --git a/networking/httpd.c b/networking/httpd.c
> > index b52526a78..c27cd3001 100644
> > --- a/networking/httpd.c
> > +++ b/networking/httpd.c
> > @@ -2301,32 +2306,8 @@ static void handle_incoming_and_exit(const
len_and_sockaddr *fromAddr)
> >   }
> >   }
> >  #endif
> > -#if ENABLE_FEATURE_HTTPD_CGI
> > - else if (STRNCASECMP(iobuf, "Cookie:") == 0) {
> > - if (!cookie) /* in case they send millions of these, do not OOM */
> > - cookie = xstrdup(skip_whitespace(iobuf + sizeof("Cookie:")-1));
> > - } else if (STRNCASECMP(iobuf, "Content-Type:") == 0) {
> > - if (!content_type)
> > - content_type = xstrdup(skip_whitespace(iobuf +
sizeof("Content-Type:")-1));
> > - } else if (STRNCASECMP(iobuf, "Referer:") == 0) {
> > - if (!G.referer)
> > - G.referer = xstrdup(skip_whitespace(iobuf + sizeof("Referer:")-1));
> > - } else if (STRNCASECMP(iobuf, "User-Agent:") == 0) {
> > - if (!G.user_agent)
> > - G.user_agent = xstrdup(skip_whitespace(iobuf +
sizeof("User-Agent:")-1));
> > - } else if (STRNCASECMP(iobuf, "Host:") == 0) {
> > - if (!G.host)
> > - G.host = xstrdup(skip_whitespace(iobuf + sizeof("Host:")-1));
> > - } else if (STRNCASECMP(iobuf, "Accept:") == 0) {
> > - if (!G.http_accept)
> > - G.http_accept = xstrdup(skip_whitespace(iobuf + sizeof("Accept:")-1));
> > - } else if (STRNCASECMP(iobuf, "Accept-Language:") == 0) {
> > - if (!G.http_accept_language)
> > - G.http_accept_language = xstrdup(skip_whitespace(iobuf +
sizeof("Accept-Language:")-1));
> > - }
> > -#endif
> >  #if ENABLE_FEATURE_HTTPD_BASIC_AUTH
> > - if (STRNCASECMP(iobuf, "Authorization:") == 0) {
> > + else if (STRNCASECMP(iobuf, "Authorization:") == 0) {
>
> You cannot convert this to 'else if'.

Doh.. Do you think the approach in my first version would be acceptable?

>
> >   /* We only allow Basic credentials.
> >   * It shows up as "Authorization: Basic <user>:<passwd>" where
> >   * "<user>:<passwd>" is base64 encoded.
> > @@ -2341,7 +2322,7 @@ static void handle_incoming_and_exit(const
len_and_sockaddr *fromAddr)
> >   }
> >  #endif
> >  #if ENABLE_FEATURE_HTTPD_RANGES
> > - if (STRNCASECMP(iobuf, "Range:") == 0) {
> > + else if (STRNCASECMP(iobuf, "Range:") == 0) {
>
> ... Same here.
>
> >   /* We know only bytes=NNN-[MMM] */
> >   char *s = skip_whitespace(iobuf + sizeof("Range:")-1);
> >   if (is_prefixed_with(s, "bytes=")) {
> > @@ -2358,7 +2339,7 @@ static void handle_incoming_and_exit(const
len_and_sockaddr *fromAddr)
> >   }
> >  #endif
> >  #if ENABLE_FEATURE_HTTPD_GZIP
> > - if (STRNCASECMP(iobuf, "Accept-Encoding:") == 0) {
> > + else if (STRNCASECMP(iobuf, "Accept-Encoding:") == 0) {
>
> ... And here.
>
> >   /* Note: we do not support "gzip;q=0"
> >   * method of _disabling_ gzip
> >   * delivery. No one uses that, though */
> > @@ -2373,6 +2354,46 @@ static void handle_incoming_and_exit(const
len_and_sockaddr *fromAddr)
> >   //}
> >   }
> >   }
> > +#endif
> > +#if ENABLE_FEATURE_HTTPD_CGI
> > + else if (STRNCASECMP(iobuf, "Content-Type:") == 0) {
> > + if (!content_type)
> > + content_type = xstrdup(skip_whitespace(iobuf +
sizeof("Content-Type:")-1));
> > + } else {
> > + HTTP_Header *cur;
> > + char *after_colon = strchr(iobuf, ':');
> > + char *ch = iobuf;
> > +
> > + if (!after_colon)
> > + continue;
> > +
> > + cur = xmalloc(sizeof(HTTP_Header));
> > + *after_colon++ = '\0';
> > +
> > + while (*ch) {
> > + if (isalpha(*ch))
> > + *ch &= ~0x20;
> > + else if (!isdigit(*ch))
> > + *ch = '_';
> > + ch++;
> > + }
> > +
> > + cur->name = xmalloc(sizeof("HTTP_")+strlen(iobuf));
> > + memcpy(cur->name, "HTTP_", sizeof("HTTP_")-1);
> > + strcpy(cur->name + sizeof("HTTP_")-1, iobuf);
>
> Why not just:
>
> cur->name = xasprintf("HTTP_%s", iobuf)?

I thought my version would be faster.

> > + cur->value = xstrdup(skip_whitespace(after_colon));
> > +
> > + /* Insert new header into header list */
> > + if (!G.hdr_list) {
> > + G.hdr_list = cur;
> > + cur->next = G.hdr_list;
> > + last = cur;
> > + } else {
> > + cur->next = G.hdr_list;
> > + last->next = cur;
> > + last = cur;
> > + }
> > + }
> >  #endif
> >   } /* while extra header reading */
> >   }
>
> Cheers,
>
> Xabier Oneca_,,_
>
> P.S. Sorry. Gmail ate indentation... :/

/Alexander

[Attachment #5 (text/html)]

<div dir="ltr">Hi Xabier,<div><br></div><div>Thank you for your \
comments.</div><div><br>&gt; &gt; diff --git a/networking/httpd.c \
b/networking/httpd.c<br>&gt; &gt; index b52526a78..c27cd3001 100644<br>&gt; &gt; --- \
a/networking/httpd.c<br>&gt; &gt; +++ b/networking/httpd.c<br>&gt; &gt; @@ -2301,32 \
+2306,8 @@ static void handle_incoming_and_exit(const len_and_sockaddr \
*fromAddr)<br>&gt; &gt;    }<br>&gt; &gt;    }<br>&gt; &gt;   #endif<br>&gt; &gt; \
-#if ENABLE_FEATURE_HTTPD_CGI<br>&gt; &gt; - else if (STRNCASECMP(iobuf, \
&quot;Cookie:&quot;) == 0) {<br>&gt; &gt; - if (!cookie) /* in case they send \
millions of these, do not OOM */<br>&gt; &gt; - cookie = \
xstrdup(skip_whitespace(iobuf + sizeof(&quot;Cookie:&quot;)-1));<br>&gt; &gt; - } \
else if (STRNCASECMP(iobuf, &quot;Content-Type:&quot;) == 0) {<br>&gt; &gt; - if \
(!content_type)<br>&gt; &gt; - content_type = xstrdup(skip_whitespace(iobuf + \
sizeof(&quot;Content-Type:&quot;)-1));<br>&gt; &gt; - } else if (STRNCASECMP(iobuf, \
&quot;Referer:&quot;) == 0) {<br>&gt; &gt; - if (!G.referer)<br>&gt; &gt; - G.referer \
= xstrdup(skip_whitespace(iobuf + sizeof(&quot;Referer:&quot;)-1));<br>&gt; &gt; - } \
else if (STRNCASECMP(iobuf, &quot;User-Agent:&quot;) == 0) {<br>&gt; &gt; - if \
(!G.user_agent)<br>&gt; &gt; - G.user_agent = xstrdup(skip_whitespace(iobuf + \
sizeof(&quot;User-Agent:&quot;)-1));<br>&gt; &gt; - } else if (STRNCASECMP(iobuf, \
&quot;Host:&quot;) == 0) {<br>&gt; &gt; - if (!G.host)<br>&gt; &gt; - G.host = \
xstrdup(skip_whitespace(iobuf + sizeof(&quot;Host:&quot;)-1));<br>&gt; &gt; - } else \
if (STRNCASECMP(iobuf, &quot;Accept:&quot;) == 0) {<br>&gt; &gt; - if \
(!G.http_accept)<br>&gt; &gt; - G.http_accept = xstrdup(skip_whitespace(iobuf + \
sizeof(&quot;Accept:&quot;)-1));<br>&gt; &gt; - } else if (STRNCASECMP(iobuf, \
&quot;Accept-Language:&quot;) == 0) {<br>&gt; &gt; - if \
(!G.http_accept_language)<br>&gt; &gt; - G.http_accept_language = \
xstrdup(skip_whitespace(iobuf + sizeof(&quot;Accept-Language:&quot;)-1));<br>&gt; \
&gt; - }<br>&gt; &gt; -#endif<br>&gt; &gt;   #if \
ENABLE_FEATURE_HTTPD_BASIC_AUTH<br>&gt; &gt; - if (STRNCASECMP(iobuf, \
&quot;Authorization:&quot;) == 0) {<br>&gt; &gt; + else if (STRNCASECMP(iobuf, \
&quot;Authorization:&quot;) == 0) {<br>&gt;<br>&gt; You cannot convert this to \
&#39;else if&#39;.</div><div><br></div><div>Doh.. Do you think the approach in my \
first version would be acceptable?</div><div><br>&gt;<br>&gt; &gt;    /* We only \
allow Basic credentials.<br>&gt; &gt;    * It shows up as &quot;Authorization: Basic \
&lt;user&gt;:&lt;passwd&gt;&quot; where<br>&gt; &gt;    * \
&quot;&lt;user&gt;:&lt;passwd&gt;&quot; is base64 encoded.<br>&gt; &gt; @@ -2341,7 \
+2322,7 @@ static void handle_incoming_and_exit(const len_and_sockaddr \
*fromAddr)<br>&gt; &gt;    }<br>&gt; &gt;   #endif<br>&gt; &gt;   #if \
ENABLE_FEATURE_HTTPD_RANGES<br>&gt; &gt; - if (STRNCASECMP(iobuf, &quot;Range:&quot;) \
== 0) {<br>&gt; &gt; + else if (STRNCASECMP(iobuf, &quot;Range:&quot;) == 0) \
{<br>&gt;<br>&gt; ... Same here.<br>&gt;<br>&gt; &gt;    /* We know only \
bytes=NNN-[MMM] */<br>&gt; &gt;    char *s = skip_whitespace(iobuf + \
sizeof(&quot;Range:&quot;)-1);<br>&gt; &gt;    if (is_prefixed_with(s, \
&quot;bytes=&quot;)) {<br>&gt; &gt; @@ -2358,7 +2339,7 @@ static void \
handle_incoming_and_exit(const len_and_sockaddr *fromAddr)<br>&gt; &gt;    }<br>&gt; \
&gt;   #endif<br>&gt; &gt;   #if ENABLE_FEATURE_HTTPD_GZIP<br>&gt; &gt; - if \
(STRNCASECMP(iobuf, &quot;Accept-Encoding:&quot;) == 0) {<br>&gt; &gt; + else if \
(STRNCASECMP(iobuf, &quot;Accept-Encoding:&quot;) == 0) {<br>&gt;<br>&gt; ... And \
here.<br>&gt;<br>&gt; &gt;    /* Note: we do not support &quot;gzip;q=0&quot;<br>&gt; \
&gt;    * method of _disabling_ gzip<br>&gt; &gt;    * delivery. No one uses that, \
though */<br>&gt; &gt; @@ -2373,6 +2354,46 @@ static void \
handle_incoming_and_exit(const len_and_sockaddr *fromAddr)<br>&gt; &gt;    \
//}<br>&gt; &gt;    }<br>&gt; &gt;    }<br>&gt; &gt; +#endif<br>&gt; &gt; +#if \
ENABLE_FEATURE_HTTPD_CGI<br>&gt; &gt; + else if (STRNCASECMP(iobuf, \
&quot;Content-Type:&quot;) == 0) {<br>&gt; &gt; + if (!content_type)<br>&gt; &gt; + \
content_type = xstrdup(skip_whitespace(iobuf + \
sizeof(&quot;Content-Type:&quot;)-1));<br>&gt; &gt; + } else {<br>&gt; &gt; + \
HTTP_Header *cur;<br>&gt; &gt; + char *after_colon = strchr(iobuf, \
&#39;:&#39;);<br>&gt; &gt; + char *ch = iobuf;<br>&gt; &gt; +<br>&gt; &gt; + if \
(!after_colon)<br>&gt; &gt; + continue;<br>&gt; &gt; +<br>&gt; &gt; + cur = \
xmalloc(sizeof(HTTP_Header));<br>&gt; &gt; + *after_colon++ = &#39;\0&#39;;<br>&gt; \
&gt; +<br>&gt; &gt; + while (*ch) {<br>&gt; &gt; + if (isalpha(*ch))<br>&gt; &gt; + \
*ch &amp;= ~0x20;<br>&gt; &gt; + else if (!isdigit(*ch))<br>&gt; &gt; + *ch = \
&#39;_&#39;;<br>&gt; &gt; + ch++;<br>&gt; &gt; + }<br>&gt; &gt; +<br>&gt; &gt; + \
cur-&gt;name = xmalloc(sizeof(&quot;HTTP_&quot;)+strlen(iobuf));<br>&gt; &gt; + \
memcpy(cur-&gt;name, &quot;HTTP_&quot;, sizeof(&quot;HTTP_&quot;)-1);<br>&gt; &gt; + \
strcpy(cur-&gt;name + sizeof(&quot;HTTP_&quot;)-1, iobuf);<br>&gt;<br>&gt; Why not \
just:<br>&gt;<br>&gt; cur-&gt;name = xasprintf(&quot;HTTP_%s&quot;, iobuf)?<br><br>I \
thought my version would be faster.<br><br>&gt; &gt; + cur-&gt;value = \
xstrdup(skip_whitespace(after_colon));<br>&gt; &gt; +<br>&gt; &gt; + /* Insert new \
header into header list */<br>&gt; &gt; + if (!G.hdr_list) {<br>&gt; &gt; + \
G.hdr_list = cur;<br>&gt; &gt; + cur-&gt;next = G.hdr_list;<br>&gt; &gt; + last = \
cur;<br>&gt; &gt; + } else {<br>&gt; &gt; + cur-&gt;next = G.hdr_list;<br>&gt; &gt; + \
last-&gt;next = cur;<br>&gt; &gt; + last = cur;<br>&gt; &gt; + }<br>&gt; &gt; + \
}<br>&gt; &gt;   #endif<br>&gt; &gt;    } /* while extra header reading */<br>&gt; \
&gt;    }<br>&gt;<br>&gt; Cheers,<br>&gt;<br>&gt; Xabier Oneca_,,_<br>&gt;<br>&gt; \
P.S. Sorry. Gmail ate indentation... \
:/</div><div><br></div><div>/Alexander</div></div>



_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic