qcgireq.c File Reference

# qDecoder API Reference

- Main Page
- Data Structures
- Files

- File List
- Globals

Functions
qcgireq.c File Reference

## Detailed Description

CGI Request Parsing API.

qDecoder supports parsing of

- COOKIE
- GET method
- POST method (application/x-www-form-urlencoded: default FORM encoding)
- POST method (multipart/form-data: especially used for file uploading)

And parsed elements are stored in qentry_t linked-list structure.

[HTML sample]
<form action="your_program.cgi">
<input type="text" name="color">
<input type="submit">
</form>
[Your Source]
qentry_t *req = qcgireq_parse(NULL, 0); // 0 is equivalent to Q_CGI_ALL.
qcgires_setcontenttype(req, "text/plain");
char *color = req->getstr(req, "color", false);
if (color != NULL) {
printf("Color = %s\n", color);
}
req->free(req);

The order of parsing sequence is (1)COOKIE (2)POST (3)GET. Thus if there is a same query name existing in different methods, COOKIE values will be stored first than POST, GET values will be added at the very last into a qentry linked-list.

Below is an example to parse only two given methods. Please note that when multiple methods are specified, it'll be parsed in the order of COOKIE, POST and GET.

qentry_t *req = qcgireq_parse(req, Q_CGI_COOKIE | Q_CGI_POST);

To change the order of parsing sequence, you can call qcgireq_parse() multiple times in the order that you want as below.

qentry_t *req;
req = qcgireq_parse(req, Q_CGI_POST);
req = qcgireq_parse(req, Q_CGI_GET);
req = qcgireq_parse(req, Q_CGI_COOKIE);

In terms of multipart/form-data encoding(used for file uploading), qDecoder can handle that in two different ways internally.

- default mode : Uploading file will be processed only in memory. (see examples/upload.c)
- file mode : Uploading file will be stored directly into disk. (see examples/uploadfile.c)

You can switch to file mode by calling qcgireq_setoption().

qentry_t *req = qcgireq_setoption(NULL, true, "/tmp", 86400);
req = qcgireq_parse(req, 0);
(...your codes here...)
req->free(req);

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

- (VARIABLE_NAME) - In the default mode, this is binary data. In the file mode this value is same as "(VARIABLE_NAME).savepath".
- (VARIABLE_NAME).filename - File name itself, path info will be removed.
- (VARIABLE_NAME).length - File size, the number of bytes.
- (VARIABLE_NAME).contenttype - Mime type like 'text/plain'.
- (VARIABLE_NAME).savepath - Only appended only in file mode. The file path where the uploaded file is saved.

[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_wcktIq
binary.length = 60014
binary.filename = hello.xls
binary.contenttype = application/vnd.ms-excel
binary.savepath = tmp/q_wcktIq

## Functions
qentry_t * | qcgireq_setoption (qentry_t *request, bool filemode, const char *basepath, int clearold)
| Set request parsing option for file uploading in case of multipart/form-data encoding.
qentry_t * | qcgireq_parse (qentry_t *request, Q_CGI_T method)
| Parse one or more request(COOKIE/POST/GET) queries.
char * | qcgireq_getquery (Q_CGI_T method)
| Get raw query string.

## Function Documentation

qentry_t* qcgireq_setoption | ( | qentry_t * | request,
|  | bool | filemode,
|  | const char * | basepath,
|  | int | clearold
| ) |  |

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

Parameters

  request | qentry_t container pointer that options will be set. NULL can be used to create a new container.
  filemode | false for parsing in memory, true for storing attached files into file-system directly.
  basepath | the base path where the uploaded files are located. Set to NULL if filemode is false.
  clearold | saved files older than this seconds will be removed automatically. Set to 0 to disable.

Returns
  qentry_t container pointer, otherwise returns NULL.

Note
  This method should be called before calling qcgireq_parse().

qentry_t *req = qcgireq_setoption(NULL, true, "/tmp", 86400);
req = qcgireq_parse(req, 0);
req->free(req);

qentry_t* qcgireq_parse | ( | qentry_t * | request,
|  | Q_CGI_T | method
| ) |  |

Parse one or more request(COOKIE/POST/GET) queries.

Parameters

  request | qentry_t container pointer that parsed key/value pairs will be stored. NULL can be used to create a new container.
  method | Target mask consists of one or more of Q_CGI_COOKIE, Q_CGI_POST and Q_CGI_GET. Q_CGI_ALL or 0 can be used for parsing all of those types.

Returns
  qentry_t* handle if successful, NULL if there was insufficient memory to allocate a new object.

qentry_t *req = qcgireq_parse(NULL, 0);
qentry_t *req = qcgireq_parse(req, Q_CGI_COOKIE | Q_CGI_POST);

Note
  When multiple methods are specified, it'll be parsed in the order of (1)COOKIE, (2)POST (3)GET unless you call it separately multiple times.

char* qcgireq_getquery | ( | Q_CGI_T | method | ) |

Get raw query string.

Parameters

  method | One of Q_CGI_COOKIE, Q_CGI_POST or Q_CGI_GET.

Returns
  malloced query string otherwise returns NULL;

char *query = qcgireq_getquery(Q_CGI_GET);
if(query != NULL) {
printf("%s\n", query);
free(query);
}