@@ -241,6 +241,14 @@ isws (int c)
241
241
{
242
242
return c == ' ' || c == '\t' ;
243
243
}
244
+
245
+ static char const *
246
+ trimwsl (char const * s )
247
+ {
248
+ while (* s && isws (* s ))
249
+ s ++ ;
250
+ return s ;
251
+ }
244
252
245
253
static int
246
254
submatch_realloc (struct submatch * sm , GENPAT re )
@@ -1162,7 +1170,7 @@ get_line (BIO *in, char *const buf, int bufsize)
1162
1170
case 0 :
1163
1171
if (BIO_should_retry (in ))
1164
1172
continue ;
1165
- return COPY_EOF ;
1173
+ return i == 0 ? COPY_EOF : COPY_OK ;
1166
1174
case -1 :
1167
1175
return COPY_READ_ERR ;
1168
1176
default :
@@ -1312,15 +1320,11 @@ get_content_length (char const *arg, int mode)
1312
1320
CONTENT_LENGTH n ;
1313
1321
1314
1322
if (mode == CL_HEADER )
1315
- {
1316
- while (isws (* arg ))
1317
- arg ++ ;
1318
- }
1323
+ arg = trimwsl (arg );
1319
1324
1320
1325
if (strtoclen (arg , mode == CL_HEADER ? 10 : 16 , & n , & p ))
1321
1326
return NO_CONTENT_LENGTH ;
1322
- while (isws (* p ))
1323
- p ++ ;
1327
+ p = (char * ) trimwsl (p );
1324
1328
if (* p )
1325
1329
{
1326
1330
if (!(mode == CL_CHUNK && * p == ';' ))
@@ -3218,6 +3222,30 @@ add_forwarded_headers (POUND_HTTP *phttp)
3218
3222
return 0 ;
3219
3223
}
3220
3224
3225
+ static int
3226
+ set_header_from_bio (BIO * bio , struct http_request * req ,
3227
+ char const * hdr , struct stringbuf * sb )
3228
+ {
3229
+ char buf [MAXBUF ];
3230
+ int rc ;
3231
+ char * str ;
3232
+
3233
+ if ((rc = get_line (bio , buf , sizeof (buf ))) == COPY_OK )
3234
+ {
3235
+ stringbuf_reset (sb );
3236
+ stringbuf_printf (sb , "%s: %s" , hdr , trimwsl (buf ));
3237
+ if ((str = stringbuf_finish (sb )) == NULL
3238
+ || http_header_list_append (& req -> headers , str , H_REPLACE ))
3239
+ {
3240
+ return -1 ;
3241
+ }
3242
+ }
3243
+ else if (rc != COPY_EOF )
3244
+ logmsg (LOG_ERR , "(%" PRItid ") error reading data: %s" ,
3245
+ POUND_TID (), copy_status_string (rc ));
3246
+ return 0 ;
3247
+ }
3248
+
3221
3249
static int
3222
3250
add_ssl_headers (POUND_HTTP * phttp )
3223
3251
{
@@ -3248,72 +3276,40 @@ add_ssl_headers (POUND_HTTP *phttp)
3248
3276
if (phttp -> lstn -> clnt_check > 0 && phttp -> x509 != NULL
3249
3277
&& (bio = BIO_new (BIO_s_mem ())) != NULL )
3250
3278
{
3279
+ int i ;
3280
+
3281
+ BIO_set_mem_eof_return (bio , 0 );
3251
3282
X509_NAME_print_ex (bio , X509_get_subject_name (phttp -> x509 ), 8 ,
3252
3283
XN_FLAG_ONELINE & ~ASN1_STRFLGS_ESC_MSB );
3253
- if (get_line (bio , buf , sizeof ( buf )) != COPY_OK )
3284
+ if (set_header_from_bio (bio , & phttp -> request , "X-SSL-Subject" , & sb ) )
3254
3285
{
3255
3286
res = -1 ;
3256
3287
goto end ;
3257
3288
}
3258
3289
3259
- stringbuf_printf (& sb , "X-SSL-Subject: %s" , buf );
3260
- if ((str = stringbuf_finish (& sb )) == NULL
3261
- || http_header_list_append (& phttp -> request .headers , str , H_REPLACE ))
3262
- {
3263
- res = -1 ;
3264
- goto end ;
3265
- }
3266
- stringbuf_reset (& sb );
3267
-
3268
3290
X509_NAME_print_ex (bio , X509_get_issuer_name (phttp -> x509 ), 8 ,
3269
3291
XN_FLAG_ONELINE & ~ASN1_STRFLGS_ESC_MSB );
3270
- if (get_line (bio , buf , sizeof (buf )) != COPY_OK )
3271
- {
3272
- res = -1 ;
3273
- goto end ;
3274
- }
3275
-
3276
- stringbuf_printf (& sb , "X-SSL-Issuer: %s" , buf );
3277
- if ((str = stringbuf_finish (& sb )) == NULL
3278
- || http_header_list_append (& phttp -> request .headers , str , H_REPLACE ))
3292
+ if (set_header_from_bio (bio , & phttp -> request , "X-SSL-Issuer" , & sb ))
3279
3293
{
3280
3294
res = -1 ;
3281
3295
goto end ;
3282
3296
}
3283
- stringbuf_reset (& sb );
3284
3297
3285
3298
ASN1_TIME_print (bio , X509_get_notBefore (phttp -> x509 ));
3286
- if (get_line (bio , buf , sizeof ( buf )) != COPY_OK )
3299
+ if (set_header_from_bio (bio , & phttp -> request , "X-SSL-notBefore" , & sb ) )
3287
3300
{
3288
3301
res = -1 ;
3289
3302
goto end ;
3290
3303
}
3291
3304
3292
- stringbuf_printf (& sb , "X-SSL-notBefore: %s" , buf );
3293
- if ((str = stringbuf_finish (& sb )) == NULL
3294
- || http_header_list_append (& phttp -> request .headers , str , H_REPLACE ))
3295
- {
3296
- res = -1 ;
3297
- goto end ;
3298
- }
3299
- stringbuf_reset (& sb );
3300
-
3301
3305
ASN1_TIME_print (bio , X509_get_notAfter (phttp -> x509 ));
3302
- if (get_line (bio , buf , sizeof ( buf )) != COPY_OK )
3306
+ if (set_header_from_bio (bio , & phttp -> request , "X-SSL-notAfter" , & sb ) )
3303
3307
{
3304
3308
res = -1 ;
3305
3309
goto end ;
3306
3310
}
3307
3311
3308
- stringbuf_printf (& sb , "X-SSL-notAfter: %s" , buf );
3309
- if ((str = stringbuf_finish (& sb )) == NULL
3310
- || http_header_list_append (& phttp -> request .headers , str , H_REPLACE ))
3311
- {
3312
- res = -1 ;
3313
- goto end ;
3314
- }
3315
3312
stringbuf_reset (& sb );
3316
-
3317
3313
stringbuf_printf (& sb , "X-SSL-serial: %ld" ,
3318
3314
ASN1_INTEGER_get (X509_get_serialNumber (phttp -> x509 )));
3319
3315
if ((str = stringbuf_finish (& sb )) == NULL
@@ -3326,9 +3322,13 @@ add_ssl_headers (POUND_HTTP *phttp)
3326
3322
3327
3323
PEM_write_bio_X509 (bio , phttp -> x509 );
3328
3324
stringbuf_add_string (& sb , "X-SSL-certificate: " );
3325
+ i = 0 ;
3329
3326
while (get_line (bio , buf , sizeof (buf )) == COPY_OK )
3330
3327
{
3328
+ if (i > 0 )
3329
+ stringbuf_add_string (& sb , "\n\t" );
3331
3330
stringbuf_add_string (& sb , buf );
3331
+ i ++ ;
3332
3332
}
3333
3333
if ((str = stringbuf_finish (& sb )) == NULL
3334
3334
|| http_header_list_append (& phttp -> request .headers , str , H_REPLACE ))
@@ -3580,7 +3580,7 @@ log_error (POUND_HTTP *phttp, int code, int en, char const *fmt, ...)
3580
3580
static int
3581
3581
http_response_validate (struct http_request * req )
3582
3582
{
3583
- char * str = req -> request ;
3583
+ char const * str = req -> request ;
3584
3584
int http_ver ;
3585
3585
3586
3586
if (!(strncmp (str , "HTTP/1." , 7 ) == 0 &&
@@ -3589,9 +3589,7 @@ http_response_validate (struct http_request *req)
3589
3589
return 0 ;
3590
3590
req -> version = http_ver - '0' ;
3591
3591
3592
- for (str += 8 ; isws (* str ); str ++ )
3593
- if (!* str )
3594
- return 0 ;
3592
+ str = trimwsl (str + 8 );
3595
3593
3596
3594
switch (str [0 ])
3597
3595
{
0 commit comments