libmicrohttpd

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

postprocessor.h (5165B)


      1 /*
      2      This file is part of libmicrohttpd
      3      Copyright (C) 2007-2022 Daniel Pittman, Christian Grothoff, and Evgeny Grin
      4 
      5      This library is free software; you can redistribute it and/or
      6      modify it under the terms of the GNU Lesser General Public
      7      License as published by the Free Software Foundation; either
      8      version 2.1 of the License, or (at your option) any later version.
      9 
     10      This library is distributed in the hope that it will be useful,
     11      but WITHOUT ANY WARRANTY; without even the implied warranty of
     12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13      Lesser General Public License for more details.
     14 
     15      You should have received a copy of the GNU Lesser General Public
     16      License along with this library; if not, write to the Free Software
     17      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
     18 */
     19 
     20 /**
     21  * @file postprocessor.h
     22  * @brief  Declarations for parsing POST data
     23  * @author Christian Grothoff
     24  * @author Karlson2k (Evgeny Grin)
     25  */
     26 
     27 #ifndef MHD_POSTPROCESSOR_H
     28 #define MHD_POSTPROCESSOR_H 1
     29 #include "internal.h"
     30 
     31 /**
     32  * States in the PP parser's state machine.
     33  */
     34 enum PP_State
     35 {
     36   /* general states */
     37   PP_Error,
     38   PP_Done,
     39   PP_Init,
     40   PP_NextBoundary,
     41 
     42   /* url encoding-states */
     43   PP_ProcessKey,
     44   PP_ProcessValue,
     45   PP_Callback,
     46 
     47   /* post encoding-states  */
     48   PP_ProcessEntryHeaders,
     49   PP_PerformCheckMultipart,
     50   PP_ProcessValueToBoundary,
     51   PP_PerformCleanup,
     52 
     53   /* nested post-encoding states */
     54   PP_Nested_Init,
     55   PP_Nested_PerformMarking,
     56   PP_Nested_ProcessEntryHeaders,
     57   PP_Nested_ProcessValueToBoundary,
     58   PP_Nested_PerformCleanup
     59 
     60 };
     61 
     62 
     63 enum RN_State
     64 {
     65   /**
     66    * No RN-preprocessing in this state.
     67    */
     68   RN_Inactive = 0,
     69 
     70   /**
     71    * If the next character is CR, skip it.  Otherwise,
     72    * just go inactive.
     73    */
     74   RN_OptN = 1,
     75 
     76   /**
     77    * Expect CRLF (and only CRLF).  As always, we also
     78    * expect only LF or only CR.
     79    */
     80   RN_Full = 2,
     81 
     82   /**
     83    * Expect either CRLF or '--'CRLF.  If '--'CRLF, transition into dash-state
     84    * for the main state machine
     85    */
     86   RN_Dash = 3,
     87 
     88   /**
     89    * Got a single dash, expect second dash.
     90    */
     91   RN_Dash2 = 4
     92 };
     93 
     94 
     95 /**
     96  * Bits for the globally known fields that
     97  * should not be deleted when we exit the
     98  * nested state.
     99  */
    100 enum NE_State
    101 {
    102   NE_none = 0,
    103   NE_content_name = 1,
    104   NE_content_type = 2,
    105   NE_content_filename = 4,
    106   NE_content_transfer_encoding = 8
    107 };
    108 
    109 
    110 /**
    111  * Internal state of the post-processor.  Note that the fields
    112  * are sorted by type to enable optimal packing by the compiler.
    113  */
    114 struct MHD_PostProcessor
    115 {
    116 
    117   /**
    118    * The connection for which we are doing
    119    * POST processing.
    120    */
    121   struct MHD_Connection *connection;
    122 
    123   /**
    124    * Function to call with POST data.
    125    */
    126   MHD_PostDataIterator ikvi;
    127 
    128   /**
    129    * Extra argument to ikvi.
    130    */
    131   void *cls;
    132 
    133   /**
    134    * Encoding as given by the headers of the connection.
    135    */
    136   const char *encoding;
    137 
    138   /**
    139    * Primary boundary (points into encoding string)
    140    */
    141   const char *boundary;
    142 
    143   /**
    144    * Nested boundary (if we have multipart/mixed encoding).
    145    */
    146   char *nested_boundary;
    147 
    148   /**
    149    * Pointer to the name given in disposition.
    150    */
    151   char *content_name;
    152 
    153   /**
    154    * Pointer to the (current) content type.
    155    */
    156   char *content_type;
    157 
    158   /**
    159    * Pointer to the (current) filename.
    160    */
    161   char *content_filename;
    162 
    163   /**
    164    * Pointer to the (current) encoding.
    165    */
    166   char *content_transfer_encoding;
    167 
    168   /**
    169    * Value data left over from previous iteration.
    170    */
    171   char xbuf[2];
    172 
    173   /**
    174    * Size of our buffer for the key.
    175    */
    176   size_t buffer_size;
    177 
    178   /**
    179    * Current position in the key buffer.
    180    */
    181   size_t buffer_pos;
    182 
    183   /**
    184    * Current position in @e xbuf.
    185    */
    186   size_t xbuf_pos;
    187 
    188   /**
    189    * Current offset in the value being processed.
    190    */
    191   uint64_t value_offset;
    192 
    193   /**
    194    * strlen(boundary) -- if boundary != NULL.
    195    */
    196   size_t blen;
    197 
    198   /**
    199    * strlen(nested_boundary) -- if nested_boundary != NULL.
    200    */
    201   size_t nlen;
    202 
    203   /**
    204    * Do we have to call the 'ikvi' callback when processing the
    205    * multipart post body even if the size of the payload is zero?
    206    * Set to #MHD_YES whenever we parse a new multiparty entry header,
    207    * and to #MHD_NO the first time we call the 'ikvi' callback.
    208    * Used to ensure that we do always call 'ikvi' even if the
    209    * payload is empty (but not more than once).
    210    */
    211   bool must_ikvi;
    212 
    213   /**
    214    * Set if we still need to run the unescape logic
    215    * on the key allocated at the end of this struct.
    216    */
    217   bool must_unescape_key;
    218 
    219   /**
    220    * State of the parser.
    221    */
    222   enum PP_State state;
    223 
    224   /**
    225    * Side-state-machine: skip CRLF (or just LF).
    226    * Set to 0 if we are not in skip mode.  Set to 2
    227    * if a CRLF is expected, set to 1 if a CR should
    228    * be skipped if it is the next character.
    229    */
    230   enum RN_State skip_rn;
    231 
    232   /**
    233    * If we are in skip_rn with "dash" mode and
    234    * do find 2 dashes, what state do we go into?
    235    */
    236   enum PP_State dash_state;
    237 
    238   /**
    239    * Which headers are global? (used to tell which
    240    * headers were only valid for the nested multipart).
    241    */
    242   enum NE_State have;
    243 
    244 };
    245 
    246 #endif /* ! MHD_POSTPROCESSOR_H */