1 | /* |
---|
2 | * Copyright (c) 2012 embedded brains GmbH. All rights reserved. |
---|
3 | * |
---|
4 | * embedded brains GmbH |
---|
5 | * Obere Lagerstr. 30 |
---|
6 | * 82178 Puchheim |
---|
7 | * Germany |
---|
8 | * <rtems@embedded-brains.de> |
---|
9 | * |
---|
10 | * The license and distribution terms for this file may be |
---|
11 | * found in the file LICENSE in this distribution or at |
---|
12 | * http://www.rtems.com/license/LICENSE. |
---|
13 | */ |
---|
14 | |
---|
15 | #ifdef HAVE_CONFIG_H |
---|
16 | #include "config.h" |
---|
17 | #endif |
---|
18 | |
---|
19 | #include <rtems.h> |
---|
20 | |
---|
21 | #include <tmacros.h> |
---|
22 | |
---|
23 | #include <rtems/rtems_bsdnet.h> |
---|
24 | |
---|
25 | #include <stdio.h> |
---|
26 | #include <mghttpd/mongoose.h> |
---|
27 | |
---|
28 | #include <rtems/imfs.h> |
---|
29 | #include <rtems/error.h> |
---|
30 | #include "init_fs.h" |
---|
31 | |
---|
32 | #include "test-http-client.h" |
---|
33 | |
---|
34 | #define TARFILE_START init_fs_tar |
---|
35 | #define TARFILE_SIZE init_fs_tar_size |
---|
36 | |
---|
37 | #define CBACKTEST_URI "/callbacktest.txt" |
---|
38 | #define CBACKTEST_TXT "HTTP/1.1 200 OK\r\n" \ |
---|
39 | "Content-Type: text/plain\r\n" \ |
---|
40 | "Content-Length: 47\r\n" \ |
---|
41 | "\r\n" \ |
---|
42 | "This is a message from the callback function.\r\n" |
---|
43 | |
---|
44 | #define INDEX_HTML "HTTP/1.1 200 OK\r\n" \ |
---|
45 | "Date: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n" \ |
---|
46 | "Last-Modified: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n" \ |
---|
47 | "Etag: \"21dae501.a2\"\r\n" \ |
---|
48 | "Content-Type: text/html\r\n" \ |
---|
49 | "Content-Length: 162\r\n" \ |
---|
50 | "Connection: close\r\n" \ |
---|
51 | "Accept-Ranges: bytes\r\n" \ |
---|
52 | "\r\n" \ |
---|
53 | "<html>\r\n" \ |
---|
54 | "<head>\r\n" \ |
---|
55 | "<title>Second Instance</title>\r\n" \ |
---|
56 | "</head>\r\n" \ |
---|
57 | "\r\n" \ |
---|
58 | "<body>\r\n" \ |
---|
59 | "<h1>Second Instance</h1>\r\n" \ |
---|
60 | "A test page for the Mongoose web server on RTEMS.\r\n" \ |
---|
61 | "</body>\r\n" \ |
---|
62 | "</html>\r\n" |
---|
63 | |
---|
64 | #define DATE_TAG "Date: " |
---|
65 | #define LASTMOD_TAG "Last-Modified: " |
---|
66 | #define TIMESTAMP_SIZE (sizeof("Fri, 01 Jan 1988 00:00:26 GMT") - 1) |
---|
67 | |
---|
68 | #define BUFFERSIZE 1024 |
---|
69 | |
---|
70 | static void test_tarfs_load(void) |
---|
71 | { |
---|
72 | rtems_status_code sc; |
---|
73 | |
---|
74 | printf("Loading tarfs image ... "); |
---|
75 | sc = rtems_tarfs_load("/",(void *)TARFILE_START, TARFILE_SIZE); |
---|
76 | if (sc != RTEMS_SUCCESSFUL) { |
---|
77 | printf ("error: untar failed: %s\n", rtems_status_text (sc)); |
---|
78 | rtems_test_exit(1); |
---|
79 | } |
---|
80 | printf ("successful\n"); |
---|
81 | } |
---|
82 | |
---|
83 | typedef struct { |
---|
84 | char *string; |
---|
85 | int size; |
---|
86 | } printctx; |
---|
87 | |
---|
88 | static void *callback(enum mg_event event, |
---|
89 | struct mg_connection *conn, |
---|
90 | const struct mg_request_info *request_info) |
---|
91 | { |
---|
92 | if (event == MG_NEW_REQUEST) { |
---|
93 | int cbacktest = strncmp(request_info->uri, CBACKTEST_URI, sizeof(CBACKTEST_URI)); |
---|
94 | if (cbacktest == 0) |
---|
95 | { |
---|
96 | mg_write(conn, CBACKTEST_TXT, sizeof(CBACKTEST_TXT)); |
---|
97 | |
---|
98 | /* Mark as processed */ |
---|
99 | return ""; |
---|
100 | } |
---|
101 | } |
---|
102 | return NULL; |
---|
103 | } |
---|
104 | |
---|
105 | static void test_mg_index_html(void) |
---|
106 | { |
---|
107 | httpc_context httpc_ctx; |
---|
108 | char *buffer = malloc(BUFFERSIZE); |
---|
109 | char *workpos = buffer; |
---|
110 | bool brv = false; |
---|
111 | int rv = 0; |
---|
112 | |
---|
113 | rtems_test_assert(buffer != NULL); |
---|
114 | |
---|
115 | puts("=== Get the index.html from second Mongoose instance:"); |
---|
116 | |
---|
117 | httpc_init_context(&httpc_ctx); |
---|
118 | brv = httpc_open_connection(&httpc_ctx, "127.0.0.1", 8080); |
---|
119 | rtems_test_assert(brv); |
---|
120 | brv = httpc_send_request(&httpc_ctx, "GET /index.html", buffer, BUFFERSIZE); |
---|
121 | rtems_test_assert(brv); |
---|
122 | brv = httpc_close_connection(&httpc_ctx); |
---|
123 | rtems_test_assert(brv); |
---|
124 | puts(buffer); |
---|
125 | |
---|
126 | /* remove timestamps from html-header */ |
---|
127 | workpos = strstr(buffer, DATE_TAG); |
---|
128 | rtems_test_assert(workpos != NULL); |
---|
129 | workpos += sizeof(DATE_TAG) - 1; |
---|
130 | memset(workpos, 'x', TIMESTAMP_SIZE); |
---|
131 | |
---|
132 | workpos = strstr(buffer, LASTMOD_TAG); |
---|
133 | rtems_test_assert(workpos != NULL); |
---|
134 | workpos += sizeof(LASTMOD_TAG) - 1; |
---|
135 | memset(workpos, 'x', TIMESTAMP_SIZE); |
---|
136 | |
---|
137 | rv = strcmp(buffer, INDEX_HTML); |
---|
138 | rtems_test_assert(rv == 0); |
---|
139 | |
---|
140 | puts("=== OK"); |
---|
141 | |
---|
142 | free(buffer); |
---|
143 | } |
---|
144 | |
---|
145 | static void test_mg_callback(void) |
---|
146 | { |
---|
147 | httpc_context httpc_ctx; |
---|
148 | char *buffer = malloc(BUFFERSIZE); |
---|
149 | bool brv = false; |
---|
150 | int rv = 0; |
---|
151 | |
---|
152 | rtems_test_assert(buffer != NULL); |
---|
153 | |
---|
154 | puts("=== Get a page generated from a callback function from" \ |
---|
155 | " first Mongoose instance:"); |
---|
156 | |
---|
157 | httpc_init_context(&httpc_ctx); |
---|
158 | brv = httpc_open_connection(&httpc_ctx, "127.0.0.1", 80); |
---|
159 | rtems_test_assert(brv); |
---|
160 | brv = httpc_send_request(&httpc_ctx, "GET " CBACKTEST_URI, buffer, BUFFERSIZE); |
---|
161 | rtems_test_assert(brv); |
---|
162 | brv = httpc_close_connection(&httpc_ctx); |
---|
163 | rtems_test_assert(brv); |
---|
164 | puts(buffer); |
---|
165 | rv = strcmp(buffer, CBACKTEST_TXT); |
---|
166 | rtems_test_assert(rv == 0); |
---|
167 | |
---|
168 | puts("=== OK"); |
---|
169 | |
---|
170 | free(buffer); |
---|
171 | } |
---|
172 | |
---|
173 | static void test_mongoose(void) |
---|
174 | { |
---|
175 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
176 | const char *options[] = { |
---|
177 | "listening_ports", "80", |
---|
178 | "document_root", "/www/", |
---|
179 | "num_threads", "1", |
---|
180 | "max_request_size", "2048", |
---|
181 | "thread_stack_size", "16384", |
---|
182 | "thread_priority", "250", |
---|
183 | "thread_policy", "o", |
---|
184 | NULL}; |
---|
185 | const char *options2[] = { |
---|
186 | "listening_ports", "8080", |
---|
187 | "document_root", "/www2/", |
---|
188 | "num_threads", "1", |
---|
189 | "thread_stack_size", "16384", |
---|
190 | "max_request_size", "2048", |
---|
191 | NULL}; |
---|
192 | |
---|
193 | struct mg_context *mg1 = mg_start(&callback, NULL, options); |
---|
194 | struct mg_context *mg2 = mg_start(NULL, NULL, options2); |
---|
195 | |
---|
196 | test_mg_index_html(); |
---|
197 | test_mg_callback(); |
---|
198 | |
---|
199 | mg_stop(mg1); |
---|
200 | mg_stop(mg2); |
---|
201 | } |
---|
202 | |
---|
203 | static void Init(rtems_task_argument arg) |
---|
204 | { |
---|
205 | int rv = 0; |
---|
206 | |
---|
207 | puts("\n\n*** TEST MGHTTPD 01 ***"); |
---|
208 | |
---|
209 | rv = rtems_bsdnet_initialize_network(); |
---|
210 | rtems_test_assert(rv == 0); |
---|
211 | |
---|
212 | test_tarfs_load(); |
---|
213 | |
---|
214 | test_mongoose(); |
---|
215 | |
---|
216 | puts("*** END OF TEST MGHTTPD 01 ***"); |
---|
217 | |
---|
218 | rtems_test_exit(0); |
---|
219 | } |
---|
220 | |
---|
221 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
222 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
223 | |
---|
224 | #define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM |
---|
225 | |
---|
226 | #define CONFIGURE_FILESYSTEM_IMFS |
---|
227 | |
---|
228 | #define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 16 |
---|
229 | |
---|
230 | #define CONFIGURE_UNLIMITED_OBJECTS |
---|
231 | |
---|
232 | #define CONFIGURE_UNIFIED_WORK_AREAS |
---|
233 | |
---|
234 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
235 | |
---|
236 | #define CONFIGURE_INIT_TASK_STACK_SIZE (16 * 1024) |
---|
237 | |
---|
238 | #define CONFIGURE_INIT |
---|
239 | |
---|
240 | #include <rtems/confdefs.h> |
---|