int main(void) { char *mode, *name, *value; mode = qValue("mode"); name = qValue("cname"); value = qValue("cvalue"); if(mode == NULL) { /* View Cookie */ int amount; qContentType("text/html"); amount = qPrint(); printf("<p>Total %d entries\n", amount); } else if(!strcmp(mode, "set")) { /* Set Cookie */ if(name == NULL || value == NULL) qError("Query not found"); if(!strcmp(name, "")) qError("Empty cookie name can not be stored."); qCookieSet(name, value, 0, NULL, NULL, NULL); qContentType("text/html"); printf("Cookie('%s'='%s') entry is stored.<br>Click <a href='%s'>here</a> to view your cookies\n", name, value, qCGIname()); } else if(!strcmp(mode, "remove")) { /* Remove Cookie */ if(name == NULL) qError("Query not found"); if(!strcmp(name, "")) qError("Empty cookie name can not be removed."); qCookieRemove(name, NULL, NULL, NULL); qContentType("text/html"); printf("Cookie('%s') entry is removed.<br>Click <a href='%s'>here</a> to view your cookies\n", name, qCGIname()); } else qError("Unknown mode."); qFree(); return 0; }
int main(void) { char *sessionid; char *mode, *name, *value; time_t expire; /* fetch queries */ expire = (time_t)qiValue("expire"); mode = qValueNotEmpty("Mode not found", "mode"); name = qValue("name"); value = qValue("value"); /* Set valid interval of this session */ /* start session. this function should be called before qContentType(). */ qSession(NULL); sessionid = qSessionGetID(); /* Mose case, you don't need to set timeout but we use it here to give you a show case */ if(expire > 0) qSessionSetTimeout(expire); switch(mode[0]) { case 's': { qSessionAdd(name, value); break; } case 'i': { qSessionAddInteger(name, atoi(value)); break; } case 'r': { qSessionRemove(name); break; } case 'd': { qSessionDestroy(); qContentType("text/html"); printf("Session destroied."); return 0; break; } } /* screen out */ qContentType("text/html"); qSessionPrint(); /* save session & free allocated memories */ qSessionFree(); return 0; }
int main(void) { char *string, *query; char *qlist[256], **qp; int queries, tmatches, matches; qContentType("text/html"); qDecoder(); string = qValueNotEmpty("Type any string.", "string"); query = qValueNotEmpty("Type any query.", "query"); queries = qArgMake(query, qlist); matches = qArgMatch(string, qlist); printf("String: <b>%s</b><br>\n", string); printf("Query Input: <b>%s</b><br>\n", query); printf("<hr>\n"); printf("qArgMake(): <b>"); for(qp = qlist; *qp; qp++) qPrintf(1, "\"%s\" ", *qp); printf("</b> (%d queries)<br>\n", queries); printf("qArgEmprint(): "); tmatches = qArgEmprint(1, string, qlist); printf(" (%d query matched)<br>\n", tmatches); printf("qArgMatch(): %d query matched<br>\n", matches); qArgFree(qlist); qFree(); return 0; }
int main(void) { char *list; qDecoder(); qContentType("text/html"); if((list = qValueFirst("checklist")) == NULL) qError("Check what you want to order please."); printf("You ordered "); for(; list; list = qValueNext()) { printf("<b>%s</b> \n", list); } qFree(); return 0; }
int main(void) { char *hostname; int retflag; qContentType("text/plain"); hostname = qValueDefault("", "hostname"); if(strlen(hostname) == 0) qError("Invalid usages."); retflag = dumpHttp(hostname); if(retflag < 0) { if(retflag == -1) qError("Invalid hostname."); else if(retflag == -2) qError("Can't create socket."); else if(retflag == -3) qError("Connection failed."); else qError("Unknown error."); } return 0; }
/* For decode multipart/form-data, used by qDecoder() */ static int _parse_multipart_data(void) { Q_Entry *entries, *back; char *query; int amount; int loop; int cnt; char *name, *value, *filename, *contenttype; int valuelen; char boundary[256], boundaryEOF[256]; char rnboundaryrn[256], rnboundaryEOF[256]; int boundarylen, boundaryEOFlen, maxboundarylen; char buf[1024]; int c, c_count; int finish; #ifdef _WIN32 setmode(fileno(stdin), _O_BINARY); setmode(fileno(stdout), _O_BINARY); #endif entries = back = NULL; /* For parse GET method & Cookie */ for (amount = 0, loop = 1; loop <= 2; loop++) { char sepchar; if (loop == 1) { query = _get_query("COOKIE"); sepchar = ';'; } else if (loop == 2) { query = _get_query("GET"); sepchar = '&'; } else break; for (cnt = 0; query && *query; amount++, cnt++) { back = entries; entries = (Q_Entry *)malloc(sizeof(Q_Entry)); if (back != NULL) back->next = entries; if (_first_entry == NULL) _first_entry = entries; entries->value = _makeword(query, sepchar); entries->name = qRemoveSpace(_makeword(entries->value, '=')); entries->next = NULL; qURLdecode(entries->name); qURLdecode(entries->value); } if (query) free(query); if (loop == 1) _cookie_cnt = cnt; else if (loop == 2) _get_cnt = cnt; } /* For parse multipart/form-data method */ /* Force to check the boundary string length to defense overflow attack */ maxboundarylen = strlen("--"); maxboundarylen += strlen(strstr(getenv("CONTENT_TYPE"), "boundary=") + strlen("boundary=")); maxboundarylen += strlen("--"); maxboundarylen += strlen("\r\n"); if (maxboundarylen >= sizeof(boundary)) qError("_parse_multipart_data(): The boundary string is too long. Stopping process."); /* find boundary string */ sprintf(boundary, "--%s", strstr(getenv("CONTENT_TYPE"), "boundary=") + strlen("boundary=")); /* This is not necessary but, I can not trust MS Explore */ qRemoveSpace(boundary); sprintf(boundaryEOF, "%s--", boundary); sprintf(rnboundaryrn, "\r\n%s\r\n", boundary); sprintf(rnboundaryEOF, "\r\n%s", boundaryEOF); boundarylen = strlen(boundary); boundaryEOFlen = strlen(boundaryEOF); /* If you want to observe the string from stdin, enable this section. */ /* This section is made for debugging. */ if (0) { int i, j; qContentType("text/html"); printf("Content Length = %s<br>\n", getenv("CONTENT_LENGTH")); printf("Boundary len %d : %s<br>\n", (int)strlen(boundary), boundary); for (i = 0; boundary[i] != '\0'; i++) printf("%02X ", boundary[i]); printf("<p>\n"); for (j = 1; CONCEPT_AWARE_fgets(buf, sizeof(buf), stdin) != NULL; j++) { printf("Line %d, len %d : %s<br>\n", j, (int)strlen(buf), buf); for (i = 0; buf[i] != '\0'; i++) printf("%02X ", buf[i]); printf("<p>\n"); } exit(0); } /* check boundary */ if (CONCEPT_AWARE_fgets(buf, sizeof(buf), stdin) == NULL) qError("_parse_multipart_data(): Your browser sent a non-HTTP compliant message."); /* for explore 4.0 of NT, it sent \r\n before starting, f*****g Micro$oft */ if (!strcmp(buf, "\r\n")) CONCEPT_AWARE_fgets(buf, sizeof(buf), stdin); if (strncmp(buf, boundary, boundarylen) != 0) qError("_parse_multipart_data(): String format invalid."); for (finish = 0, cnt = 0; finish != 1; amount++, cnt++) { /* parse header */ name = ""; filename = ""; contenttype = ""; while (CONCEPT_AWARE_fgets(buf, sizeof(buf), stdin)) { if (!strcmp(buf, "\r\n")) break; else if (!qStrincmp(buf, "Content-Disposition: ", strlen("Content-Disposition: "))) { /* get name field */ name = strdup(buf + strlen("Content-Disposition: form-data; name=\"")); for (c_count = 0; (name[c_count] != '\"') && (name[c_count] != '\0'); c_count++); name[c_count] = '\0'; /* get filename field */ if (strstr(buf, "; filename=\"") != NULL) { int erase; filename = strdup(strstr(buf, "; filename=\"") + strlen("; filename=\"")); for (c_count = 0; (filename[c_count] != '\"') && (filename[c_count] != '\0'); c_count++); filename[c_count] = '\0'; /* erase '\' */ for (erase = 0, c_count = strlen(filename) - 1; c_count >= 0; c_count--) { if (erase == 1) filename[c_count] = ' '; else { if (filename[c_count] == '\\') { erase = 1; filename[c_count] = ' '; } } } qRemoveSpace(filename); } } else if (!qStrincmp(buf, "Content-Type: ", strlen("Content-Type: "))) { contenttype = strdup(buf + strlen("Content-Type: ")); qRemoveSpace(contenttype); } } /* get value field */ for (value = NULL, valuelen = (1024 * 16), c_count = 0; (c = CONCEPT_AWARE_fgetc(stdin)) != EOF; ) { if (c_count == 0) { value = (char *)malloc(sizeof(char) * valuelen); if (value == NULL) qError("_parse_multipart_data(): Memory allocation fail."); } else if (c_count == valuelen - 1) { char *valuetmp; valuelen *= 2; /* Here, we do not use realloc(). Because sometimes it is unstable. */ valuetmp = (char *)malloc(sizeof(char) * valuelen); if (valuetmp == NULL) qError("_parse_multipart_data(): Memory allocation fail."); memcpy(valuetmp, value, c_count); free(value); value = valuetmp; } value[c_count++] = (char)c; /* check end */ if ((c == '\n') || (c == '-')) { value[c_count] = '\0'; if ((c_count - (2 + boundarylen + 2)) >= 0) { if (!strcmp(value + (c_count - (2 + boundarylen + 2)), rnboundaryrn)) { value[c_count - (2 + boundarylen + 2)] = '\0'; valuelen = c_count - (2 + boundarylen + 2); break; } } if ((c_count - (2 + boundaryEOFlen)) >= 0) { if (!strcmp(value + (c_count - (2 + boundaryEOFlen)), rnboundaryEOF)) { value[c_count - (2 + boundaryEOFlen)] = '\0'; valuelen = c_count - (2 + boundaryEOFlen); finish = 1; break; } } /* For Micro$oft Explore on MAC, they do not follow rules */ if ((c_count - (boundarylen + 2)) == 0) { char boundaryrn[256]; sprintf(boundaryrn, "%s\r\n", boundary); if (!strcmp(value, boundaryrn)) { value[0] = '\0'; valuelen = 0; break; } } if ((c_count - boundaryEOFlen) == 0) { if (!strcmp(value, boundaryEOF)) { value[0] = '\0'; valuelen = 0; finish = 1; break; } } } } if (c == EOF) qError("_parse_multipart_data(): Broken stream at end of '%s'.", name); /* store in linked list */ /* store data */ back = entries; entries = (Q_Entry *)malloc(sizeof(Q_Entry)); if (back != NULL) back->next = entries; if (_first_entry == NULL) _first_entry = entries; entries->name = name; entries->value = value; entries->next = NULL; if (strcmp(filename, "") != 0) { /* store data length, 'NAME.length'*/ back = entries; entries = (Q_Entry *)malloc(sizeof(Q_Entry)); back->next = entries; entries->name = (char *)malloc(sizeof(char) * (strlen(name) + strlen(".length") + 1)); entries->value = (char *)malloc(sizeof(char) * 20 + 1); sprintf(entries->name, "%s.length", name); sprintf(entries->value, "%d", valuelen); /* store filename, 'NAME.filename'*/ back = entries; entries = (Q_Entry *)malloc(sizeof(Q_Entry)); back->next = entries; entries->name = (char *)malloc(sizeof(char) * (strlen(name) + strlen(".filename") + 1)); entries->value = filename; sprintf(entries->name, "%s.filename", name); entries->next = NULL; /* store contenttype, 'NAME.contenttype'*/ back = entries; entries = (Q_Entry *)malloc(sizeof(Q_Entry)); back->next = entries; entries->name = (char *)malloc(sizeof(char) * (strlen(name) + strlen(".contenttype") + 1)); entries->value = contenttype; sprintf(entries->name, "%s.contenttype", name); entries->next = NULL; cnt += 3; } } _post_cnt = cnt; return amount; }