aboutsummaryrefslogtreecommitdiff
path: root/doc/examples/responseheaders.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2008-08-24 18:03:05 +0000
committerChristian Grothoff <christian@grothoff.org>2008-08-24 18:03:05 +0000
commit9181dd0a072670d61a2e6839f23d7e33165ca089 (patch)
tree88806884c43d3579c3a357d8b26d1843e009ceab /doc/examples/responseheaders.c
parent484e65d76e26874485e906f5221156442bb52467 (diff)
downloadlibmicrohttpd-9181dd0a072670d61a2e6839f23d7e33165ca089.tar.gz
libmicrohttpd-9181dd0a072670d61a2e6839f23d7e33165ca089.zip
indenting
Diffstat (limited to 'doc/examples/responseheaders.c')
-rw-r--r--doc/examples/responseheaders.c84
1 files changed, 48 insertions, 36 deletions
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
12long get_file_size (const char *filename) 12long
13get_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
34int answer_to_connection (void *cls, struct MHD_Connection *connection, const char *url, 34int
35 const char *method, const char *version, const char *upload_data, 35answer_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
92int main () 103int
104main ()
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