bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / server / util_script.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "apr.h"
18 #include "apr_lib.h"
19 #include "apr_strings.h"
20
21 #define APR_WANT_STRFUNC
22 #include "apr_want.h"
23
24 #if APR_HAVE_STDLIB_H
25 #include <stdlib.h>
26 #endif
27
28 #define CORE_PRIVATE
29 #include "ap_config.h"
30 #include "httpd.h"
31 #include "http_config.h"
32 #include "http_main.h"
33 #include "http_log.h"
34 #include "http_core.h"
35 #include "http_protocol.h"
36 #include "http_request.h"       /* for sub_req_lookup_uri() */
37 #include "util_script.h"
38 #include "apr_date.h"           /* For apr_date_parse_http() */
39 #include "util_ebcdic.h"
40
41 #ifdef OS2
42 #define INCL_DOS
43 #include <os2.h>
44 #endif
45
46 /*
47  * Various utility functions which are common to a whole lot of
48  * script-type extensions mechanisms, and might as well be gathered
49  * in one place (if only to avoid creating inter-module dependancies
50  * where there don't have to be).
51  */
52
53 #define MALFORMED_MESSAGE "malformed header from script. Bad header="
54 #define MALFORMED_HEADER_LENGTH_TO_SHOW 30
55
56 static char *http2env(apr_pool_t *a, const char *w)
57 {
58     char *res = (char *)apr_palloc(a, sizeof("HTTP_") + strlen(w));
59     char *cp = res;
60     char c;
61
62     *cp++ = 'H';
63     *cp++ = 'T';
64     *cp++ = 'T';
65     *cp++ = 'P';
66     *cp++ = '_';
67
68     while ((c = *w++) != 0) {
69         if (!apr_isalnum(c)) {
70             *cp++ = '_';
71         }
72         else {
73             *cp++ = apr_toupper(c);
74         }
75     }
76     *cp = 0;
77  
78     return res;
79 }
80
81 AP_DECLARE(char **) ap_create_environment(apr_pool_t *p, apr_table_t *t)
82 {
83     const apr_array_header_t *env_arr = apr_table_elts(t);
84     const apr_table_entry_t *elts = (const apr_table_entry_t *) env_arr->elts;
85     char **env = (char **) apr_palloc(p, (env_arr->nelts + 2) * sizeof(char *));
86     int i, j;
87     char *tz;
88     char *whack;
89
90     j = 0;
91     if (!apr_table_get(t, "TZ")) {
92         tz = getenv("TZ");
93         if (tz != NULL) {
94             env[j++] = apr_pstrcat(p, "TZ=", tz, NULL);
95         }
96     }
97     for (i = 0; i < env_arr->nelts; ++i) {
98         if (!elts[i].key) {
99             continue;
100         }
101         env[j] = apr_pstrcat(p, elts[i].key, "=", elts[i].val, NULL);
102         whack = env[j];
103         if (apr_isdigit(*whack)) {
104             *whack++ = '_';
105         }
106         while (*whack != '=') {
107             if (!apr_isalnum(*whack) && *whack != '_') {
108                 *whack = '_';
109             }
110             ++whack;
111         }
112         ++j;
113     }
114
115     env[j] = NULL;
116     return env;
117 }
118
119 AP_DECLARE(void) ap_add_common_vars(request_rec *r)
120 {
121     apr_table_t *e;
122     server_rec *s = r->server;
123     conn_rec *c = r->connection;
124     const char *rem_logname;
125     char *env_path;
126 #if defined(WIN32) || defined(OS2) || defined(BEOS)
127     char *env_temp;
128 #endif
129     const char *host;
130     const apr_array_header_t *hdrs_arr = apr_table_elts(r->headers_in);
131     const apr_table_entry_t *hdrs = (const apr_table_entry_t *) hdrs_arr->elts;
132     int i;
133     apr_port_t rport;
134
135     /* use a temporary apr_table_t which we'll overlap onto
136      * r->subprocess_env later
137      * (exception: if r->subprocess_env is empty at the start,
138      * write directly into it)
139      */
140     if (apr_is_empty_table(r->subprocess_env)) {
141       e = r->subprocess_env;
142     }
143     else {
144       e = apr_table_make(r->pool, 25 + hdrs_arr->nelts);
145     }
146
147     /* First, add environment vars from headers... this is as per
148      * CGI specs, though other sorts of scripting interfaces see
149      * the same vars...
150      */
151
152     for (i = 0; i < hdrs_arr->nelts; ++i) {
153         if (!hdrs[i].key) {
154             continue;
155         }
156
157         /* A few headers are special cased --- Authorization to prevent
158          * rogue scripts from capturing passwords; content-type and -length
159          * for no particular reason.
160          */
161
162         if (!strcasecmp(hdrs[i].key, "Content-type")) {
163             apr_table_addn(e, "CONTENT_TYPE", hdrs[i].val);
164         }
165         else if (!strcasecmp(hdrs[i].key, "Content-length")) {
166             apr_table_addn(e, "CONTENT_LENGTH", hdrs[i].val);
167         }
168         /*
169          * You really don't want to disable this check, since it leaves you
170          * wide open to CGIs stealing passwords and people viewing them
171          * in the environment with "ps -e".  But, if you must...
172          */
173 #ifndef SECURITY_HOLE_PASS_AUTHORIZATION
174         else if (!strcasecmp(hdrs[i].key, "Authorization") 
175                  || !strcasecmp(hdrs[i].key, "Proxy-Authorization")) {
176             continue;
177         }
178 #endif
179         else {
180             apr_table_addn(e, http2env(r->pool, hdrs[i].key), hdrs[i].val);
181         }
182     }
183
184     if (!(env_path = getenv("PATH"))) {
185         env_path = DEFAULT_PATH;
186     }
187     apr_table_addn(e, "PATH", apr_pstrdup(r->pool, env_path));
188
189 #ifdef WIN32
190     if (env_temp = getenv("SystemRoot")) {
191         apr_table_addn(e, "SystemRoot", env_temp);         
192     }
193     if (env_temp = getenv("COMSPEC")) {
194         apr_table_addn(e, "COMSPEC", env_temp);            
195     }
196     if (env_temp = getenv("PATHEXT")) {
197         apr_table_addn(e, "PATHEXT", env_temp);            
198     }
199     if (env_temp = getenv("WINDIR")) {
200         apr_table_addn(e, "WINDIR", env_temp);
201     }
202 #endif
203
204 #ifdef OS2
205     if ((env_temp = getenv("COMSPEC")) != NULL) {
206         apr_table_addn(e, "COMSPEC", env_temp);            
207     }
208     if ((env_temp = getenv("ETC")) != NULL) {
209         apr_table_addn(e, "ETC", env_temp);            
210     }
211     if ((env_temp = getenv("DPATH")) != NULL) {
212         apr_table_addn(e, "DPATH", env_temp);            
213     }
214     if ((env_temp = getenv("PERLLIB_PREFIX")) != NULL) {
215         apr_table_addn(e, "PERLLIB_PREFIX", env_temp);            
216     }
217 #endif
218
219 #ifdef BEOS
220     if ((env_temp = getenv("LIBRARY_PATH")) != NULL) {
221         apr_table_addn(e, "LIBRARY_PATH", env_temp);            
222     }
223 #endif
224
225     apr_table_addn(e, "SERVER_SIGNATURE", ap_psignature("", r));
226     apr_table_addn(e, "SERVER_SOFTWARE", ap_get_server_version());
227     apr_table_addn(e, "SERVER_NAME",
228                    ap_escape_html(r->pool, ap_get_server_name(r)));
229     apr_table_addn(e, "SERVER_ADDR", r->connection->local_ip);  /* Apache */
230     apr_table_addn(e, "SERVER_PORT",
231                   apr_psprintf(r->pool, "%u", ap_get_server_port(r)));
232     host = ap_get_remote_host(c, r->per_dir_config, REMOTE_HOST, NULL);
233     if (host) {
234         apr_table_addn(e, "REMOTE_HOST", host);
235     }
236     apr_table_addn(e, "REMOTE_ADDR", c->remote_ip);
237     apr_table_addn(e, "DOCUMENT_ROOT", ap_document_root(r));    /* Apache */
238     apr_table_addn(e, "SERVER_ADMIN", s->server_admin); /* Apache */
239     apr_table_addn(e, "SCRIPT_FILENAME", r->filename);  /* Apache */
240
241     apr_sockaddr_port_get(&rport, c->remote_addr);
242     apr_table_addn(e, "REMOTE_PORT", apr_itoa(r->pool, rport));
243
244     if (r->user) {
245         apr_table_addn(e, "REMOTE_USER", r->user);
246     }
247     else if (r->prev) {
248         request_rec *back = r->prev;
249
250         while (back) {
251             if (back->user) {
252                 apr_table_addn(e, "REDIRECT_REMOTE_USER", back->user);
253                 break;
254             }
255             back = back->prev;
256         }
257     }
258     if (r->ap_auth_type) {
259         apr_table_addn(e, "AUTH_TYPE", r->ap_auth_type);
260     }
261     rem_logname = ap_get_remote_logname(r);
262     if (rem_logname) {
263         apr_table_addn(e, "REMOTE_IDENT", apr_pstrdup(r->pool, rem_logname));
264     }
265
266     /* Apache custom error responses. If we have redirected set two new vars */
267
268     if (r->prev) {
269         if (r->prev->args) {
270             apr_table_addn(e, "REDIRECT_QUERY_STRING", r->prev->args);
271         }
272         if (r->prev->uri) {
273             apr_table_addn(e, "REDIRECT_URL", r->prev->uri);
274         }
275     }
276
277     if (e != r->subprocess_env) {
278       apr_table_overlap(r->subprocess_env, e, APR_OVERLAP_TABLES_SET);
279     }
280 }
281
282 /* This "cute" little function comes about because the path info on
283  * filenames and URLs aren't always the same. So we take the two,
284  * and find as much of the two that match as possible.
285  */
286
287 AP_DECLARE(int) ap_find_path_info(const char *uri, const char *path_info)
288 {
289     int lu = strlen(uri);
290     int lp = strlen(path_info);
291
292     while (lu-- && lp-- && uri[lu] == path_info[lp]);
293
294     if (lu == -1) {
295         lu = 0;
296     }
297
298     while (uri[lu] != '\0' && uri[lu] != '/') {
299         lu++;
300     }
301     return lu;
302 }
303
304 /* Obtain the Request-URI from the original request-line, returning
305  * a new string from the request pool containing the URI or "".
306  */
307 static char *original_uri(request_rec *r)
308 {
309     char *first, *last;
310
311     if (r->the_request == NULL) {
312         return (char *) apr_pcalloc(r->pool, 1);
313     }
314
315     first = r->the_request;     /* use the request-line */
316
317     while (*first && !apr_isspace(*first)) {
318         ++first;                /* skip over the method */
319     }
320     while (apr_isspace(*first)) {
321         ++first;                /*   and the space(s)   */
322     }
323
324     last = first;
325     while (*last && !apr_isspace(*last)) {
326         ++last;                 /* end at next whitespace */
327     }
328
329     return apr_pstrmemdup(r->pool, first, last - first);
330 }
331
332 AP_DECLARE(void) ap_add_cgi_vars(request_rec *r)
333 {
334     apr_table_t *e = r->subprocess_env;
335
336     apr_table_setn(e, "GATEWAY_INTERFACE", "CGI/1.1");
337     apr_table_setn(e, "SERVER_PROTOCOL", r->protocol);
338     apr_table_setn(e, "REQUEST_METHOD", r->method);
339     apr_table_setn(e, "QUERY_STRING", r->args ? r->args : "");
340     apr_table_setn(e, "REQUEST_URI", original_uri(r)); 
341
342     /* Note that the code below special-cases scripts run from includes,
343      * because it "knows" that the sub_request has been hacked to have the
344      * args and path_info of the original request, and not any that may have
345      * come with the script URI in the include command.  Ugh.
346      */
347
348     if (!strcmp(r->protocol, "INCLUDED")) {
349         apr_table_setn(e, "SCRIPT_NAME", r->uri);
350         if (r->path_info && *r->path_info) {
351             apr_table_setn(e, "PATH_INFO", r->path_info);
352         }
353     }
354     else if (!r->path_info || !*r->path_info) {
355         apr_table_setn(e, "SCRIPT_NAME", r->uri);
356     }
357     else {
358         int path_info_start = ap_find_path_info(r->uri, r->path_info);
359
360         apr_table_setn(e, "SCRIPT_NAME",
361                       apr_pstrndup(r->pool, r->uri, path_info_start));
362
363         apr_table_setn(e, "PATH_INFO", r->path_info);
364     }
365
366     if (r->path_info && r->path_info[0]) {
367         /*
368          * To get PATH_TRANSLATED, treat PATH_INFO as a URI path.
369          * Need to re-escape it for this, since the entire URI was
370          * un-escaped before we determined where the PATH_INFO began.
371          */
372         request_rec *pa_req;
373
374         pa_req = ap_sub_req_lookup_uri(ap_escape_uri(r->pool, r->path_info), r,
375                                        NULL);
376
377         if (pa_req->filename) {
378             char *pt = apr_pstrcat(r->pool, pa_req->filename, pa_req->path_info,
379                                   NULL);
380 #ifdef WIN32
381             /* We need to make this a real Windows path name */
382             apr_filepath_merge(&pt, "", pt, APR_FILEPATH_NATIVE, r->pool);
383 #endif
384             apr_table_setn(e, "PATH_TRANSLATED", pt);
385         }
386         ap_destroy_sub_req(pa_req);
387     }
388 }
389
390
391 static int set_cookie_doo_doo(void *v, const char *key, const char *val)
392 {
393     apr_table_addn(v, key, val);
394     return 1;
395 }
396
397 #define HTTP_UNSET (-HTTP_OK)
398
399 AP_DECLARE(int) ap_scan_script_header_err_core(request_rec *r, char *buffer,
400                                        int (*getsfunc) (char *, int, void *),
401                                        void *getsfunc_data)
402 {
403     char x[MAX_STRING_LEN];
404     char *w, *l;
405     int p;
406     int cgi_status = HTTP_UNSET;
407     apr_table_t *merge;
408     apr_table_t *cookie_table;
409
410     if (buffer) {
411         *buffer = '\0';
412     }
413     w = buffer ? buffer : x;
414
415     /* temporary place to hold headers to merge in later */
416     merge = apr_table_make(r->pool, 10);
417
418     /* The HTTP specification says that it is legal to merge duplicate
419      * headers into one.  Some browsers that support Cookies don't like
420      * merged headers and prefer that each Set-Cookie header is sent
421      * separately.  Lets humour those browsers by not merging.
422      * Oh what a pain it is.
423      */
424     cookie_table = apr_table_make(r->pool, 2);
425     apr_table_do(set_cookie_doo_doo, cookie_table, r->err_headers_out, "Set-Cookie", NULL);
426
427     while (1) {
428
429         if ((*getsfunc) (w, MAX_STRING_LEN - 1, getsfunc_data) == 0) {
430             ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_TOCLIENT, 0, r,
431                           "Premature end of script headers: %s", 
432                           apr_filename_of_pathname(r->filename));
433             return HTTP_INTERNAL_SERVER_ERROR;
434         }
435
436         /* Delete terminal (CR?)LF */
437
438         p = strlen(w);
439              /* Indeed, the host's '\n':
440                 '\012' for UNIX; '\015' for MacOS; '\025' for OS/390
441                  -- whatever the script generates.
442              */                                  
443         if (p > 0 && w[p - 1] == '\n') {
444             if (p > 1 && w[p - 2] == CR) {
445                 w[p - 2] = '\0';
446             }
447             else {
448                 w[p - 1] = '\0';
449             }
450         }
451
452         /*
453          * If we've finished reading the headers, check to make sure any
454          * HTTP/1.1 conditions are met.  If so, we're done; normal processing
455          * will handle the script's output.  If not, just return the error.
456          * The appropriate thing to do would be to send the script process a
457          * SIGPIPE to let it know we're ignoring it, close the channel to the
458          * script process, and *then* return the failed-to-meet-condition
459          * error.  Otherwise we'd be waiting for the script to finish
460          * blithering before telling the client the output was no good.
461          * However, we don't have the information to do that, so we have to
462          * leave it to an upper layer.
463          */
464         if (w[0] == '\0') {
465             int cond_status = OK;
466
467             /* PR#38070: This fails because it gets confused when a
468              * CGI Status header overrides ap_meets_conditions.
469              * 
470              * We can fix that by dropping ap_meets_conditions when
471              * Status has been set.  Since this is the only place
472              * cgi_status gets used, let's test it explicitly.
473              *
474              * The alternative would be to ignore CGI Status when
475              * ap_meets_conditions returns anything interesting.
476              * That would be safer wrt HTTP, but would break CGI.
477              */
478             if ((cgi_status == HTTP_UNSET) && (r->method_number == M_GET)) {
479                 cond_status = ap_meets_conditions(r);
480             }
481             apr_table_overlap(r->err_headers_out, merge,
482                 APR_OVERLAP_TABLES_MERGE);
483             if (!apr_is_empty_table(cookie_table)) {
484                 /* the cookies have already been copied to the cookie_table */
485                 apr_table_unset(r->err_headers_out, "Set-Cookie");
486                 r->err_headers_out = apr_table_overlay(r->pool,
487                     r->err_headers_out, cookie_table);
488             }
489             return cond_status;
490         }
491
492         /* if we see a bogus header don't ignore it. Shout and scream */
493
494 #if APR_CHARSET_EBCDIC
495             /* Chances are that we received an ASCII header text instead of
496              * the expected EBCDIC header lines. Try to auto-detect:
497              */
498         if (!(l = strchr(w, ':'))) {
499             int maybeASCII = 0, maybeEBCDIC = 0;
500             unsigned char *cp, native;
501             apr_size_t inbytes_left, outbytes_left;
502
503             for (cp = w; *cp != '\0'; ++cp) {
504                 native = apr_xlate_conv_byte(ap_hdrs_from_ascii, *cp);
505                 if (apr_isprint(*cp) && !apr_isprint(native))
506                     ++maybeEBCDIC;
507                 if (!apr_isprint(*cp) && apr_isprint(native))
508                     ++maybeASCII;
509             }
510             if (maybeASCII > maybeEBCDIC) {
511                 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
512                              "CGI Interface Error: Script headers apparently ASCII: (CGI = %s)",
513                              r->filename);
514                 inbytes_left = outbytes_left = cp - w;
515                 apr_xlate_conv_buffer(ap_hdrs_from_ascii,
516                                       w, &inbytes_left, w, &outbytes_left);
517             }
518         }
519 #endif /*APR_CHARSET_EBCDIC*/
520         if (!(l = strchr(w, ':'))) {
521             char malformed[(sizeof MALFORMED_MESSAGE) + 1
522                            + MALFORMED_HEADER_LENGTH_TO_SHOW];
523
524             strcpy(malformed, MALFORMED_MESSAGE);
525             strncat(malformed, w, MALFORMED_HEADER_LENGTH_TO_SHOW);
526
527             if (!buffer) {
528                 /* Soak up all the script output - may save an outright kill */
529                 while ((*getsfunc) (w, MAX_STRING_LEN - 1, getsfunc_data)) {
530                     continue;
531                 }
532             }
533
534             ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_TOCLIENT, 0, r,
535                           "%s: %s", malformed, 
536                           apr_filename_of_pathname(r->filename));
537             return HTTP_INTERNAL_SERVER_ERROR;
538         }
539
540         *l++ = '\0';
541         while (*l && apr_isspace(*l)) {
542             ++l;
543         }
544
545         if (!strcasecmp(w, "Content-type")) {
546             char *tmp;
547
548             /* Nuke trailing whitespace */
549
550             char *endp = l + strlen(l) - 1;
551             while (endp > l && apr_isspace(*endp)) {
552                 *endp-- = '\0';
553             }
554
555             tmp = apr_pstrdup(r->pool, l);
556             ap_content_type_tolower(tmp);
557             ap_set_content_type(r, tmp);
558         }
559         /*
560          * If the script returned a specific status, that's what
561          * we'll use - otherwise we assume 200 OK.
562          */
563         else if (!strcasecmp(w, "Status")) {
564             r->status = cgi_status = atoi(l);
565             r->status_line = apr_pstrdup(r->pool, l);
566         }
567         else if (!strcasecmp(w, "Location")) {
568             apr_table_set(r->headers_out, w, l);
569         }
570         else if (!strcasecmp(w, "Content-Length")) {
571             apr_table_set(r->headers_out, w, l);
572         }
573         else if (!strcasecmp(w, "Content-Range")) {
574             apr_table_set(r->headers_out, w, l);
575         }
576         else if (!strcasecmp(w, "Transfer-Encoding")) {
577             apr_table_set(r->headers_out, w, l);
578         }
579         /*
580          * If the script gave us a Last-Modified header, we can't just
581          * pass it on blindly because of restrictions on future values.
582          */
583         else if (!strcasecmp(w, "Last-Modified")) {
584             ap_update_mtime(r, apr_date_parse_http(l));
585             ap_set_last_modified(r);
586         }
587         else if (!strcasecmp(w, "Set-Cookie")) {
588             apr_table_add(cookie_table, w, l);
589         }
590         else {
591             apr_table_add(merge, w, l);
592         }
593     }
594
595     return OK;
596 }
597
598 static int getsfunc_FILE(char *buf, int len, void *f)
599 {
600     return apr_file_gets(buf, len, (apr_file_t *) f) == APR_SUCCESS;
601 }
602
603 AP_DECLARE(int) ap_scan_script_header_err(request_rec *r, apr_file_t *f,
604                                           char *buffer)
605 {
606     return ap_scan_script_header_err_core(r, buffer, getsfunc_FILE, f);
607 }
608
609 static int getsfunc_BRIGADE(char *buf, int len, void *arg)
610 {
611     apr_bucket_brigade *bb = (apr_bucket_brigade *)arg;
612     const char *dst_end = buf + len - 1; /* leave room for terminating null */
613     char *dst = buf;
614     apr_bucket *e = APR_BRIGADE_FIRST(bb);
615     apr_status_t rv;
616     int done = 0;
617
618     while ((dst < dst_end) && !done && !APR_BUCKET_IS_EOS(e)) {
619         const char *bucket_data;
620         apr_size_t bucket_data_len;
621         const char *src;
622         const char *src_end;
623         apr_bucket * next;
624
625         rv = apr_bucket_read(e, &bucket_data, &bucket_data_len,
626                              APR_BLOCK_READ);
627         if (!APR_STATUS_IS_SUCCESS(rv) || (bucket_data_len == 0)) {
628             return 0;
629         }
630         src = bucket_data;
631         src_end = bucket_data + bucket_data_len;
632         while ((src < src_end) && (dst < dst_end) && !done) {
633             if (*src == '\n') {
634                 done = 1;
635             }
636             else if (*src != '\r') {
637                 *dst++ = *src;
638             }
639             src++;
640         }
641
642         if (src < src_end) {
643             apr_bucket_split(e, src - bucket_data);
644         }
645         next = APR_BUCKET_NEXT(e);
646         APR_BUCKET_REMOVE(e);
647         apr_bucket_destroy(e);
648         e = next;
649     }
650     *dst = 0;
651     return 1;
652 }
653
654 AP_DECLARE(int) ap_scan_script_header_err_brigade(request_rec *r,
655                                                   apr_bucket_brigade *bb,
656                                                   char *buffer)
657 {
658     return ap_scan_script_header_err_core(r, buffer, getsfunc_BRIGADE, bb);
659 }
660
661 struct vastrs {
662     va_list args;
663     int arg;
664     const char *curpos;
665 };
666
667 static int getsfunc_STRING(char *w, int len, void *pvastrs)
668 {
669     struct vastrs *strs = (struct vastrs*) pvastrs;
670     const char *p;
671     int t;
672     
673     if (!strs->curpos || !*strs->curpos) 
674         return 0;
675     p = ap_strchr_c(strs->curpos, '\n');
676     if (p)
677         ++p;
678     else
679         p = ap_strchr_c(strs->curpos, '\0');
680     t = p - strs->curpos;
681     if (t > len)
682         t = len;
683     strncpy (w, strs->curpos, t);
684     w[t] = '\0';
685     if (!strs->curpos[t]) {
686         ++strs->arg;
687         strs->curpos = va_arg(strs->args, const char *);
688     }
689     else
690         strs->curpos += t;
691     return t;    
692 }
693
694 /* ap_scan_script_header_err_strs() accepts additional const char* args...
695  * each is treated as one or more header lines, and the first non-header
696  * character is returned to **arg, **data.  (The first optional arg is
697  * counted as 0.)
698  */
699 AP_DECLARE_NONSTD(int) ap_scan_script_header_err_strs(request_rec *r, 
700                                                       char *buffer, 
701                                                       const char **termch,
702                                                       int *termarg, ...)
703 {
704     struct vastrs strs;
705     int res;
706
707     va_start(strs.args, termarg);
708     strs.arg = 0;
709     strs.curpos = va_arg(strs.args, char*);
710     res = ap_scan_script_header_err_core(r, buffer, getsfunc_STRING, (void *) &strs);
711     if (termch)
712         *termch = strs.curpos;
713     if (termarg)
714         *termarg = strs.arg;
715     va_end(strs.args);
716     return res;
717 }