]>
git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/test/embed.c
1 // Copyright (c) 2004-2009 Sergey Lyubka
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // Unit test for the civetweb web server. Tests embedded API.
33 #if !defined(LISTENING_PORT)
34 #define LISTENING_PORT "23456"
37 static const char *standard_reply
= "HTTP/1.1 200 OK\r\n"
38 "Content-Type: text/plain\r\n"
39 "Connection: close\r\n\r\n";
41 static void test_get_var(struct mg_connection
*conn
,
42 const struct mg_request_info
*ri
) {
48 mg_printf(conn
, "%s", standard_reply
);
52 cl
= mg_get_header(conn
, "Content-Length");
53 mg_printf(conn
, "cl: %p\n", cl
);
54 if ((!strcmp(ri
->request_method
, "POST") ||
55 !strcmp(ri
->request_method
, "PUT"))
58 buf
= malloc(buf_len
);
59 /* Read in two pieces, to test continuation */
61 mg_read(conn
, buf
, 2);
62 mg_read(conn
, buf
+ 2, buf_len
- 2);
64 mg_read(conn
, buf
, buf_len
);
66 } else if (ri
->query_string
!= NULL
) {
67 buf_len
= strlen(ri
->query_string
);
68 buf
= malloc(buf_len
+ 1);
69 strcpy(buf
, ri
->query_string
);
71 var
= malloc(buf_len
+ 1);
72 var_len
= mg_get_var(buf
, buf_len
, "my_var", var
, buf_len
+ 1);
73 mg_printf(conn
, "Value: [%s]\n", var
);
74 mg_printf(conn
, "Value size: [%d]\n", var_len
);
79 static void test_get_header(struct mg_connection
*conn
,
80 const struct mg_request_info
*ri
) {
84 mg_printf(conn
, "%s", standard_reply
);
85 printf("HTTP headers: %d\n", ri
->num_headers
);
86 for (i
= 0; i
< ri
->num_headers
; i
++) {
87 printf("[%s]: [%s]\n", ri
->http_headers
[i
].name
, ri
->http_headers
[i
].value
);
90 value
= mg_get_header(conn
, "Host");
92 mg_printf(conn
, "Value: [%s]", value
);
96 static void test_get_request_info(struct mg_connection
*conn
,
97 const struct mg_request_info
*ri
) {
100 mg_printf(conn
, "%s", standard_reply
);
102 mg_printf(conn
, "Method: [%s]\n", ri
->request_method
);
103 mg_printf(conn
, "URI: [%s]\n", ri
->uri
);
104 mg_printf(conn
, "HTTP version: [%s]\n", ri
->http_version
);
106 for (i
= 0; i
< ri
->num_headers
; i
++) {
107 mg_printf(conn
, "HTTP header [%s]: [%s]\n",
108 ri
->http_headers
[i
].name
,
109 ri
->http_headers
[i
].value
);
112 mg_printf(conn
, "Query string: [%s]\n",
113 ri
->query_string
? ri
->query_string
: "");
114 mg_printf(conn
, "Remote IP: [%lu]\n", ri
->remote_ip
);
115 mg_printf(conn
, "Remote port: [%d]\n", ri
->remote_port
);
116 mg_printf(conn
, "Remote user: [%s]\n",
117 ri
->remote_user
? ri
->remote_user
: "");
120 static void test_error(struct mg_connection
*conn
,
121 const struct mg_request_info
*ri
) {
122 int status
= (int) ri
->ev_data
;
123 mg_printf(conn
, "HTTP/1.1 %d XX\r\n"
124 "Conntection: close\r\n\r\n", status
);
125 mg_printf(conn
, "Error: [%d]", status
);
128 static void test_post(struct mg_connection
*conn
,
129 const struct mg_request_info
*ri
) {
134 mg_printf(conn
, "%s", standard_reply
);
135 if (strcmp(ri
->request_method
, "POST") == 0 &&
136 (cl
= mg_get_header(conn
, "Content-Length")) != NULL
) {
138 if ((buf
= malloc(len
)) != NULL
) {
139 mg_write(conn
, buf
, len
);
145 static const struct test_config
{
148 void (*func
)(struct mg_connection
*, const struct mg_request_info
*);
150 {MG_NEW_REQUEST
, "/test_get_header", &test_get_header
},
151 {MG_NEW_REQUEST
, "/test_get_var", &test_get_var
},
152 {MG_NEW_REQUEST
, "/test_get_request_info", &test_get_request_info
},
153 {MG_NEW_REQUEST
, "/test_post", &test_post
},
154 {MG_HTTP_ERROR
, "", &test_error
},
158 static void *callback(enum mg_event event
,
159 struct mg_connection
*conn
) {
160 const struct mg_request_info
*request_info
= mg_get_request_info(conn
);
163 for (i
= 0; test_config
[i
].uri
!= NULL
; i
++) {
164 if (event
== test_config
[i
].event
&&
165 (event
== MG_HTTP_ERROR
||
166 !strcmp(request_info
->uri
, test_config
[i
].uri
))) {
167 test_config
[i
].func(conn
, request_info
);
176 struct mg_context
*ctx
;
177 const char *options
[] = {"listening_ports", LISTENING_PORT
, NULL
};
179 ctx
= mg_start(callback
, NULL
, options
);