@@ -21,17 +21,21 @@ static X509 *x509 = NULL;
21
21
22
22
typedef struct {
23
23
BIO * out ;
24
+ const char * content_type ;
25
+ const char * txt ;
24
26
char version ;
25
27
int keep_alive ;
26
28
} server_args ;
27
29
28
30
/*-
29
31
* Pretty trivial HTTP mock server:
30
- * For POST, copy request headers+body from mem BIO 'in' as response to 'out'.
31
- * For GET, redirect to RPATH, else respond with 'rsp' of ASN1 type 'it'.
32
- * Respond with HTTP version 1.'version' and 'keep_alive' (unless implicit).
32
+ * For POST, copy request headers+body from mem BIO |in| as response to |out|.
33
+ * For GET, redirect to RPATH unless already there, else use |content_type| and
34
+ * respond with |txt| if not NULL, else with |rsp| of ASN1 type |it|.
35
+ * Response hdr has HTTP version 1.|version| and |keep_alive| (unless implicit).
33
36
*/
34
37
static int mock_http_server (BIO * in , BIO * out , char version , int keep_alive ,
38
+ const char * content_type , const char * txt ,
35
39
ASN1_VALUE * rsp , const ASN1_ITEM * it )
36
40
{
37
41
const char * req , * path ;
@@ -40,7 +44,7 @@ static int mock_http_server(BIO *in, BIO *out, char version, int keep_alive,
40
44
int is_get = count >= 4 && strncmp (hdr , "GET " , 4 ) == 0 ;
41
45
int len ;
42
46
43
- /* first line should contain "< GET or POST> <path> HTTP/1.x" */
47
+ /* first line should contain "( GET| POST) <path> HTTP/1.x" */
44
48
if (is_get )
45
49
hdr += 4 ;
46
50
else if (TEST_true (count >= 5 && strncmp (hdr , "POST " , 5 ) == 0 ))
@@ -79,11 +83,15 @@ static int mock_http_server(BIO *in, BIO *out, char version, int keep_alive,
79
83
version == '0' ? "keep-alive" : "close" ) <= 0 )
80
84
return 0 ;
81
85
if (is_get ) { /* construct new header and body */
82
- if ((len = ASN1_item_i2d (rsp , NULL , it )) <= 0 )
86
+ if (txt != NULL )
87
+ len = strlen (txt );
88
+ else if ((len = ASN1_item_i2d (rsp , NULL , it )) <= 0 )
83
89
return 0 ;
84
- if (BIO_printf (out , "Content-Type: application/x-x509-ca-cert \r\n"
85
- "Content-Length: %d\r\n\r\n" , len ) <= 0 )
90
+ if (BIO_printf (out , "Content-Type: %s \r\n"
91
+ "Content-Length: %d\r\n\r\n" , content_type , len ) <= 0 )
86
92
return 0 ;
93
+ if (txt != NULL )
94
+ return BIO_puts (out , txt );
87
95
return ASN1_item_i2d_bio (it , out , rsp );
88
96
} else {
89
97
len = strlen ("Connection: " );
@@ -106,47 +114,91 @@ static long http_bio_cb_ex(BIO *bio, int oper, const char *argp, size_t len,
106
114
107
115
if (oper == (BIO_CB_CTRL | BIO_CB_RETURN ) && cmd == BIO_CTRL_FLUSH )
108
116
ret = mock_http_server (bio , args -> out , args -> version , args -> keep_alive ,
117
+ args -> content_type , args -> txt ,
109
118
(ASN1_VALUE * )x509 , x509_it );
110
119
return ret ;
111
120
}
112
121
113
- static int test_http_x509 (int do_get )
122
+ #define text1 "test\n"
123
+ #define text2 "more\n"
124
+ #define REAL_SERVER_URL "http://httpbin.org/"
125
+ #define DOCTYPE_HTML "<!DOCTYPE html>\n"
126
+
127
+ static int test_http_method (int do_get , int do_txt )
114
128
{
115
- X509 * rcert = NULL ;
116
129
BIO * wbio = BIO_new (BIO_s_mem ());
117
130
BIO * rbio = BIO_new (BIO_s_mem ());
118
- server_args mock_args = { NULL , '0' , 0 };
119
- BIO * rsp , * req = ASN1_item_i2d_mem_bio ( x509_it , ( ASN1_VALUE * ) x509 ) ;
131
+ server_args mock_args = { NULL , NULL , NULL , '0' , 0 };
132
+ BIO * req , * rsp ;
120
133
STACK_OF (CONF_VALUE ) * headers = NULL ;
121
- const char content_type [] = "application/x-x509-ca-cert" ;
134
+ const char * content_type ;
122
135
int res = 0 ;
123
-
136
+ int real_server = do_txt && 0 ; /* remove "&& 0" for using real server */
137
+
138
+ if (do_txt ) {
139
+ content_type = "text/plain" ;
140
+ req = BIO_new (BIO_s_mem ());
141
+ if (req == NULL
142
+ || BIO_puts (req , text1 ) != sizeof (text1 ) - 1
143
+ || BIO_puts (req , text2 ) != sizeof (text2 ) - 1 ) {
144
+ BIO_free (req );
145
+ req = NULL ;
146
+ }
147
+ mock_args .txt = text1 ;
148
+ } else {
149
+ content_type = "application/x-x509-ca-cert" ;
150
+ req = ASN1_item_i2d_mem_bio (x509_it , (ASN1_VALUE * )x509 );
151
+ mock_args .txt = NULL ;
152
+ }
124
153
if (wbio == NULL || rbio == NULL || req == NULL )
125
154
goto err ;
155
+
126
156
mock_args .out = rbio ;
157
+ mock_args .content_type = content_type ;
127
158
BIO_set_callback_ex (wbio , http_bio_cb_ex );
128
159
BIO_set_callback_arg (wbio , (char * )& mock_args );
129
160
130
161
rsp = do_get ?
131
- OSSL_HTTP_get ("/will-be-redirected" ,
162
+ OSSL_HTTP_get (real_server ? REAL_SERVER_URL :
163
+ do_txt ? RPATH : "/will-be-redirected" ,
132
164
NULL /* proxy */ , NULL /* no_proxy */ ,
133
- wbio , rbio , NULL /* bio_update_fn */ , NULL /* arg */ ,
134
- 0 /* buf_size */ , headers , content_type ,
135
- 1 /* expect_asn1 */ ,
165
+ real_server ? NULL : wbio ,
166
+ real_server ? NULL : rbio ,
167
+ NULL /* bio_update_fn */ , NULL /* arg */ ,
168
+ 0 /* buf_size */ , headers ,
169
+ real_server ? "text/html; charset=utf-8" : content_type ,
170
+ !do_txt /* expect_asn1 */ ,
136
171
OSSL_HTTP_DEFAULT_MAX_RESP_LEN , 0 /* timeout */ )
137
172
: OSSL_HTTP_transfer (NULL , NULL /* host */ , NULL /* port */ , RPATH ,
138
173
0 /* use_ssl */ ,NULL /* proxy */ , NULL /* no_pr */ ,
139
174
wbio , rbio , NULL /* bio_fn */ , NULL /* arg */ ,
140
175
0 /* buf_size */ , headers , content_type ,
141
- req , content_type , 1 /* expect_asn1 */ ,
176
+ req , content_type , ! do_txt /* expect_asn1 */ ,
142
177
OSSL_HTTP_DEFAULT_MAX_RESP_LEN , 0 /* timeout */ ,
143
178
0 /* keep_alive */ );
144
- rcert = d2i_X509_bio (rsp , NULL );
145
- BIO_free (rsp );
146
- res = TEST_ptr (rcert ) && TEST_int_eq (X509_cmp (x509 , rcert ), 0 );
179
+ if (rsp != NULL ) {
180
+ if (do_get && real_server ) {
181
+ char rtext [sizeof (DOCTYPE_HTML )];
182
+
183
+ res = TEST_int_eq (BIO_gets (rsp , rtext , sizeof (rtext )),
184
+ sizeof (DOCTYPE_HTML ) - 1 )
185
+ && TEST_str_eq (rtext , DOCTYPE_HTML );
186
+ } else if (do_txt ) {
187
+ char rtext [sizeof (text1 ) + 1 /* more space than needed */ ];
188
+
189
+ res = TEST_int_eq (BIO_gets (rsp , rtext , sizeof (rtext )),
190
+ sizeof (text1 ) - 1 )
191
+ && TEST_str_eq (rtext , text1 );
192
+ } else {
193
+ X509 * rcert = d2i_X509_bio (rsp , NULL );
194
+
195
+ res = TEST_ptr (rcert ) && TEST_int_eq (X509_cmp (x509 , rcert ), 0 );
196
+ X509_free (rcert );
197
+ }
198
+ BIO_free (rsp );
199
+ }
147
200
148
201
err :
149
- X509_free (rcert );
150
202
BIO_free (req );
151
203
BIO_free (wbio );
152
204
BIO_free (rbio );
@@ -159,8 +211,8 @@ static int test_http_keep_alive(char version, int keep_alive, int kept_alive)
159
211
BIO * wbio = BIO_new (BIO_s_mem ());
160
212
BIO * rbio = BIO_new (BIO_s_mem ());
161
213
BIO * rsp ;
162
- server_args mock_args = { NULL , '0' , 0 };
163
214
const char * const content_type = "application/x-x509-ca-cert" ;
215
+ server_args mock_args = { NULL , content_type , NULL , '0' , 0 };
164
216
OSSL_HTTP_REQ_CTX * rctx = NULL ;
165
217
int i , res = 0 ;
166
218
@@ -306,14 +358,24 @@ static int test_http_url_invalid_path(void)
306
358
return test_http_url_invalid ("https://[FF01::101]pkix" );
307
359
}
308
360
361
+ static int test_http_get_txt (void )
362
+ {
363
+ return test_http_method (1 /* GET */ , 1 );
364
+ }
365
+
366
+ static int test_http_post_txt (void )
367
+ {
368
+ return test_http_method (0 /* POST */ , 1 );
369
+ }
370
+
309
371
static int test_http_get_x509 (void )
310
372
{
311
- return test_http_x509 ( 1 );
373
+ return test_http_method ( 1 /* GET */ , 0 ); /* includes redirection */
312
374
}
313
375
314
376
static int test_http_post_x509 (void )
315
377
{
316
- return test_http_x509 ( 0 );
378
+ return test_http_method ( 0 /* POST */ , 0 );
317
379
}
318
380
319
381
static int test_http_keep_alive_0_no_no (void )
@@ -380,6 +442,8 @@ int setup_tests(void)
380
442
ADD_TEST (test_http_url_invalid_prefix );
381
443
ADD_TEST (test_http_url_invalid_port );
382
444
ADD_TEST (test_http_url_invalid_path );
445
+ ADD_TEST (test_http_get_txt );
446
+ ADD_TEST (test_http_post_txt );
383
447
ADD_TEST (test_http_get_x509 );
384
448
ADD_TEST (test_http_post_x509 );
385
449
ADD_TEST (test_http_keep_alive_0_no_no );
0 commit comments