libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

processingpost.inc (8959B)


      1 The previous chapters already have demonstrated a variety of possibilities to send information
      2 to the HTTP server, but it is not recommended that the @emph{GET} method is used to alter the way
      3 the server operates. To induce changes on the server, the @emph{POST} method is preferred over
      4 and is much more powerful than @emph{GET} and will be introduced in this chapter.
      5 
      6 We are going to write an application that asks for the visitor's name and, after the user has posted it,
      7 composes an individual response text. Even though it was not mandatory to use the @emph{POST} method here,
      8 as there is no permanent change caused by the POST, it is an illustrative example on how to share data
      9 between different functions for the same connection. Furthermore, the reader should be able to extend
     10 it easily.
     11 
     12 @heading GET request
     13 When the first @emph{GET} request arrives, the server shall respond with a HTML page containing an
     14 edit field for the name.
     15 
     16 @verbatim
     17 const char* askpage = "<html><body>\
     18                        What's your name, Sir?<br>\
     19                        <form action=\"/namepost\" method=\"post\">\
     20                        <input name=\"name\" type=\"text\"\
     21                        <input type=\"submit\" value=\" Send \"></form>\
     22                        </body></html>";
     23 @end verbatim
     24 @noindent
     25 
     26 The @code{action} entry is the @emph{URI} to be called by the browser when posting, and the
     27 @code{name} will be used later to be sure it is the editbox's content that has been posted.
     28 
     29 We also prepare the answer page, where the name is to be filled in later, and an error page
     30 as the response for anything but proper @emph{GET} and @emph{POST} requests:
     31 
     32 @verbatim
     33 const char* greatingpage="<html><body><h1>Welcome, %s!</center></h1></body></html>";
     34 
     35 const char* errorpage="<html><body>This doesn't seem to be right.</body></html>";
     36 @end verbatim
     37 @noindent
     38 
     39 Whenever we need to send a page, we use an extra function
     40 @code{int send_page(struct MHD_Connection *connection, const char* page)}
     41 for this, which does not contain anything new and whose implementation is therefore
     42 not discussed further in the tutorial.
     43 
     44 
     45 @heading POST request
     46 Posted data can be of arbitrary and considerable size; for example, if a user uploads a big
     47 image to the server. Similar to the case of the header fields, there may also be different streams
     48 of posted data, such as one containing the text of an editbox and another the state of a button.
     49 Likewise, we will have to register an iterator function that is going to be called maybe several times
     50 not only if there are different POSTs but also if one POST has only been received partly yet and
     51 needs processing before another chunk can be received.
     52 
     53 Such an iterator function is called by a @emph{postprocessor}, which must be created upon arriving
     54 of the post request.  We want the iterator function to read the first post data which is tagged
     55 @code{name} and to create an individual greeting string based on the template and the name.
     56 But in order to pass this string to other functions and still be able to differentiate different
     57 connections, we must first define a structure to share the information, holding the most import entries.
     58 
     59 @verbatim
     60 struct connection_info_struct
     61 {
     62   int connectiontype;
     63   char *answerstring;
     64   struct MHD_PostProcessor *postprocessor;
     65 };
     66 @end verbatim
     67 @noindent
     68 
     69 With these information available to the iterator function, it is able to fulfill its task.
     70 Once it has composed the greeting string, it returns @code{MHD_NO} to inform the post processor
     71 that it does not need to be called again. Note that this function does not handle processing
     72 of data for the same @code{key}. If we were to expect that the name will be posted in several
     73 chunks, we had to expand the namestring dynamically as additional parts of it with the same @code{key}
     74 came in. But in this example, the name is assumed to fit entirely inside one single packet.
     75 
     76 @verbatim
     77 static enum MHD_Result
     78 iterate_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key,
     79               const char *filename, const char *content_type,
     80               const char *transfer_encoding, const char *data,
     81 	      uint64_t off, size_t size)
     82 {
     83   struct connection_info_struct *con_info = coninfo_cls;
     84 
     85   if (0 == strcmp (key, "name"))
     86     {
     87       if ((size > 0) && (size <= MAXNAMESIZE))
     88         {
     89           char *answerstring;
     90           answerstring = malloc (MAXANSWERSIZE);
     91           if (!answerstring) return MHD_NO;
     92 
     93           snprintf (answerstring, MAXANSWERSIZE, greatingpage, data);
     94           con_info->answerstring = answerstring;
     95         }
     96       else con_info->answerstring = NULL;
     97 
     98       return MHD_NO;
     99     }
    100 
    101   return MHD_YES;
    102 }
    103 @end verbatim
    104 @noindent
    105 
    106 Once a connection has been established, it can be terminated for many reasons. As these
    107 reasons include unexpected events, we have to register another function that cleans up any resources
    108 that might have been allocated for that connection by us, namely the post processor and the greetings
    109 string. This cleanup function must take into account that it will also be called for finished
    110 requests other than @emph{POST} requests.
    111 
    112 @verbatim
    113 void
    114 request_completed (void *cls, struct MHD_Connection *connection,
    115      		        void **req_cls,
    116                         enum MHD_RequestTerminationCode toe)
    117 {
    118   struct connection_info_struct *con_info = *req_cls;
    119 
    120   if (NULL == con_info)
    121     return;
    122   if (con_info->connectiontype == POST)
    123     {
    124       MHD_destroy_post_processor (con_info->postprocessor);
    125       if (con_info->answerstring) free (con_info->answerstring);
    126     }
    127 
    128   free (con_info);
    129   *req_cls = NULL;
    130 }
    131 @end verbatim
    132 @noindent
    133 
    134 @emph{GNU libmicrohttpd} is informed that it shall call the above function when the daemon is started
    135 in the main function.
    136 
    137 @verbatim
    138 ...
    139 daemon = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD, PORT, NULL, NULL,
    140                            &answer_to_connection, NULL,
    141 			   MHD_OPTION_NOTIFY_COMPLETED, &request_completed, NULL,
    142 			   MHD_OPTION_END);
    143 ...
    144 @end verbatim
    145 @noindent
    146 
    147 @heading Request handling
    148 With all other functions prepared, we can now discuss the actual request handling.
    149 
    150 On the first iteration for a new request, we start by allocating a new instance of a
    151 @code{struct connection_info_struct} structure, which will store all necessary information for later
    152 iterations and other functions.
    153 
    154 @verbatim
    155 static enum MHD_Result
    156 answer_to_connection (void *cls, struct MHD_Connection *connection,
    157 		      const char *url,
    158                       const char *method, const char *version,
    159 		      const char *upload_data,
    160                       size_t *upload_data_size, void **req_cls)
    161 {
    162   if(NULL == *req_cls)
    163     {
    164       struct connection_info_struct *con_info;
    165 
    166       con_info = malloc (sizeof (struct connection_info_struct));
    167       if (NULL == con_info) return MHD_NO;
    168       con_info->answerstring = NULL;
    169 @end verbatim
    170 @noindent
    171 
    172 If the new request is a @emph{POST}, the postprocessor must be created now. In addition, the type
    173 of the request is stored for convenience.
    174 @verbatim
    175       if (0 == strcmp (method, "POST"))
    176         {
    177           con_info->postprocessor
    178 	    = MHD_create_post_processor (connection, POSTBUFFERSIZE,
    179                                          iterate_post, (void*) con_info);
    180 
    181           if (NULL == con_info->postprocessor)
    182             {
    183               free (con_info);
    184               return MHD_NO;
    185             }
    186           con_info->connectiontype = POST;
    187         }
    188       else con_info->connectiontype = GET;
    189 @end verbatim
    190 @noindent
    191 
    192 The address of our structure will both serve as the indicator for successive iterations and to remember
    193 the particular details about the connection.
    194 @verbatim
    195       *req_cls = (void*) con_info;
    196       return MHD_YES;
    197     }
    198 @end verbatim
    199 @noindent
    200 
    201 The rest of the function will not be executed on the first iteration. A @emph{GET} request is easily
    202 satisfied by sending the question form.
    203 @verbatim
    204   if (0 == strcmp (method, "GET"))
    205     {
    206       return send_page (connection, askpage);
    207     }
    208 @end verbatim
    209 @noindent
    210 
    211 In case of @emph{POST}, we invoke the post processor for as long as data keeps incoming, setting
    212 @code{*upload_data_size} to zero in order to indicate that we have processed---or at least have
    213 considered---all of it.
    214 @verbatim
    215   if (0 == strcmp (method, "POST"))
    216     {
    217       struct connection_info_struct *con_info = *req_cls;
    218 
    219       if (*upload_data_size != 0)
    220         {
    221           MHD_post_process (con_info->postprocessor, upload_data,
    222 	                    *upload_data_size);
    223           *upload_data_size = 0;
    224 
    225           return MHD_YES;
    226         }
    227       else if (NULL != con_info->answerstring)
    228         return send_page (connection, con_info->answerstring);
    229     }
    230 @end verbatim
    231 @noindent
    232 
    233 Finally, if they are neither @emph{GET} nor @emph{POST} requests, the error page is returned.
    234 @verbatim
    235   return send_page(connection, errorpage);
    236 }
    237 @end verbatim
    238 @noindent
    239 
    240 These were the important parts of the program @code{simplepost.c}.