示例#1
0
int main(void) {
	// Parse (GET/COOKIE/POST) queries.
	Q_ENTRY *req = qCgiRequestParse(NULL, 0);

	const char *mode = req->getStr(req, "mode", false);
	const char *name = req->getStr(req, "cname", false);
	const char *value = req->getStr(req, "cvalue", false);

	if (mode == NULL) { // View Cookie
		qCgiResponseSetContentType(req, "text/plain");
		printf("Total %d entries\n", req->getNum(req));
		req->print(req, stdout, true);
	} else if (!strcmp(mode, "set")) { // Set Cookie
		if (name == NULL || value == NULL) qCgiResponseError(req, "Query not found");
		if (strlen(name) == 0) qCgiResponseError(req, "Empty cookie name can not be stored.");

		qCgiResponseSetCookie(req, name, value, 0, NULL, NULL, false);
		qCgiResponseSetContentType(req, "text/html");
		printf("Cookie('%s'='%s') entry is stored.<br>Click <a href='cookie.cgi'>here</a> to view your cookies\n", name, value);
	} else if (!strcmp(mode, "remove")) { // Remove Cookie
		if (name == NULL) qCgiResponseError(req, "Query not found");
		if (!strcmp(name, "")) qCgiResponseError(req, "Empty cookie name can not be removed.");

		qCgiResponseRemoveCookie(req, name, NULL, NULL, false);
		qCgiResponseSetContentType(req, "text/html");
		printf("Cookie('%s') entry is removed.<br>Click <a href='cookie.cgi'>here</a> to view your cookies\n", name);
	} else {
		qCgiResponseError(req, "Unknown mode.");
	}

	req->free(req);
	return 0;
}
示例#2
0
int main(void) {
	// Parse queries.
	Q_ENTRY *req = qCgiRequestParse(NULL, 0);

	// get queries
	const char *text = req->getStr(req, "text", false);
	const char *filedata   = req->getStr(req, "binary", false);
	int filelength = req->getInt(req, "binary.length");
	const char *filename   = req->getStr(req, "binary.filename", false);
	const char *contenttype = req->getStr(req, "binary.contenttype", false);

	// check queries
	if (text == NULL) qCgiResponseError(req, "Invalid usages.");
	if (filename == NULL || filelength == 0) qCgiResponseError(req, "Select file, please.");

	char  filepath[1024];
	sprintf(filepath, "%s/%s", BASEPATH, filename);

	if (savefile(filepath, filedata, filelength) < 0) {
		qCgiResponseError(req, "File(%s) open fail. Please make sure CGI or directory has right permission.", filepath);
	}

	// result out
	qCgiResponseSetContentType(req, "text/html");
	printf("You entered: <b>%s</b>\n", text);
	printf("<br><a href=\"%s\">%s</a> (%d bytes, %s) saved.", filepath, filename, filelength, contenttype);

	// dump
	printf("\n<p><hr>--[ DUMP INTERNAL DATA STRUCTURE ]--\n<pre>");
	req->print(req, stdout, false);
	printf("\n</pre>\n");

	// de-allocate
	req->free(req);
	return 0;
}