diff options
author | Christian Grothoff <christian@grothoff.org> | 2008-08-24 18:03:05 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2008-08-24 18:03:05 +0000 |
commit | 9181dd0a072670d61a2e6839f23d7e33165ca089 (patch) | |
tree | 88806884c43d3579c3a357d8b26d1843e009ceab /doc | |
parent | 484e65d76e26874485e906f5221156442bb52467 (diff) | |
download | libmicrohttpd-9181dd0a072670d61a2e6839f23d7e33165ca089.tar.gz libmicrohttpd-9181dd0a072670d61a2e6839f23d7e33165ca089.zip |
indenting
Diffstat (limited to 'doc')
-rw-r--r-- | doc/examples/basicauthentication.c | 143 | ||||
-rw-r--r-- | doc/examples/hellobrowser.c | 24 | ||||
-rw-r--r-- | doc/examples/logging.c | 23 | ||||
-rw-r--r-- | doc/examples/responseheaders.c | 84 | ||||
-rw-r--r-- | doc/examples/simplepost.c | 144 |
5 files changed, 250 insertions, 168 deletions
diff --git a/doc/examples/basicauthentication.c b/doc/examples/basicauthentication.c index fd457c22..b3af2e97 100644 --- a/doc/examples/basicauthentication.c +++ b/doc/examples/basicauthentication.c | |||
@@ -11,60 +11,75 @@ | |||
11 | #define PASSWORD "and his password" | 11 | #define PASSWORD "and his password" |
12 | 12 | ||
13 | 13 | ||
14 | char* string_to_base64 (const char *message); | 14 | char *string_to_base64 (const char *message); |
15 | 15 | ||
16 | 16 | ||
17 | int ask_for_authentication (struct MHD_Connection *connection, const char *realm) | 17 | int |
18 | ask_for_authentication (struct MHD_Connection *connection, const char *realm) | ||
18 | { | 19 | { |
19 | int ret; | 20 | int ret; |
20 | struct MHD_Response *response; | 21 | struct MHD_Response *response; |
21 | char *headervalue; | 22 | char *headervalue; |
22 | const char *strbase = "Basic realm="; | 23 | const char *strbase = "Basic realm="; |
23 | 24 | ||
24 | response = MHD_create_response_from_data (0, NULL, MHD_NO, MHD_NO); | 25 | response = MHD_create_response_from_data (0, NULL, MHD_NO, MHD_NO); |
25 | if (!response) return MHD_NO; | 26 | if (!response) |
26 | 27 | return MHD_NO; | |
28 | |||
27 | headervalue = malloc (strlen (strbase) + strlen (realm) + 1); | 29 | headervalue = malloc (strlen (strbase) + strlen (realm) + 1); |
28 | if (!headervalue) return MHD_NO; | 30 | if (!headervalue) |
31 | return MHD_NO; | ||
29 | 32 | ||
30 | strcpy (headervalue, strbase); | 33 | strcpy (headervalue, strbase); |
31 | strcat (headervalue, realm); | 34 | strcat (headervalue, realm); |
32 | 35 | ||
33 | ret = MHD_add_response_header (response, "WWW-Authenticate", headervalue); | 36 | ret = MHD_add_response_header (response, "WWW-Authenticate", headervalue); |
34 | free (headervalue); | 37 | free (headervalue); |
35 | if (!ret) {MHD_destroy_response (response); return MHD_NO;} | 38 | if (!ret) |
39 | { | ||
40 | MHD_destroy_response (response); | ||
41 | return MHD_NO; | ||
42 | } | ||
36 | 43 | ||
37 | ret = MHD_queue_response (connection, MHD_HTTP_UNAUTHORIZED, response); | 44 | ret = MHD_queue_response (connection, MHD_HTTP_UNAUTHORIZED, response); |
38 | 45 | ||
39 | MHD_destroy_response (response); | 46 | MHD_destroy_response (response); |
40 | 47 | ||
41 | return ret; | 48 | return ret; |
42 | } | 49 | } |
43 | 50 | ||
44 | int is_authenticated (struct MHD_Connection *connection, | 51 | int |
45 | const char *username, const char *password) | 52 | is_authenticated (struct MHD_Connection *connection, |
53 | const char *username, const char *password) | ||
46 | { | 54 | { |
47 | const char *headervalue; | 55 | const char *headervalue; |
48 | char *expected_b64, *expected; | 56 | char *expected_b64, *expected; |
49 | const char *strbase = "Basic "; | 57 | const char *strbase = "Basic "; |
50 | int authenticated; | 58 | int authenticated; |
51 | 59 | ||
52 | headervalue = MHD_lookup_connection_value (connection, MHD_HEADER_KIND, "Authorization"); | 60 | headervalue = |
53 | if (NULL == headervalue) return 0; | 61 | MHD_lookup_connection_value (connection, MHD_HEADER_KIND, |
54 | if (0 != strncmp (headervalue, strbase, strlen (strbase))) return 0; | 62 | "Authorization"); |
63 | if (NULL == headervalue) | ||
64 | return 0; | ||
65 | if (0 != strncmp (headervalue, strbase, strlen (strbase))) | ||
66 | return 0; | ||
55 | 67 | ||
56 | expected = malloc (strlen (username) + 1 + strlen (password) + 1); | 68 | expected = malloc (strlen (username) + 1 + strlen (password) + 1); |
57 | if (NULL == expected) return 0; | 69 | if (NULL == expected) |
70 | return 0; | ||
58 | 71 | ||
59 | strcpy (expected, username); | 72 | strcpy (expected, username); |
60 | strcat (expected, ":"); | 73 | strcat (expected, ":"); |
61 | strcat (expected, password); | 74 | strcat (expected, password); |
62 | 75 | ||
63 | expected_b64 = string_to_base64 (expected); | 76 | expected_b64 = string_to_base64 (expected); |
64 | if (NULL == expected_b64) return 0; | 77 | if (NULL == expected_b64) |
65 | 78 | return 0; | |
79 | |||
66 | strcpy (expected, strbase); | 80 | strcpy (expected, strbase); |
67 | authenticated = (strcmp (headervalue + strlen (strbase), expected_b64) == 0); | 81 | authenticated = |
82 | (strcmp (headervalue + strlen (strbase), expected_b64) == 0); | ||
68 | 83 | ||
69 | free (expected_b64); | 84 | free (expected_b64); |
70 | 85 | ||
@@ -72,15 +87,19 @@ int is_authenticated (struct MHD_Connection *connection, | |||
72 | } | 87 | } |
73 | 88 | ||
74 | 89 | ||
75 | int secret_page (struct MHD_Connection *connection) | 90 | int |
91 | secret_page (struct MHD_Connection *connection) | ||
76 | { | 92 | { |
77 | int ret; | 93 | int ret; |
78 | struct MHD_Response *response; | 94 | struct MHD_Response *response; |
79 | const char *page = "<html><body>A secret.</body></html>"; | 95 | const char *page = "<html><body>A secret.</body></html>"; |
80 | 96 | ||
81 | response = MHD_create_response_from_data (strlen (page), (void*) page, MHD_NO, MHD_NO); | 97 | response = |
82 | if (!response) return MHD_NO; | 98 | MHD_create_response_from_data (strlen (page), (void *) page, MHD_NO, |
83 | 99 | MHD_NO); | |
100 | if (!response) | ||
101 | return MHD_NO; | ||
102 | |||
84 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); | 103 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); |
85 | MHD_destroy_response (response); | 104 | MHD_destroy_response (response); |
86 | 105 | ||
@@ -88,64 +107,78 @@ int secret_page (struct MHD_Connection *connection) | |||
88 | } | 107 | } |
89 | 108 | ||
90 | 109 | ||
91 | int answer_to_connection (void *cls, struct MHD_Connection *connection, | 110 | int |
92 | const char *url, const char *method, const char *version, | 111 | answer_to_connection (void *cls, struct MHD_Connection *connection, |
93 | const char *upload_data, unsigned int *upload_data_size, | 112 | const char *url, const char *method, |
94 | void **con_cls) | 113 | const char *version, const char *upload_data, |
114 | unsigned int *upload_data_size, void **con_cls) | ||
95 | { | 115 | { |
96 | if (0 != strcmp(method, "GET")) return MHD_NO; | 116 | if (0 != strcmp (method, "GET")) |
97 | if (NULL == *con_cls) {*con_cls = connection; return MHD_YES;} | 117 | return MHD_NO; |
118 | if (NULL == *con_cls) | ||
119 | { | ||
120 | *con_cls = connection; | ||
121 | return MHD_YES; | ||
122 | } | ||
123 | |||
124 | if (!is_authenticated (connection, USER, PASSWORD)) | ||
125 | return ask_for_authentication (connection, REALM); | ||
98 | 126 | ||
99 | if (!is_authenticated (connection, USER, PASSWORD)) | ||
100 | return ask_for_authentication (connection, REALM); | ||
101 | |||
102 | return secret_page (connection); | 127 | return secret_page (connection); |
103 | } | 128 | } |
104 | 129 | ||
105 | 130 | ||
106 | int main () | 131 | int |
132 | main () | ||
107 | { | 133 | { |
108 | struct MHD_Daemon *daemon; | 134 | struct MHD_Daemon *daemon; |
109 | 135 | ||
110 | daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, PORT, NULL, NULL, | 136 | daemon = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY, PORT, NULL, NULL, |
111 | &answer_to_connection, NULL, MHD_OPTION_END); | 137 | &answer_to_connection, NULL, MHD_OPTION_END); |
112 | if (NULL == daemon) return 1; | 138 | if (NULL == daemon) |
139 | return 1; | ||
113 | 140 | ||
114 | getchar (); | 141 | getchar (); |
115 | 142 | ||
116 | MHD_stop_daemon (daemon); | 143 | MHD_stop_daemon (daemon); |
117 | return 0; | 144 | return 0; |
118 | } | 145 | } |
119 | 146 | ||
120 | 147 | ||
121 | char* string_to_base64 (const char *message) | 148 | char * |
149 | string_to_base64 (const char *message) | ||
122 | { | 150 | { |
123 | const char *lookup = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | 151 | const char *lookup = |
152 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | ||
124 | unsigned long l; | 153 | unsigned long l; |
125 | int i; | 154 | int i; |
126 | char *tmp; | 155 | char *tmp; |
127 | size_t length = strlen (message); | 156 | size_t length = strlen (message); |
128 | 157 | ||
129 | tmp = malloc (length * 2); | 158 | tmp = malloc (length * 2); |
130 | if (NULL == tmp) return tmp; | 159 | if (NULL == tmp) |
160 | return tmp; | ||
131 | 161 | ||
132 | tmp[0] = 0; | 162 | tmp[0] = 0; |
133 | 163 | ||
134 | for (i = 0; i < length; i += 3) | 164 | for (i = 0; i < length; i += 3) |
135 | { | 165 | { |
136 | l = ( ((unsigned long) message[i])<<16 ) | 166 | l = (((unsigned long) message[i]) << 16) |
137 | | (((i+1) < length) ? (((unsigned long) message[i+1])<<8 ) : 0 ) | 167 | | (((i + 1) < length) ? (((unsigned long) message[i + 1]) << 8) : 0) |
138 | | (((i+2) < length) ? ( (unsigned long) message[i+2] ) : 0 ); | 168 | | (((i + 2) < length) ? ((unsigned long) message[i + 2]) : 0); |
169 | |||
139 | 170 | ||
171 | strncat (tmp, &lookup[(l >> 18) & 0x3F], 1); | ||
172 | strncat (tmp, &lookup[(l >> 12) & 0x3F], 1); | ||
140 | 173 | ||
141 | strncat (tmp, &lookup[(l>>18) & 0x3F], 1); | 174 | if (i + 1 < length) |
142 | strncat (tmp, &lookup[(l>>12) & 0x3F], 1); | 175 | strncat (tmp, &lookup[(l >> 6) & 0x3F], 1); |
143 | 176 | if (i + 2 < length) | |
144 | if (i+1 < length) strncat (tmp, &lookup[(l>> 6) & 0x3F], 1); | 177 | strncat (tmp, &lookup[l & 0x3F], 1); |
145 | if (i+2 < length) strncat (tmp, &lookup[l & 0x3F], 1); | ||
146 | } | 178 | } |
147 | 179 | ||
148 | if (length % 3) strncat (tmp, "===", 3-length%3); | 180 | if (length % 3) |
149 | 181 | strncat (tmp, "===", 3 - length % 3); | |
182 | |||
150 | return tmp; | 183 | return tmp; |
151 | } | 184 | } |
diff --git a/doc/examples/hellobrowser.c b/doc/examples/hellobrowser.c index 15cb5788..02ac8ef1 100644 --- a/doc/examples/hellobrowser.c +++ b/doc/examples/hellobrowser.c | |||
@@ -5,30 +5,36 @@ | |||
5 | 5 | ||
6 | #define PORT 8888 | 6 | #define PORT 8888 |
7 | 7 | ||
8 | int answer_to_connection (void *cls, struct MHD_Connection *connection, const char *url, | 8 | int |
9 | const char *method, const char *version, const char *upload_data, | 9 | answer_to_connection (void *cls, struct MHD_Connection *connection, |
10 | unsigned int *upload_data_size, void **con_cls) | 10 | const char *url, const char *method, |
11 | const char *version, const char *upload_data, | ||
12 | unsigned int *upload_data_size, void **con_cls) | ||
11 | { | 13 | { |
12 | const char *page = "<html><body>Hello, browser!</body></html>"; | 14 | const char *page = "<html><body>Hello, browser!</body></html>"; |
13 | struct MHD_Response *response; | 15 | struct MHD_Response *response; |
14 | int ret; | 16 | int ret; |
15 | 17 | ||
16 | response = MHD_create_response_from_data (strlen (page), (void*) page, MHD_NO, MHD_NO); | 18 | response = |
19 | MHD_create_response_from_data (strlen (page), (void *) page, MHD_NO, | ||
20 | MHD_NO); | ||
17 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); | 21 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); |
18 | MHD_destroy_response (response); | 22 | MHD_destroy_response (response); |
19 | 23 | ||
20 | return ret; | 24 | return ret; |
21 | } | 25 | } |
22 | 26 | ||
23 | int main () | 27 | int |
28 | main () | ||
24 | { | 29 | { |
25 | struct MHD_Daemon *daemon; | 30 | struct MHD_Daemon *daemon; |
26 | 31 | ||
27 | daemon = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY, PORT, NULL, NULL, | 32 | daemon = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY, PORT, NULL, NULL, |
28 | &answer_to_connection, NULL, MHD_OPTION_END); | 33 | &answer_to_connection, NULL, MHD_OPTION_END); |
29 | if (NULL == daemon) return 1; | 34 | if (NULL == daemon) |
35 | return 1; | ||
30 | 36 | ||
31 | getchar (); | 37 | getchar (); |
32 | 38 | ||
33 | MHD_stop_daemon (daemon); | 39 | MHD_stop_daemon (daemon); |
34 | return 0; | 40 | return 0; |
diff --git a/doc/examples/logging.c b/doc/examples/logging.c index 213c12bf..d1dd4f1f 100644 --- a/doc/examples/logging.c +++ b/doc/examples/logging.c | |||
@@ -6,32 +6,39 @@ | |||
6 | #define PORT 8888 | 6 | #define PORT 8888 |
7 | 7 | ||
8 | 8 | ||
9 | int print_out_key (void *cls, enum MHD_ValueKind kind, const char *key, const char *value) | 9 | int |
10 | print_out_key (void *cls, enum MHD_ValueKind kind, const char *key, | ||
11 | const char *value) | ||
10 | { | 12 | { |
11 | printf ("%s = %s\n", key, value); | 13 | printf ("%s = %s\n", key, value); |
12 | return MHD_YES; | 14 | return MHD_YES; |
13 | } | 15 | } |
14 | 16 | ||
15 | int answer_to_connection (void *cls, struct MHD_Connection *connection, const char *url, | 17 | int |
16 | const char *method, const char *version, const char *upload_data, | 18 | answer_to_connection (void *cls, struct MHD_Connection *connection, |
17 | unsigned int *upload_data_size, void **con_cls) | 19 | const char *url, const char *method, |
20 | const char *version, const char *upload_data, | ||
21 | unsigned int *upload_data_size, void **con_cls) | ||
18 | { | 22 | { |
19 | printf ("New request %s for %s using version %s\n", method, url, version); | 23 | printf ("New request %s for %s using version %s\n", method, url, version); |
20 | 24 | ||
21 | MHD_get_connection_values (connection, MHD_HEADER_KIND, print_out_key, NULL); | 25 | MHD_get_connection_values (connection, MHD_HEADER_KIND, print_out_key, |
26 | NULL); | ||
22 | 27 | ||
23 | return MHD_NO; | 28 | return MHD_NO; |
24 | } | 29 | } |
25 | 30 | ||
26 | int main () | 31 | int |
32 | main () | ||
27 | { | 33 | { |
28 | struct MHD_Daemon *daemon; | 34 | struct MHD_Daemon *daemon; |
29 | 35 | ||
30 | daemon = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY, PORT, NULL, NULL, | 36 | daemon = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY, PORT, NULL, NULL, |
31 | &answer_to_connection, NULL, MHD_OPTION_END); | 37 | &answer_to_connection, NULL, MHD_OPTION_END); |
32 | if (NULL == daemon) return 1; | 38 | if (NULL == daemon) |
39 | return 1; | ||
33 | 40 | ||
34 | getchar (); | 41 | getchar (); |
35 | 42 | ||
36 | MHD_stop_daemon (daemon); | 43 | MHD_stop_daemon (daemon); |
37 | return 0; | 44 | return 0; |
diff --git a/doc/examples/responseheaders.c b/doc/examples/responseheaders.c index 1555beed..31c3900d 100644 --- a/doc/examples/responseheaders.c +++ b/doc/examples/responseheaders.c | |||
@@ -9,31 +9,33 @@ | |||
9 | #define MIMETYPE "image/png" | 9 | #define MIMETYPE "image/png" |
10 | 10 | ||
11 | 11 | ||
12 | long get_file_size (const char *filename) | 12 | long |
13 | get_file_size (const char *filename) | ||
13 | { | 14 | { |
14 | FILE *fp; | 15 | FILE *fp; |
15 | 16 | ||
16 | fp = fopen (filename, "rb"); | 17 | fp = fopen (filename, "rb"); |
17 | if (fp) | 18 | if (fp) |
18 | { | 19 | { |
19 | long size; | 20 | long size; |
20 | 21 | ||
21 | if ( (0 != fseek (fp, 0, SEEK_END)) | 22 | if ((0 != fseek (fp, 0, SEEK_END)) || (-1 == (size = ftell (fp)))) |
22 | || (-1 == (size = ftell (fp))) ) | ||
23 | size = 0; | 23 | size = 0; |
24 | 24 | ||
25 | fclose (fp); | 25 | fclose (fp); |
26 | 26 | ||
27 | return size; | 27 | return size; |
28 | } | 28 | } |
29 | else | 29 | else |
30 | return 0; | 30 | return 0; |
31 | } | 31 | } |
32 | 32 | ||
33 | 33 | ||
34 | int answer_to_connection (void *cls, struct MHD_Connection *connection, const char *url, | 34 | int |
35 | const char *method, const char *version, const char *upload_data, | 35 | answer_to_connection (void *cls, struct MHD_Connection *connection, |
36 | unsigned int *upload_data_size, void **con_cls) | 36 | const char *url, const char *method, |
37 | const char *version, const char *upload_data, | ||
38 | unsigned int *upload_data_size, void **con_cls) | ||
37 | { | 39 | { |
38 | unsigned char *buffer = NULL; | 40 | unsigned char *buffer = NULL; |
39 | struct MHD_Response *response; | 41 | struct MHD_Response *response; |
@@ -41,44 +43,53 @@ int answer_to_connection (void *cls, struct MHD_Connection *connection, const ch | |||
41 | FILE *fp; | 43 | FILE *fp; |
42 | int ret = 0; | 44 | int ret = 0; |
43 | 45 | ||
44 | if (0 != strcmp(method, "GET")) return MHD_NO; | 46 | if (0 != strcmp (method, "GET")) |
47 | return MHD_NO; | ||
45 | 48 | ||
46 | size = get_file_size (FILENAME); | 49 | size = get_file_size (FILENAME); |
47 | if (size != 0) | 50 | if (size != 0) |
48 | { | 51 | { |
49 | fp = fopen (FILENAME, "rb"); | 52 | fp = fopen (FILENAME, "rb"); |
50 | if (fp) | 53 | if (fp) |
51 | { | 54 | { |
52 | buffer = malloc (size); | 55 | buffer = malloc (size); |
53 | 56 | ||
54 | if (buffer) | 57 | if (buffer) |
55 | if (size == fread (buffer, 1, size, fp)) ret = 1; | 58 | if (size == fread (buffer, 1, size, fp)) |
56 | 59 | ret = 1; | |
57 | fclose(fp); | 60 | |
58 | } | 61 | fclose (fp); |
62 | } | ||
59 | } | 63 | } |
60 | 64 | ||
61 | if (!ret) | 65 | if (!ret) |
62 | { | 66 | { |
63 | const char *errorstr = "<html><body>An internal server error has occured!\ | 67 | const char *errorstr = |
68 | "<html><body>An internal server error has occured!\ | ||
64 | </body></html>"; | 69 | </body></html>"; |
65 | 70 | ||
66 | if (buffer) free(buffer); | 71 | if (buffer) |
67 | 72 | free (buffer); | |
68 | response = MHD_create_response_from_data(strlen(errorstr), (void*)errorstr, | 73 | |
69 | MHD_NO, MHD_NO); | 74 | response = |
75 | MHD_create_response_from_data (strlen (errorstr), (void *) errorstr, | ||
76 | MHD_NO, MHD_NO); | ||
70 | 77 | ||
71 | if (response) | 78 | if (response) |
72 | { | 79 | { |
73 | ret = MHD_queue_response (connection, MHD_HTTP_INTERNAL_SERVER_ERROR, response); | 80 | ret = |
81 | MHD_queue_response (connection, MHD_HTTP_INTERNAL_SERVER_ERROR, | ||
82 | response); | ||
74 | MHD_destroy_response (response); | 83 | MHD_destroy_response (response); |
75 | 84 | ||
76 | return MHD_YES; | 85 | return MHD_YES; |
77 | } | 86 | } |
78 | else return MHD_NO; | 87 | else |
88 | return MHD_NO; | ||
79 | } | 89 | } |
80 | 90 | ||
81 | response = MHD_create_response_from_data (size, (void*)buffer, MHD_YES, MHD_NO); | 91 | response = |
92 | MHD_create_response_from_data (size, (void *) buffer, MHD_YES, MHD_NO); | ||
82 | 93 | ||
83 | MHD_add_response_header (response, "Content-Type", MIMETYPE); | 94 | MHD_add_response_header (response, "Content-Type", MIMETYPE); |
84 | 95 | ||
@@ -89,18 +100,19 @@ int answer_to_connection (void *cls, struct MHD_Connection *connection, const ch | |||
89 | } | 100 | } |
90 | 101 | ||
91 | 102 | ||
92 | int main () | 103 | int |
104 | main () | ||
93 | { | 105 | { |
94 | struct MHD_Daemon *daemon; | 106 | struct MHD_Daemon *daemon; |
95 | 107 | ||
96 | daemon = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY, PORT, NULL, NULL, | 108 | daemon = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY, PORT, NULL, NULL, |
97 | &answer_to_connection, NULL, MHD_OPTION_END); | 109 | &answer_to_connection, NULL, MHD_OPTION_END); |
98 | if (NULL == daemon) return 1; | 110 | if (NULL == daemon) |
111 | return 1; | ||
99 | 112 | ||
100 | getchar (); | 113 | getchar (); |
101 | 114 | ||
102 | MHD_stop_daemon (daemon); | 115 | MHD_stop_daemon (daemon); |
103 | 116 | ||
104 | return 0; | 117 | return 0; |
105 | } | 118 | } |
106 | |||
diff --git a/doc/examples/simplepost.c b/doc/examples/simplepost.c index 8df0a94d..6a1322db 100644 --- a/doc/examples/simplepost.c +++ b/doc/examples/simplepost.c | |||
@@ -15,30 +15,36 @@ struct connection_info_struct | |||
15 | { | 15 | { |
16 | int connectiontype; | 16 | int connectiontype; |
17 | char *answerstring; | 17 | char *answerstring; |
18 | struct MHD_PostProcessor *postprocessor; | 18 | struct MHD_PostProcessor *postprocessor; |
19 | }; | 19 | }; |
20 | 20 | ||
21 | const char* askpage = "<html><body>\ | 21 | const char *askpage = "<html><body>\ |
22 | What's your name, Sir?<br>\ | 22 | What's your name, Sir?<br>\ |
23 | <form action=\"/namepost\" method=\"post\">\ | 23 | <form action=\"/namepost\" method=\"post\">\ |
24 | <input name=\"name\" type=\"text\"\ | 24 | <input name=\"name\" type=\"text\"\ |
25 | <input type=\"submit\" value=\" Send \"></form>\ | 25 | <input type=\"submit\" value=\" Send \"></form>\ |
26 | </body></html>"; | 26 | </body></html>"; |
27 | 27 | ||
28 | const char* greatingpage = "<html><body><h1>Welcome, %s!</center></h1></body></html>"; | 28 | const char *greatingpage = |
29 | "<html><body><h1>Welcome, %s!</center></h1></body></html>"; | ||
29 | 30 | ||
30 | const char* errorpage = "<html><body>This doesn't seem to be right.</body></html>"; | 31 | const char *errorpage = |
32 | "<html><body>This doesn't seem to be right.</body></html>"; | ||
31 | 33 | ||
32 | 34 | ||
33 | int send_page (struct MHD_Connection *connection, const char* page) | 35 | int |
36 | send_page (struct MHD_Connection *connection, const char *page) | ||
34 | { | 37 | { |
35 | int ret; | 38 | int ret; |
36 | struct MHD_Response *response; | 39 | struct MHD_Response *response; |
37 | |||
38 | 40 | ||
39 | response = MHD_create_response_from_data (strlen (page), (void*) page, MHD_NO, MHD_NO); | 41 | |
40 | if (!response) return MHD_NO; | 42 | response = |
41 | 43 | MHD_create_response_from_data (strlen (page), (void *) page, MHD_NO, | |
44 | MHD_NO); | ||
45 | if (!response) | ||
46 | return MHD_NO; | ||
47 | |||
42 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); | 48 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); |
43 | MHD_destroy_response (response); | 49 | MHD_destroy_response (response); |
44 | 50 | ||
@@ -46,12 +52,15 @@ int send_page (struct MHD_Connection *connection, const char* page) | |||
46 | } | 52 | } |
47 | 53 | ||
48 | 54 | ||
49 | int iterate_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key, | 55 | int |
50 | const char *filename, const char *content_type, | 56 | iterate_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key, |
51 | const char *transfer_encoding, const char *data, size_t off, size_t size) | 57 | const char *filename, const char *content_type, |
58 | const char *transfer_encoding, const char *data, size_t off, | ||
59 | size_t size) | ||
52 | { | 60 | { |
53 | struct connection_info_struct *con_info = (struct connection_info_struct*) coninfo_cls; | 61 | struct connection_info_struct *con_info = |
54 | 62 | (struct connection_info_struct *) coninfo_cls; | |
63 | |||
55 | 64 | ||
56 | if (0 == strcmp (key, "name")) | 65 | if (0 == strcmp (key, "name")) |
57 | { | 66 | { |
@@ -59,12 +68,14 @@ int iterate_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key, | |||
59 | { | 68 | { |
60 | char *answerstring; | 69 | char *answerstring; |
61 | answerstring = malloc (MAXANSWERSIZE); | 70 | answerstring = malloc (MAXANSWERSIZE); |
62 | if (!answerstring) return MHD_NO; | 71 | if (!answerstring) |
63 | 72 | return MHD_NO; | |
73 | |||
64 | snprintf (answerstring, MAXANSWERSIZE, greatingpage, data); | 74 | snprintf (answerstring, MAXANSWERSIZE, greatingpage, data); |
65 | con_info->answerstring = answerstring; | 75 | con_info->answerstring = answerstring; |
66 | } | 76 | } |
67 | else con_info->answerstring = NULL; | 77 | else |
78 | con_info->answerstring = NULL; | ||
68 | 79 | ||
69 | return MHD_NO; | 80 | return MHD_NO; |
70 | } | 81 | } |
@@ -72,91 +83,104 @@ int iterate_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key, | |||
72 | return MHD_YES; | 83 | return MHD_YES; |
73 | } | 84 | } |
74 | 85 | ||
75 | void request_completed (void *cls, struct MHD_Connection *connection, void **con_cls, | 86 | void |
76 | enum MHD_RequestTerminationCode toe) | 87 | request_completed (void *cls, struct MHD_Connection *connection, |
88 | void **con_cls, enum MHD_RequestTerminationCode toe) | ||
77 | { | 89 | { |
78 | struct connection_info_struct *con_info = (struct connection_info_struct*) *con_cls; | 90 | struct connection_info_struct *con_info = |
91 | (struct connection_info_struct *) *con_cls; | ||
79 | 92 | ||
80 | 93 | ||
81 | if (NULL == con_info) return; | 94 | if (NULL == con_info) |
95 | return; | ||
82 | 96 | ||
83 | if (con_info->connectiontype == POST) | 97 | if (con_info->connectiontype == POST) |
84 | { | 98 | { |
85 | MHD_destroy_post_processor (con_info->postprocessor); | 99 | MHD_destroy_post_processor (con_info->postprocessor); |
86 | if (con_info->answerstring) free (con_info->answerstring); | 100 | if (con_info->answerstring) |
101 | free (con_info->answerstring); | ||
87 | } | 102 | } |
88 | 103 | ||
89 | free (con_info); | 104 | free (con_info); |
90 | *con_cls = NULL; | 105 | *con_cls = NULL; |
91 | } | 106 | } |
92 | 107 | ||
93 | 108 | ||
94 | int answer_to_connection (void *cls, struct MHD_Connection *connection, const char *url, | 109 | int |
95 | const char *method, const char *version, const char *upload_data, | 110 | answer_to_connection (void *cls, struct MHD_Connection *connection, |
96 | unsigned int *upload_data_size, void **con_cls) | 111 | const char *url, const char *method, |
112 | const char *version, const char *upload_data, | ||
113 | unsigned int *upload_data_size, void **con_cls) | ||
97 | { | 114 | { |
98 | if(NULL == *con_cls) | 115 | if (NULL == *con_cls) |
99 | { | 116 | { |
100 | struct connection_info_struct *con_info; | 117 | struct connection_info_struct *con_info; |
101 | 118 | ||
102 | con_info = malloc (sizeof (struct connection_info_struct)); | 119 | con_info = malloc (sizeof (struct connection_info_struct)); |
103 | if (NULL == con_info) return MHD_NO; | 120 | if (NULL == con_info) |
121 | return MHD_NO; | ||
104 | con_info->answerstring = NULL; | 122 | con_info->answerstring = NULL; |
105 | 123 | ||
106 | if (0 == strcmp (method, "POST")) | 124 | if (0 == strcmp (method, "POST")) |
107 | { | 125 | { |
108 | con_info->postprocessor = MHD_create_post_processor (connection, POSTBUFFERSIZE, | 126 | con_info->postprocessor = |
109 | iterate_post, (void*) con_info); | 127 | MHD_create_post_processor (connection, POSTBUFFERSIZE, |
128 | iterate_post, (void *) con_info); | ||
110 | 129 | ||
111 | if (NULL == con_info->postprocessor) | 130 | if (NULL == con_info->postprocessor) |
112 | { | 131 | { |
113 | free (con_info); | 132 | free (con_info); |
114 | return MHD_NO; | 133 | return MHD_NO; |
115 | } | 134 | } |
116 | 135 | ||
117 | con_info->connectiontype = POST; | 136 | con_info->connectiontype = POST; |
118 | } | 137 | } |
119 | else con_info->connectiontype = GET; | 138 | else |
139 | con_info->connectiontype = GET; | ||
140 | |||
141 | *con_cls = (void *) con_info; | ||
120 | 142 | ||
121 | *con_cls = (void*) con_info; | ||
122 | |||
123 | return MHD_YES; | 143 | return MHD_YES; |
124 | } | 144 | } |
125 | 145 | ||
126 | if (0 == strcmp (method, "GET")) | 146 | if (0 == strcmp (method, "GET")) |
127 | { | 147 | { |
128 | return send_page (connection, askpage); | 148 | return send_page (connection, askpage); |
129 | } | 149 | } |
130 | 150 | ||
131 | if (0 == strcmp (method, "POST")) | 151 | if (0 == strcmp (method, "POST")) |
132 | { | 152 | { |
133 | struct connection_info_struct *con_info = *con_cls; | 153 | struct connection_info_struct *con_info = *con_cls; |
134 | 154 | ||
135 | if (*upload_data_size != 0) | 155 | if (*upload_data_size != 0) |
136 | { | 156 | { |
137 | MHD_post_process(con_info->postprocessor, upload_data, *upload_data_size); | 157 | MHD_post_process (con_info->postprocessor, upload_data, |
158 | *upload_data_size); | ||
138 | *upload_data_size = 0; | 159 | *upload_data_size = 0; |
139 | 160 | ||
140 | return MHD_YES; | 161 | return MHD_YES; |
141 | } | 162 | } |
142 | else | 163 | else if (NULL != con_info->answerstring) |
143 | if (NULL != con_info->answerstring) return send_page (connection, con_info->answerstring); | 164 | return send_page (connection, con_info->answerstring); |
144 | } | 165 | } |
145 | 166 | ||
146 | return send_page(connection, errorpage); | 167 | return send_page (connection, errorpage); |
147 | } | 168 | } |
148 | 169 | ||
149 | int main () | 170 | int |
171 | main () | ||
150 | { | 172 | { |
151 | struct MHD_Daemon *daemon; | 173 | struct MHD_Daemon *daemon; |
152 | 174 | ||
153 | 175 | ||
154 | daemon = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY, PORT, NULL, NULL, | 176 | daemon = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY, PORT, NULL, NULL, |
155 | &answer_to_connection, NULL, MHD_OPTION_NOTIFY_COMPLETED, | 177 | &answer_to_connection, NULL, |
156 | request_completed, NULL, MHD_OPTION_END); | 178 | MHD_OPTION_NOTIFY_COMPLETED, request_completed, |
157 | if (NULL == daemon) return 1; | 179 | NULL, MHD_OPTION_END); |
180 | if (NULL == daemon) | ||
181 | return 1; | ||
158 | 182 | ||
159 | getchar (); | 183 | getchar (); |
160 | 184 | ||
161 | MHD_stop_daemon (daemon); | 185 | MHD_stop_daemon (daemon); |
162 | 186 | ||