]> granicus.if.org Git - apache/blobdiff - server/util_script.c
Log the value of Status header lines in script responses rather than
[apache] / server / util_script.c
index 2008beeb6a6e3975d521f6e6b4c840d6ae5b049f..703d16009472b34d73e0b36e64c9c9d63380dfa9 100644 (file)
  * where there don't have to be).
  */
 
-#define MALFORMED_MESSAGE "malformed header from script. Bad header="
-#define MALFORMED_HEADER_LENGTH_TO_SHOW 30
-
-APLOG_USE_MODULE(core);
+/* we know core's module_index is 0 */
+#undef APLOG_MODULE_INDEX
+#define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
 
 static char *http2env(request_rec *r, const char *w)
 {
@@ -145,6 +144,7 @@ AP_DECLARE(void) ap_add_common_vars(request_rec *r)
     const apr_table_entry_t *hdrs = (const apr_table_entry_t *) hdrs_arr->elts;
     int i;
     apr_port_t rport;
+    char *q;
 
     /* use a temporary apr_table_t which we'll overlap onto
      * r->subprocess_env later
@@ -230,21 +230,28 @@ AP_DECLARE(void) ap_add_common_vars(request_rec *r)
     apr_table_addn(e, "SERVER_SIGNATURE", ap_psignature("", r));
     apr_table_addn(e, "SERVER_SOFTWARE", ap_get_server_banner());
     apr_table_addn(e, "SERVER_NAME",
-                   ap_escape_html(r->pool, ap_get_server_name(r)));
+                   ap_escape_html(r->pool, ap_get_server_name_for_url(r)));
     apr_table_addn(e, "SERVER_ADDR", r->connection->local_ip);  /* Apache */
     apr_table_addn(e, "SERVER_PORT",
                   apr_psprintf(r->pool, "%u", ap_get_server_port(r)));
     add_unless_null(e, "REMOTE_HOST",
                     ap_get_remote_host(c, r->per_dir_config, REMOTE_HOST, NULL));
-    apr_table_addn(e, "REMOTE_ADDR", c->remote_ip);
+    apr_table_addn(e, "REMOTE_ADDR", r->useragent_ip);
     apr_table_addn(e, "DOCUMENT_ROOT", ap_document_root(r));    /* Apache */
     apr_table_setn(e, "REQUEST_SCHEME", ap_http_scheme(r));
     apr_table_addn(e, "CONTEXT_PREFIX", ap_context_prefix(r));
     apr_table_addn(e, "CONTEXT_DOCUMENT_ROOT", ap_context_document_root(r));
     apr_table_addn(e, "SERVER_ADMIN", s->server_admin); /* Apache */
-    apr_table_addn(e, "SCRIPT_FILENAME", r->filename);  /* Apache */
+    if (apr_table_get(r->notes, "proxy-noquery") && (q = ap_strchr(r->filename, '?'))) {
+        *q = '\0';
+        apr_table_addn(e, "SCRIPT_FILENAME", apr_pstrdup(r->pool, r->filename));
+        *q = '?';
+    }
+    else {
+        apr_table_addn(e, "SCRIPT_FILENAME", r->filename);  /* Apache */
+    }
 
-    rport = c->remote_addr->port;
+    rport = c->client_addr->port;
     apr_table_addn(e, "REMOTE_PORT", apr_itoa(r->pool, rport));
 
     if (r->user) {
@@ -399,10 +406,12 @@ static int set_cookie_doo_doo(void *v, const char *key, const char *val)
 }
 
 #define HTTP_UNSET (-HTTP_OK)
+#define SCRIPT_LOG_MARK  __FILE__,__LINE__,module_index
 
-AP_DECLARE(int) ap_scan_script_header_err_core(request_rec *r, char *buffer,
+AP_DECLARE(int) ap_scan_script_header_err_core_ex(request_rec *r, char *buffer,
                                        int (*getsfunc) (char *, int, void *),
-                                       void *getsfunc_data)
+                                       void *getsfunc_data,
+                                       int module_index)
 {
     char x[MAX_STRING_LEN];
     char *w, *l;
@@ -410,6 +419,8 @@ AP_DECLARE(int) ap_scan_script_header_err_core(request_rec *r, char *buffer,
     int cgi_status = HTTP_UNSET;
     apr_table_t *merge;
     apr_table_t *cookie_table;
+    int trace_log = APLOG_R_MODULE_IS_LEVEL(r, module_index, APLOG_TRACE1);
+    int first_header = 1;
 
     if (buffer) {
         *buffer = '\0';
@@ -432,13 +443,16 @@ AP_DECLARE(int) ap_scan_script_header_err_core(request_rec *r, char *buffer,
 
         int rv = (*getsfunc) (w, MAX_STRING_LEN - 1, getsfunc_data);
         if (rv == 0) {
-            ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_TOCLIENT, 0, r,
-                          "Premature end of script headers: %s",
+            const char *msg = "Premature end of script headers";
+            if (first_header)
+                msg = "End of script output before headers";
+            ap_log_rerror(SCRIPT_LOG_MARK, APLOG_ERR|APLOG_TOCLIENT, 0, r,
+                          "%s: %s", msg,
                           apr_filepath_name_get(r->filename));
             return HTTP_INTERNAL_SERVER_ERROR;
         }
         else if (rv == -1) {
-            ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_TOCLIENT, 0, r,
+            ap_log_rerror(SCRIPT_LOG_MARK, APLOG_ERR|APLOG_TOCLIENT, 0, r,
                           "Script timed out before returning headers: %s",
                           apr_filepath_name_get(r->filename));
             return HTTP_GATEWAY_TIME_OUT;
@@ -477,7 +491,7 @@ AP_DECLARE(int) ap_scan_script_header_err_core(request_rec *r, char *buffer,
 
             /* PR#38070: This fails because it gets confused when a
              * CGI Status header overrides ap_meets_conditions.
-             * 
+             *
              * We can fix that by dropping ap_meets_conditions when
              * Status has been set.  Since this is the only place
              * cgi_status gets used, let's test it explicitly.
@@ -500,6 +514,14 @@ AP_DECLARE(int) ap_scan_script_header_err_core(request_rec *r, char *buffer,
             return cond_status;
         }
 
+        if (trace_log) {
+            if (first_header)
+                ap_log_rerror(SCRIPT_LOG_MARK, APLOG_TRACE4, 0, r,
+                              "Headers from script '%s':",
+                              apr_filepath_name_get(r->filename));
+            ap_log_rerror(SCRIPT_LOG_MARK, APLOG_TRACE4, 0, r, "  %s", w);
+        }
+
         /* if we see a bogus header don't ignore it. Shout and scream */
 
 #if APR_CHARSET_EBCDIC
@@ -519,7 +541,7 @@ AP_DECLARE(int) ap_scan_script_header_err_core(request_rec *r, char *buffer,
                     ++maybeASCII;
             }
             if (maybeASCII > maybeEBCDIC) {
-                ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
+                ap_log_error(SCRIPT_LOG_MARK, APLOG_ERR, 0, r->server,
                              "CGI Interface Error: Script headers apparently ASCII: (CGI = %s)",
                              r->filename);
                 inbytes_left = outbytes_left = cp - w;
@@ -529,22 +551,16 @@ AP_DECLARE(int) ap_scan_script_header_err_core(request_rec *r, char *buffer,
         }
 #endif /*APR_CHARSET_EBCDIC*/
         if (!(l = strchr(w, ':'))) {
-            char malformed[(sizeof MALFORMED_MESSAGE) + 1
-                           + MALFORMED_HEADER_LENGTH_TO_SHOW];
-
-            strcpy(malformed, MALFORMED_MESSAGE);
-            strncat(malformed, w, MALFORMED_HEADER_LENGTH_TO_SHOW);
-
             if (!buffer) {
                 /* Soak up all the script output - may save an outright kill */
-                while ((*getsfunc) (w, MAX_STRING_LEN - 1, getsfunc_data)) {
+                while ((*getsfunc)(w, MAX_STRING_LEN - 1, getsfunc_data) > 0) {
                     continue;
                 }
             }
 
-            ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_TOCLIENT, 0, r,
-                          "%s: %s", malformed,
-                          apr_filepath_name_get(r->filename));
+            ap_log_rerror(SCRIPT_LOG_MARK, APLOG_ERR|APLOG_TOCLIENT, 0, r,
+                          "malformed header from script '%s': Bad header: %.30s",
+                          apr_filepath_name_get(r->filename), w);
             return HTTP_INTERNAL_SERVER_ERROR;
         }
 
@@ -573,6 +589,14 @@ AP_DECLARE(int) ap_scan_script_header_err_core(request_rec *r, char *buffer,
          */
         else if (!strcasecmp(w, "Status")) {
             r->status = cgi_status = atoi(l);
+            if (!ap_is_HTTP_VALID_RESPONSE(cgi_status))
+                ap_log_rerror(SCRIPT_LOG_MARK, APLOG_ERR|APLOG_TOCLIENT, 0, r,
+                              "Invalid status line from script '%s': %s",
+                              apr_filepath_name_get(r->filename), l);
+            else
+                ap_log_rerror(SCRIPT_LOG_MARK, APLOG_TRACE1, 0, r,
+                              "Status line from script '%s': %s",
+                              apr_filepath_name_get(r->filename), l);
             r->status_line = apr_pstrdup(r->pool, l);
         }
         else if (!strcasecmp(w, "Location")) {
@@ -604,11 +628,21 @@ AP_DECLARE(int) ap_scan_script_header_err_core(request_rec *r, char *buffer,
         else {
             apr_table_add(merge, w, l);
         }
+        first_header = 0;
     }
     /* never reached - we leave this function within the while loop above */
     return OK;
 }
 
+AP_DECLARE(int) ap_scan_script_header_err_core(request_rec *r, char *buffer,
+                                       int (*getsfunc) (char *, int, void *),
+                                       void *getsfunc_data)
+{
+    return ap_scan_script_header_err_core_ex(r, buffer, getsfunc,
+                                             getsfunc_data,
+                                             APLOG_MODULE_INDEX);
+}
+
 static int getsfunc_FILE(char *buf, int len, void *f)
 {
     return apr_file_gets(buf, len, (apr_file_t *) f) == APR_SUCCESS;
@@ -617,9 +651,18 @@ static int getsfunc_FILE(char *buf, int len, void *f)
 AP_DECLARE(int) ap_scan_script_header_err(request_rec *r, apr_file_t *f,
                                           char *buffer)
 {
-    return ap_scan_script_header_err_core(r, buffer, getsfunc_FILE, f);
+    return ap_scan_script_header_err_core_ex(r, buffer, getsfunc_FILE, f,
+                                             APLOG_MODULE_INDEX);
 }
 
+AP_DECLARE(int) ap_scan_script_header_err_ex(request_rec *r, apr_file_t *f,
+                                          char *buffer, int module_index)
+{
+    return ap_scan_script_header_err_core_ex(r, buffer, getsfunc_FILE, f,
+                                             module_index);
+}
+
+
 static int getsfunc_BRIGADE(char *buf, int len, void *arg)
 {
     apr_bucket_brigade *bb = (apr_bucket_brigade *)arg;
@@ -629,7 +672,8 @@ static int getsfunc_BRIGADE(char *buf, int len, void *arg)
     apr_status_t rv;
     int done = 0;
 
-    while ((dst < dst_end) && !done && !APR_BUCKET_IS_EOS(e)) {
+    while ((dst < dst_end) && !done && e != APR_BRIGADE_SENTINEL(bb)
+           && !APR_BUCKET_IS_EOS(e)) {
         const char *bucket_data;
         apr_size_t bucket_data_len;
         const char *src;
@@ -639,6 +683,7 @@ static int getsfunc_BRIGADE(char *buf, int len, void *arg)
         rv = apr_bucket_read(e, &bucket_data, &bucket_data_len,
                              APR_BLOCK_READ);
         if (rv != APR_SUCCESS || (bucket_data_len == 0)) {
+            *dst = '\0';
             return APR_STATUS_IS_TIMEUP(rv) ? -1 : 0;
         }
         src = bucket_data;
@@ -662,16 +707,27 @@ static int getsfunc_BRIGADE(char *buf, int len, void *arg)
         e = next;
     }
     *dst = 0;
-    return 1;
+    return done;
 }
 
 AP_DECLARE(int) ap_scan_script_header_err_brigade(request_rec *r,
                                                   apr_bucket_brigade *bb,
                                                   char *buffer)
 {
-    return ap_scan_script_header_err_core(r, buffer, getsfunc_BRIGADE, bb);
+    return ap_scan_script_header_err_core_ex(r, buffer, getsfunc_BRIGADE, bb,
+                                             APLOG_MODULE_INDEX);
 }
 
+AP_DECLARE(int) ap_scan_script_header_err_brigade_ex(request_rec *r,
+                                                     apr_bucket_brigade *bb,
+                                                     char *buffer,
+                                                     int module_index)
+{
+    return ap_scan_script_header_err_core_ex(r, buffer, getsfunc_BRIGADE, bb,
+                                             module_index);
+}
+
+
 struct vastrs {
     va_list args;
     int arg;
@@ -684,8 +740,10 @@ static int getsfunc_STRING(char *w, int len, void *pvastrs)
     const char *p;
     int t;
 
-    if (!strs->curpos || !*strs->curpos)
+    if (!strs->curpos || !*strs->curpos) {
+        w[0] = '\0';
         return 0;
+    }
     p = ap_strchr_c(strs->curpos, '\n');
     if (p)
         ++p;
@@ -710,6 +768,28 @@ static int getsfunc_STRING(char *w, int len, void *pvastrs)
  * character is returned to **arg, **data.  (The first optional arg is
  * counted as 0.)
  */
+AP_DECLARE_NONSTD(int) ap_scan_script_header_err_strs_ex(request_rec *r,
+                                                         char *buffer,
+                                                         int module_index,
+                                                         const char **termch,
+                                                         int *termarg, ...)
+{
+    struct vastrs strs;
+    int res;
+
+    va_start(strs.args, termarg);
+    strs.arg = 0;
+    strs.curpos = va_arg(strs.args, char*);
+    res = ap_scan_script_header_err_core_ex(r, buffer, getsfunc_STRING,
+                                            (void *) &strs, module_index);
+    if (termch)
+        *termch = strs.curpos;
+    if (termarg)
+        *termarg = strs.arg;
+    va_end(strs.args);
+    return res;
+}
+
 AP_DECLARE_NONSTD(int) ap_scan_script_header_err_strs(request_rec *r,
                                                       char *buffer,
                                                       const char **termch,
@@ -721,7 +801,8 @@ AP_DECLARE_NONSTD(int) ap_scan_script_header_err_strs(request_rec *r,
     va_start(strs.args, termarg);
     strs.arg = 0;
     strs.curpos = va_arg(strs.args, char*);
-    res = ap_scan_script_header_err_core(r, buffer, getsfunc_STRING, (void *) &strs);
+    res = ap_scan_script_header_err_core_ex(r, buffer, getsfunc_STRING,
+                                            (void *) &strs, APLOG_MODULE_INDEX);
     if (termch)
         *termch = strs.curpos;
     if (termarg)
@@ -730,7 +811,6 @@ AP_DECLARE_NONSTD(int) ap_scan_script_header_err_strs(request_rec *r,
     return res;
 }
 
-
 static void
 argstr_to_table(char *str, apr_table_t *parms)
 {
@@ -741,7 +821,7 @@ argstr_to_table(char *str, apr_table_t *parms)
     if (str == NULL) {
         return;
     }
-    
+
     key = apr_strtok(str, "&", &strtok_state);
     while (key) {
         value = strchr(key, '=');