The qDecoder Project

qCgiRequest.c File Reference

Web Query & Cookie Handling API. More…


Defines

#define _Q_MULTIPART_CHUNK_SIZE   (64 * 1024)

Functions

Q_ENTRYqCgiRequestParseOption (bool filemode, const char *basepath, int clearold)
 Set request parsing option for file uploading in case of multipart/form-data encoding.
Q_ENTRYqCgiRequestParse (Q_ENTRY *request)
 Parse web request(COOKIE/GET/POST) query into name and value fairs then store into the Q_ENTRY structure.
Q_ENTRYqCgiRequestParseQueries (Q_ENTRY *request, const char *method)
 Parse GET/POST queries into name and value fairs then store into the Q_ENTRY structure.
Q_ENTRYqCgiRequestParseCookies (Q_ENTRY *request)
 Parse cookies into name and value fairs then store into the Q_ENTRY structure.
char * qCgiRequestGetQueryString (const char *query_type)
 Get unparsed query string.


Detailed Description

Web Query & Cookie Handling API.

qDecoder supports parsing

Anyway you don’t care about this. You don’t need to know which method (COOKIE/GET/POST) is used for sending data.

   [HTML sample]
   <form action="your_program.cgi">
     <input type="text" name="color">
     <input type="submit">
   </form>
   [Your Source]
   Q_ENTRY *req = qCgiRequestParse(NULL);
   const char *color = qEntryGetStr(req, "color");
   printf("color = %sn", color);
   qEntryFree(req);

The storing sequence is (1)COOKIE (2)GET (3)POST. Thus if same query names (which are sent by different method) exist, COOKIE query will be returned.

If you would like to get POST queries only. See below sample codes.

   Q_ENTRY *req = qCgiRequestParseQueries(NULL, "POST");
   const char *color = qEntryGetStr(req, "color");
   printf("color = %sn", color);

If you would like to get POST and COOKIE queries only. See below sample codes.

   Q_ENTRY *req = NULL;
   req = qCgiRequestParseCookies(req);
   req = qCgiRequestParseQueries(req, "POST");
   const char *color = qEntryGetStr(req, "color");
   printf("color = %sn", color);
   qEntryFree(req);

In case of multipart/form-data encoding(used for file uploading), qDecoder supports 2 modes for handling file uploading. I just made a name for that.

You can switch to file mode by calling qCgiRequestParseOption().
   Q_ENTRY *req = qCgiRequestParseOption(true, "/tmp", 86400);
   req = qCgiRequestParse(req);
   (...your codes here...)
   qEntryFree(req);

Basically, when file is uploaded qDecoder store it’s meta information like below.

   [default mode example]
   binary = (...binary data...)
   binary.filename = hello.xls
   binary.length = 3292
   binary.contenttype = application/vnd.ms-excel
   [file mode example]
   binary = tmp/Q_7b91698bc6d6ac7eaac93e71ce729b7c/1-hello.xls
   binary.filename = hello.xls
   binary.length = 3292
   binary.contenttype = application/vnd.ms-excel
   binary.savepath = tmp/Q_7b91698bc6d6ac7eaac93e71ce729b7c/1-hello.xls

If you want to activate progress mode. Please follow below steps


Function Documentation

Q_ENTRY* qCgiRequestParseOption ( bool  filemode,
const char *  basepath,
int  clearold 
)

Set request parsing option for file uploading in case of multipart/form-data encoding.

Parameters:
request if request is a NULL pointer, the function allocates, initializes, and returns a new object. otherwise use it.
filemode false(default) for parsing in memory, true for storing attached files to file
basepath the base path where the uploaded files are located. can be NULL if filemode is false.
clearold automatically remove temporary uploading file older than this secconds. to disable, set to 0.
Returns:
an initialized Q_ENTRY* handle, NULL if basepath can not be found.
Note:
This method should be called before qCgiRequestParse().
   Q_ENTRY *req = qCgiRequestParseOption(true, "/tmp", 86400);
   req = qCgiRequestParse(req);

Q_ENTRY* qCgiRequestParse ( Q_ENTRY request  ) 

Parse web request(COOKIE/GET/POST) query into name and value fairs then store into the Q_ENTRY structure.

Parameters:
request if request is a NULL pointer, the function allocates, initializes, and returns a new object. but if you called qCgiParseRequestOption() to set options, pass it’s pointer.
Returns:
Q_ENTRY* handle if successful, NULL if there was insufficient memory to allocate a new object.
   Q_ENTRY *req = qCgiRequestParse(NULL);
   printf("%sn", qEntryGetStr(req, "name"));

Note:
The parsing sequence is (1)COOKIE, (2)GET (3)POST. Thus if same query names (which are sent by different method) exist, qGetValue() will return the value sent by GET method.

Q_ENTRY* qCgiRequestParseQueries ( Q_ENTRY request,
const char *  method 
)

Parse GET/POST queries into name and value fairs then store into the Q_ENTRY structure.

Parameters:
request if request is a NULL pointer, the function allocates, initializes, and returns a new object. otherwise reuse(append at) Q_ENTRY* handle.
method “GET” or “POST”. NULL can be used for parse both GET/POST queries.
Returns:
Q_ENTRY* handle if successful, NULL if there was insufficient memory to allocate a new object.
   Q_ENTRY *getpost = qCgiRequestParseQueries(NULL, NULL);
   printf("%sn", qEntryGetStr(getpost, "name"));
   // if you would like to parse only POST queries.
   Q_ENTRY *post = qCgiRequestParseQueries(NULL, "POST");
   printf("%sn", qEntryGetStr(post, "name"));

Q_ENTRY* qCgiRequestParseCookies ( Q_ENTRY request  ) 

Parse cookies into name and value fairs then store into the Q_ENTRY structure.

Parameters:
request if request is a NULL pointer, the function allocates, initializes, and returns a new object. otherwise cookie value will be appended at Q_ENTRY* handle.
Returns:
Q_ENTRY* handle if successful, NULL if there was insufficient memory to allocate a new object.
   Q_ENTRY *cookies = qCgiRequestParseCookies(NULL);
   printf("%sn", qEntryGetStr(cookies, "name"));

char* qCgiRequestGetQueryString ( const char *  query_type  ) 

Get unparsed query string.

Parameters:
query_type GET, POST, COOKIE (case-sensitive)
Returns:
query string which is memory allocated if successful, otherwise returns NULL;
   char *query = qCgiRequestGetQueryString("GET");
   if(query != NULL) {
     printf("%sn", query);
     free(query);
   }


[Home] [About] [Examples] [Changes] [Download] [SVN Repository] [Install] [Reference]