Re: [PATCH 3/4] httpd: Support caching via 'ETag:' header

2020-07-11 Thread Sergey Ponomarev
Hi Xabier, thanks for the review. Unfortunately I don't have time anymore
so if it is possible just remove the line after merge. As far

On Sat, 11 Jul 2020 at 18:46, Xabier Oneca -- xOneca 
wrote:

> Hi Sergey,
>
> Nice patch. Just one comment below:
>
> [...]
> > diff --git a/networking/httpd.c b/networking/httpd.c
> > index 97b61fb77..50a837229 100644
> > --- a/networking/httpd.c
> > +++ b/networking/httpd.c
> > @@ -1699,6 +1725,14 @@ static NOINLINE void send_file_and_exit(const
> char *url, int what)
> > send_headers_and_exit(HTTP_NOT_FOUND);
> > log_and_exit();
> > }
> > +#if ENABLE_FEATURE_HTTPD_CACHE
> > +   if (DEBUG)
> > +   bb_perror_msg("Not modified '%s'", url);
> > +   if (modified_since == last_mod) {
> > +   send_headers_and_exit(HTTP_NOT_MODIFIED);
> > +   log_and_exit();
>
> Here this log_and_exit() is redundant. It is already called from
> send_headers_and_exit().
>
> Cheers,
>
> Xabier Oneca_,,_
>


-- 
Sergey Ponomarev , skype:stokito
___
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox


Re: [PATCH 3/4] httpd: Support caching via 'ETag:' header

2020-07-11 Thread Xabier Oneca -- xOneca
Hi Sergey,

Nice patch. Just one comment below:

[...]
> diff --git a/networking/httpd.c b/networking/httpd.c
> index 97b61fb77..50a837229 100644
> --- a/networking/httpd.c
> +++ b/networking/httpd.c
> @@ -1699,6 +1725,14 @@ static NOINLINE void send_file_and_exit(const char 
> *url, int what)
> send_headers_and_exit(HTTP_NOT_FOUND);
> log_and_exit();
> }
> +#if ENABLE_FEATURE_HTTPD_CACHE
> +   if (DEBUG)
> +   bb_perror_msg("Not modified '%s'", url);
> +   if (modified_since == last_mod) {
> +   send_headers_and_exit(HTTP_NOT_MODIFIED);
> +   log_and_exit();

Here this log_and_exit() is redundant. It is already called from
send_headers_and_exit().

Cheers,

Xabier Oneca_,,_
___
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox


[PATCH 3/4] httpd: Support caching via 'ETag:' header

2020-07-09 Thread Sergey Ponomarev
If server returned ETag in response then next time client (browser) will add it 
to request into If-None-Match header. Then httpd will check if file wasn't 
modified and if not return 304 Not Modified status code. The ETag value is 
itself the last modification date in unix epoch.

Signed-off-by: Sergey Ponomarev 
---
 networking/httpd.c | 49 +++---
 1 file changed, 46 insertions(+), 3 deletions(-)

diff --git a/networking/httpd.c b/networking/httpd.c
index 97b61fb77..50a837229 100644
--- a/networking/httpd.c
+++ b/networking/httpd.c
@@ -214,6 +214,14 @@
 //config:  help
 //config:  Makes httpd send files using GZIP content encoding if the
 //config:  client supports it and a pre-compressed .gz exists.
+//config:
+//config:config FEATURE_HTTPD_CACHE
+//config:  bool "Support caching via 'ETag:' header"
+//config:  default y
+//config:  depends on HTTPD
+//config:  help
+//config:  Enable caching Conditional GET requests via "ETag" and 
If-None-Match" headers.
+//config:  The ETag value is simply file's last modification date so not 
completely reliable.
 
 //applet:IF_HTTPD(APPLET(httpd, BB_DIR_USR_SBIN, BB_SUID_DROP))
 
@@ -310,6 +318,7 @@ enum {
HTTP_OK = 200,
HTTP_PARTIAL_CONTENT = 206,
HTTP_MOVED_TEMPORARILY = 302,
+   HTTP_NOT_MODIFIED = 304,
HTTP_BAD_REQUEST = 400,   /* malformed syntax */
HTTP_UNAUTHORIZED = 401, /* authentication needed, respond with auth 
hdr */
HTTP_NOT_FOUND = 404,
@@ -327,7 +336,6 @@ enum {
HTTP_NO_CONTENT = 204,
HTTP_MULTIPLE_CHOICES = 300,
HTTP_MOVED_PERMANENTLY = 301,
-   HTTP_NOT_MODIFIED = 304,
HTTP_PAYMENT_REQUIRED = 402,
HTTP_BAD_GATEWAY = 502,
HTTP_SERVICE_UNAVAILABLE = 503, /* overload, maintenance */
@@ -340,6 +348,9 @@ static const uint16_t http_response_type[] ALIGN2 = {
HTTP_PARTIAL_CONTENT,
 #endif
HTTP_MOVED_TEMPORARILY,
+#if ENABLE_FEATURE_HTTPD_CACHE
+   HTTP_NOT_MODIFIED,
+#endif
HTTP_REQUEST_TIMEOUT,
HTTP_NOT_IMPLEMENTED,
 #if ENABLE_FEATURE_HTTPD_BASIC_AUTH
@@ -356,7 +367,6 @@ static const uint16_t http_response_type[] ALIGN2 = {
HTTP_NO_CONTENT,
HTTP_MULTIPLE_CHOICES,
HTTP_MOVED_PERMANENTLY,
-   HTTP_NOT_MODIFIED,
HTTP_BAD_GATEWAY,
HTTP_SERVICE_UNAVAILABLE,
 #endif
@@ -371,6 +381,9 @@ static const struct {
{ "Partial Content", NULL },
 #endif
{ "Found", NULL },
+#if ENABLE_FEATURE_HTTPD_CACHE
+   { "Not Modified" },
+#endif
{ "Request Timeout", "No request appeared within 60 seconds" },
{ "Not Implemented", "The requested method is not recognized" },
 #if ENABLE_FEATURE_HTTPD_BASIC_AUTH
@@ -387,7 +400,6 @@ static const struct {
{ "No Content" },
{ "Multiple Choices" },
{ "Moved Permanently" },
-   { "Not Modified" },
{ "Bad Gateway", "" },
{ "Service Unavailable", "" },
 #endif
@@ -400,7 +412,10 @@ struct globals {
/* client can handle gzip / we are going to send gzip */
smallint content_gzip;
 #endif
+#if ENABLE_FEATURE_HTTPD_CACHE
+   time_t modified_since;
time_t last_mod;
+#endif
char *rmt_ip_str;   /* for $REMOTE_ADDR and $REMOTE_PORT */
const char *bind_addr_or_port;
 
@@ -457,7 +472,10 @@ struct globals {
 #define index_page(G.index_page   )
 #define found_mime_type   (G.found_mime_type  )
 #define found_moved_temporarily (G.found_moved_temporarily)
+#if ENABLE_FEATURE_HTTPD_CACHE
+# define modified_since   (G.modified_since   )
 #define last_mod  (G.last_mod )
+#endif
 #define ip_a_d(G.ip_a_d   )
 #define g_realm   (G.g_realm  )
 #define remoteuser(G.remoteuser   )
@@ -486,6 +504,7 @@ enum {
setup_common_bufsiz(); \
SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
IF_FEATURE_HTTPD_BASIC_AUTH(g_realm = "Web Server Authentication";) \
+   IF_FEATURE_HTTPD_CACHE(modified_since = 0;) \
IF_FEATURE_HTTPD_RANGES(range_start = -1;) \
bind_addr_or_port = "80"; \
index_page = index_html; \
@@ -1166,8 +1185,10 @@ static void send_headers(unsigned responseNum)
 #if ENABLE_FEATURE_HTTPD_RANGES
"Accept-Ranges: bytes\r\n"
 #endif
+#if ENABLE_FEATURE_HTTPD_CACHE
/* Instead of Last-Modified formatted date send it as a 
plain unix timestamp via ETag */
"ETag: %ld\r\n"
+#endif
/* Because of 4.4 (5), we can forgo sending of "Content-Length"
 * since we close connection afterwards, but it helps clients
 * to e.g. estimate download times, show progress bars etc.
@@ -1175,7 +1196,9 @@ static void send_headers(unsigned responseNum)
 * but de-facto standard is to send it (see comment below).
 */
"Content-Length: %"OFF_FMT"u\r\n",
+#if EN