📄 cgi-util.c
字号:
cgi_entries = NULL; cgi_num_entries = 0; cgi_errno = CGIERR_NONE; cgi_request_method = CGIREQ_NONE; cgi_query = NULL; cgi_content_type = CGITYPE_NONE;}/* Parse cookies. Return number found. Internal use only, should be called from cgi_init() */#define MAX_COOKIE_STUFF 1024int cgi_parse_cookies(){ char *cookies; char *cookie; char *name, *value; char sscanf_string[1024]; /* Gonna be a bad day if we overflow this */ int num_cookies; cgi_cookie_type * tmp_cookie; if(!(cookies = getenv("HTTP_COOKIE"))) { /* no cookies */ cgi_errno = CGIERR_NO_COOKIES; return 0; } memset((void *)sscanf_string,0,sizeof(sscanf_string)); sprintf(sscanf_string, "%%%i[^=]=%%%i[^;]", MAX_COOKIE_STUFF,MAX_COOKIE_STUFF); num_cookies = 0; cookie = cookies; do { /* If it's not our first cookie, the strstr from the while will leave us with a ';' at the start of cookie */ if(num_cookies != 0) cookie++; num_cookies++; name = malloc((size_t)MAX_COOKIE_STUFF); value = malloc((size_t)MAX_COOKIE_STUFF); if(sscanf(cookie,sscanf_string,name,value) != 2) { free(name); free(value); continue; } tmp_cookie = realloc((void *)cgi_cookies, sizeof(cgi_cookie_type) * num_cookies); if(tmp_cookie != NULL) { cgi_cookies = tmp_cookie; } else { /* Ran out of memory, but we can put in a best effort anyway */ cgi_num_cookies = num_cookies; return num_cookies; } cgi_cookies[num_cookies - 1].name = name; cgi_cookies[num_cookies - 1].val = value; } while((cookie = strstr(cookie, ";")) != NULL); cgi_num_cookies = num_cookies; return num_cookies;}/* Grab a cookie, if it exists. Return NULL if it doesn't: *//* (Based on code by Pete Cassidy (pcassidy@iol.ie) - May 10, 2000) *//* (Restructured by Chris Wareham (chris.wareham@catchword.com) - Nov 1, 2000) */const char *cgi_getcookie(const char *cookie_name){ int i; for (i = 0; i < cgi_num_cookies; i++) { if(strcmp(cgi_cookies[i].name,cookie_name) == 0) { cgi_errno = CGIERR_NONE; return cgi_cookies[i].val; } } cgi_errno = CGIERR_COOKIE_NOT_FOUND; return(NULL);}#ifdef SKIP_JUNK/* ------------------------------------------------------------------------- *//* THIS IS THE OLD cgi_getcookie(), AND IS CURRENTLY DISABLED */const char * cgi_getcookie(const char * cookie_name){ char * cookieval, * tmpcookie, * rawcookie, * left, * right; int done; /* Get raw cookie data: */ rawcookie = getenv("HTTP_COOKIE"); if (rawcookie == NULL) { /* No cookies at all? The cookie we want can't exist! */ cgi_errno = CGIERR_NO_COOKIES; return(NULL); } /* Strtok is destructive, so make a temporary copy of the raw cookie data: */ tmpcookie = malloc(sizeof(char) * (strlen(rawcookie) + 1)); if (tmpcookie == NULL) { cgi_errno = CGIERR_OUT_OF_MEMORY; return(NULL); } strcpy(tmpcookie, rawcookie); /* Tokenize out all cookies and check for the one we're looking for: */ left = strtok(tmpcookie, ";"); cookieval = NULL; done = 0; do { /* Grab the righthand size of the current cookie pair's "=" sign: */ right = strchr(left, '=') + (1 * sizeof(char)); /* Change the "=" into a NULL character, to get the lefthand side: */ *strchr(left, '=') = '\0'; /* See if this is our cookie: */ if (strcmp(left, cookie_name) == 0) { /* If so, set our return-string to the value (righthand side): */ cookieval = malloc(sizeof(char) * (strlen(right) + 1)); if (cookieval == NULL) { cgi_errno = CGIERR_OUT_OF_MEMORY; return(NULL); } strcpy(cookieval, right); done = 1; } /* Jump to next cookie: */ if (!done) { left = strtok(NULL, ";"); if (left == NULL) { /* No more to parse? */ done = 1; } else { /* Skip the extra space: */ left++; } } } while (!done); /* Free the temporary copy of the raw cookie data: */ free(tmpcookie); /* Return the cookie value (which may be NULL if we never found it): */ if (cookieval == NULL) cgi_errno = CGIERR_COOKIE_NOT_FOUND; else cgi_errno = CGIERR_NONE; return(cookieval);}/* ------------------------------------------------------------------------- */#endif/* Count number of entries named 'field_name': *//* Stephen Woodbridge <woodbri@swoodbridge.com> */int cgi_getnumentries(const char *field_name){ int cnt, i; cnt = 0; for (i = 0; i < cgi_num_entries; i++) { if (strcmp(cgi_entries[i].name, field_name) == 0) cnt++; } return cnt;}/* Grab a value and return it as a string: */const char * cgi_getentrystr(const char *field_name){ return cgi_getnentrystr(field_name, 0);}/* Grab a value and return it as a string: */const char * cgi_getnentrystr(const char *field_name, int n){ int x; int cnt; cnt = 0; if (n < 0 || n > cgi_getnumentries(field_name)) { cgi_errno = CGIERR_N_OUT_OF_BOUNDS; return(NULL); } if (cgi_request_method != CGIREQ_NONE) { /* Look for the name: */ for (x = 0; x < cgi_num_entries; x++) { if (strcmp(cgi_entries[x].name, field_name) == 0) { if (cnt == n) return (cgi_entries[x].val); cnt++; } } return(NULL); } else { /* printf("CGI-UTIL: \"%s\" ? ", field_name); fgets(buf, 512, stdin); buf[strlen(buf) - 1] = '\0'; */ return(NULL); }}/* Grab a content-type and return it as a string: */const char * cgi_getentrytype(const char *field_name){ return cgi_getnentrytype(field_name, 0);}/* Grab a content-type and return it as a string: */const char * cgi_getnentrytype(const char *field_name, int n){ int x; int cnt; cnt = 0; if (n < 0 || n > cgi_getnumentries(field_name)) { cgi_errno = CGIERR_N_OUT_OF_BOUNDS; return(NULL); } if (cgi_request_method != CGIREQ_NONE) { /* Look for the name: */ for (x = 0; x < cgi_num_entries; x++) { if (strcmp(cgi_entries[x].name, field_name) == 0) { if (cnt == n) return (cgi_entries[x].content_type); cnt++; } } return(NULL); } else return(NULL);}/* Grab a value and return it as an integer: */int cgi_getentryint(const char *field_name){ return cgi_getnentryint(field_name, 0);}/* Grab a value and return it as an integer: */int cgi_getnentryint(const char *field_name, int n){ int v; v = 0; if (cgi_getentrystr(field_name) != NULL) { if (sscanf(cgi_getnentrystr(field_name, 0), "%d", &v) != 1) cgi_errno = CGIERR_NOT_INTEGER; } else cgi_errno = CGIERR_NOT_INTEGER; return(v);}/* Grab a value and return it as a double: */double cgi_getentrydouble(const char *field_name){ return cgi_getnentrydouble(field_name, 0);}/* Grab a value and return it as a double: */double cgi_getnentrydouble(const char *field_name, int n){ double v; v = 0; if (cgi_getentrystr(field_name) != NULL) { if (sscanf(cgi_getnentrystr(field_name, 0), "%lf", &v) != 1) cgi_errno = CGIERR_NOT_DOUBLE; } else cgi_errno = CGIERR_NOT_DOUBLE; return(v);}/* Grab a value and return it as a boolean (depending on if the value was "yes", "on" or "true", or "no", "off" or "false"): */int cgi_getentrybool(const char *field_name, int def){ return cgi_getnentrybool(field_name, def, 0);}/* Grab a value and return it as a boolean (depending on if the value was "yes", "on" or "true", or "no", "off" or "false"): */int cgi_getnentrybool(const char *field_name, int def, int n){ const char * temp; int v, cnt; cnt = 0; if (n < 0 || n > cgi_getnumentries(field_name)) { cgi_errno = CGIERR_N_OUT_OF_BOUNDS; return(def); } /* Assume the default: */ v = def; /* Get the value (if any): */ temp = cgi_getnentrystr(field_name, n); if (temp != NULL) { if (temp[0] == 'y' || temp[0] == 'Y' || /* Yes */ temp[1] == 'n' || temp[1] == 'N' || /* On */ temp[0] == 't' || temp[0] == 'T') /* True */ { /* A "yes" or "on" is a 1: */ v = 1; } else if (temp[0] == 'n' || temp[0] == 'N' || /* Yes */ temp[1] == 'f' || temp[1] == 'F' || /* Off */ temp[0] == 'f' || temp[0] == 'F') /* False */ { /* A "no" or "off" is a 0: */ v = 0; } else if (temp[0] != 0) { /* We got something, but not "yes", "on", "no" or "off": */ cgi_errno = CGIERR_NOT_BOOL; } } else cgi_errno = CGIERR_NOT_BOOL; return(v);}/* Open a file and send it to "stdout" (the browser): *//* (Returns an error if we can't open the file) */#define CGI_BUFLEN 1024int cgi_dump_no_abort(const char * filename){ FILE * fi; char * c; char buf[CGI_BUFLEN + 1]; cgi_errno = CGIERR_NONE; /* Open the file: */ fi = fopen(filename, "r"); if (fi == NULL) cgi_errno = CGIERR_CANT_OPEN; else { /* Read data and push it to "stdout": */ do { c = fgets(buf, CGI_BUFLEN, fi); if (c != NULL) fputs(c, stdout); } while (!feof(fi)); fclose(fi); } return(cgi_errno);}/* Open a file and send it to "stdout" (the browser): *//* (Displays an error message and quits the CGI if we can't open the file) */void cgi_dump(const char * filename){ if (cgi_dump_no_abort(filename) != CGIERR_NONE) { printf("Can't open %s - %s\n", filename, strerror(errno)); exit(0); }}/* Display a simple error message and quit the CGI: */void cgi_error(const char * reason){ printf("<h1>Error</h1>\n"); printf("%s\n", reason); exit(0);}/* Returns whether or not an e-mail address appears to be in the correct syntax ("username@host.domain"): */int cgi_goodemailaddress(const char * addr){ int i; /* No "@".. what? */ if (strchr(addr, '@') == NULL) return 0; /* "@" or "." at the end or beginning? */ if (addr[strlen(addr) - 1] == '@' || addr[strlen(addr) - 1] == '.' || addr[0] == '@' || addr[0] == '.') return 0; /* No "." after the "@"? More than one "@"? */ if (strchr(strchr(addr, '@'), '.') == NULL || strchr(strchr(addr, '@') + 1, '@') != NULL) return 0; /* Any illegal characters within the string? */ for (i = 0; i < strlen(addr); i++) { if (isalnum((unsigned char)addr[i]) == 0 && addr[i] != '.' && addr[i] != '@' && addr[i] != '_' && addr[i] != '-') return(0); } /* Must be ok... */ return 1;}/* Returns the English string description for a particular cgi-util error value: */const char * cgi_strerror(int err){ if (err < 0 || err > CGIERR_NUM_ERRS) return(""); else return(cgi_error_strings[err]);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -