http_static: fix http(s) redirects 53/37753/2
authorDave Barach <dave@barachs.net>
Tue, 6 Dec 2022 13:39:29 +0000 (08:39 -0500)
committerDave Barach <dave@barachs.net>
Tue, 6 Dec 2022 16:25:15 +0000 (11:25 -0500)
Add an http redirect template to generate correct-looking "301 Moved
Permanently" replies.

Supply a default value of 1<<31 for the use_ptr_thresh config parameter.

Expose hss_session_get() so friend plugins which register GET / POST
handlers with the http_static server can add data to the session fifos.

Type: fix

Signed-off-by: Dave Barach <dave@barachs.net>
Change-Id: Ie1452eaf61c6f67311fbab092bc1fe03050bf94f

src/plugins/http/http.c
src/plugins/http/http.h
src/plugins/http_static/http_static.h
src/plugins/http_static/static_server.c

index 048b403..27801e8 100644 (file)
@@ -324,10 +324,12 @@ static const char *http_error_template = "HTTP/1.1 %s\r\n"
                                         "Pragma: no-cache\r\n"
                                         "Content-Length: 0\r\n\r\n";
 
+static const char *http_redirect_template = "HTTP/1.1 %s\r\n";
+
 /**
  * http response boilerplate
  */
-static const char *http_response_template = "HTTP/1.1 200 OK\r\n"
+static const char *http_response_template = "HTTP/1.1 %s\r\n"
                                            "Date: %U GMT\r\n"
                                            "Expires: %U GMT\r\n"
                                            "Server: VPP Static\r\n"
@@ -541,9 +543,14 @@ state_srv_wait_app (http_conn_t *hc, transport_send_params_t *sp)
       goto error;
     }
 
-  if (msg.code != HTTP_STATUS_OK)
+  ec = msg.code;
+
+  switch (msg.code)
     {
-      ec = msg.code;
+    case HTTP_STATUS_OK:
+    case HTTP_STATUS_MOVED:
+      break;
+    default:
       goto error;
     }
 
@@ -558,15 +565,29 @@ state_srv_wait_app (http_conn_t *hc, transport_send_params_t *sp)
    * - data length
    */
   now = clib_timebase_now (&hm->timebase);
-  header = format (0, http_response_template,
-                  /* Date */
-                  format_clib_timebase_time, now,
-                  /* Expires */
-                  format_clib_timebase_time, now + 600.0,
-                  /* Content type */
-                  http_content_type_str[msg.content_type],
-                  /* Length */
-                  msg.data.len);
+
+  switch (msg.code)
+    {
+    case HTTP_STATUS_OK:
+      header =
+       format (0, http_response_template, http_status_code_str[msg.code],
+               /* Date */
+               format_clib_timebase_time, now,
+               /* Expires */
+               format_clib_timebase_time, now + 600.0,
+               /* Content type */
+               http_content_type_str[msg.content_type],
+               /* Length */
+               msg.data.len);
+      break;
+    case HTTP_STATUS_MOVED:
+      header =
+       format (0, http_redirect_template, http_status_code_str[msg.code]);
+      /* Location: http(s)://new-place already queued up as data */
+      break;
+    default:
+      goto error;
+    }
 
   offset = send_data (hc, header, vec_len (header), 0);
   if (offset != vec_len (header))
index 04272ae..b11e0da 100644 (file)
@@ -95,6 +95,7 @@ typedef enum http_content_type_
 
 #define foreach_http_status_code                                              \
   _ (200, OK, "200 OK")                                                       \
+  _ (301, MOVED, "301 Moved Permanently")                                     \
   _ (400, BAD_REQUEST, "400 Bad Request")                                     \
   _ (404, NOT_FOUND, "404 Not Found")                                         \
   _ (405, METHOD_NOT_ALLOWED, "405 Method Not Allowed")                       \
index c754337..4e038ed 100644 (file)
@@ -163,6 +163,7 @@ void hss_register_url_handler (hss_url_handler_fn fp, const char *url,
                               http_req_method_t type);
 void hss_session_send_data (hss_url_handler_args_t *args);
 void hss_builtinurl_json_handlers_init (void);
+hss_session_t *hss_session_get (u32 thread_index, u32 hs_index);
 
 #endif /* __included_http_static_h__ */
 
index cd36be7..a4c62fc 100644 (file)
@@ -41,7 +41,7 @@ hss_session_alloc (u32 thread_index)
   return hs;
 }
 
-static hss_session_t *
+__clib_export hss_session_t *
 hss_session_get (u32 thread_index, u32 hs_index)
 {
   hss_main_t *hsm = &hss_main;
@@ -261,7 +261,6 @@ try_index_file (hss_main_t *hsm, hss_session_t *hs, u8 *path)
 
   redirect =
     format (0,
-           "HTTP/1.1 301 Moved Permanently\r\n"
            "Location: http%s://%U%s%s\r\n\r\n",
            proto == TRANSPORT_PROTO_TLS ? "s" : "", format_ip46_address,
            &endpt.ip, endpt.is_ip4, print_port ? port_str : (u8 *) "", path);
@@ -275,7 +274,7 @@ try_index_file (hss_main_t *hsm, hss_session_t *hs, u8 *path)
   hs->data_len = vec_len (redirect);
   hs->free_data = 1;
 
-  return HTTP_STATUS_OK;
+  return HTTP_STATUS_MOVED;
 }
 
 static int