/* we're running under a web server as cgi */ int setup_cgi(struct cgi_state *cgi) { char *p; if ((p = getenv("CONTENT_LENGTH"))) { cgi->content_length = atoi(p); } if ((p = getenv("REQUEST_METHOD"))) { cgi->got_request = 1; if (strcasecmp(p, "POST") == 0) { cgi->request_post = 1; } } if ((p = getenv("QUERY_STRING"))) { cgi->query_string = __ax_strdup(p); } if ((p = getenv("SCRIPT_NAME"))) { cgi->url = __ax_strdup(p); cgi->pathinfo = cgi->url; } if ((p = getenv("CONTENT_TYPE"))) { cgi->content_type = __ax_strdup(p); } return 0; }
/* we are a mini-web server. We need to read the request from stdin */ int setup_standalone(struct cgi_state *cgi) { char line[1024]; char *url=NULL; char *p; while (fgets(line, sizeof(line)-1, stdin)) { trim_tail(line, CRLF); if (line[0] == 0) break; if (strncasecmp(line,"GET ", 4)==0) { cgi->got_request = 1; url = __ax_strdup(&line[4]); } else if (strncasecmp(line,"POST ", 5)==0) { cgi->got_request = 1; cgi->request_post = 1; url = __ax_strdup(&line[5]); } else if (strncasecmp(line,"PUT ", 4)==0) { cgi->got_request = 1; cgi->http_error(cgi, "400 Bad Request", "", "This server does not accept PUT requests"); return -1; } else if (strncasecmp(line,"Content-Length: ", 16)==0) { add_header(cgi, line); cgi->content_length = atoi(&line[16]); } else if (strncasecmp(line,"Content-Type: ", 14)==0) { add_header(cgi, line); cgi->content_type = __ax_strdup(&line[14]); } else { add_header(cgi, line); } /* ignore all other requests! */ } if (!url) { cgi->http_error(cgi, "400 Bad Request", "", "You must specify a GET or POST request"); exit(1); } /* trim the URL */ if ((p = strchr(url,' ')) || (p=strchr(url,'\t'))) { *p = 0; } /* anything following a ? in the URL is part of the query string */ if ((p=strchr(url,'?'))) { cgi->query_string = p+1; *p = 0; } cgi->url = url; cgi->pathinfo = url; return 0; }
/* add a name/value pair to the list of cgi variables */ void put(struct cgi_state *cgi, const char *name, const char *value) { struct cgi_var *var; int len; char *cgi_name, *p; if (!name || !value) return; var = __ax_malloc(sizeof(*var)); memset(var, 0, sizeof(*var)); var->next = cgi->variables; /* trim leading spaces */ while (*name && (*name == '+' || *name == ' ')) name++; var->name = __ax_strdup(name); var->value = __ax_strdup(value); unescape(var->name); unescape(var->value); /* trim trailing spaces */ len = strlen(var->value); while (len && isspace(var->value[len-1])) { var->value[len-1] = 0; len--; } for (p=var->name; *p; p++) { if (!isalnum(*p) && !strchr("_-", *p)) { *p = '_'; } } cgi->variables = var; __ax_asprintf(&cgi_name, "CGI_%s", var->name); __ax_free(cgi_name); }
int op_get_file_list(INPUT *input) { INPUT *tmp; WFM2_FILE_LIST_PARAM sFileListParam; char *full_path = NULL; int nRet=0; memset(&sFileListParam, 0, sizeof(WFM2_FILE_LIST_PARAM)); if((tmp = CGI_Find_Parameter(input, "path"))) { full_path = (char *)__ax_malloc(strlen(tmp->val) + MAX_FILE_LENGTH + PATH_PREFIX_LENGTH + 256); int exist = check_exist(tmp->val, full_path); if (exist < 0) { Status_To_JSON(WFM2_FILE_NO_EXIST); if(full_path) __ax_free(full_path); return -1; } sFileListParam.node = __ax_strdup(tmp->val); } if((tmp = CGI_Find_Parameter(input, "list_mode"))) { strncpy(sFileListParam.listMode, tmp->val, WFM2_LIST_MODE_LEN); } if((tmp = CGI_Find_Parameter(input, "start"))) { sFileListParam.start = atoi(tmp->val); } else { sFileListParam.start = 0; } if((tmp = CGI_Find_Parameter(input, "limit"))) { sFileListParam.limit = atoi(tmp->val); } else { sFileListParam.limit = 0; } if((tmp = CGI_Find_Parameter(input, "is_iso"))) { sFileListParam.is_iso = atoi(tmp->val); } else { sFileListParam.is_iso = 0; } if((tmp = CGI_Find_Parameter(input, "type"))) { // 1:music, 2:video, 3:photo, 4: folder sFileListParam.filetype = atoi(tmp->val); if(sFileListParam.filetype < MUSIC || sFileListParam.filetype > MEDIA) sFileListParam.filetype = 0; } if((tmp = CGI_Find_Parameter(input, "mp4_360"))) { sFileListParam.mp4_360 = atoi(tmp->val); } else { sFileListParam.mp4_360 = 0; } if((tmp = CGI_Find_Parameter(input, "mp4_720"))) { sFileListParam.mp4_720 = atoi(tmp->val); } else { sFileListParam.mp4_720 = 0; } if((tmp = CGI_Find_Parameter(input, "mp4_240"))) { sFileListParam.mp4_240 = atoi(tmp->val); } else { sFileListParam.mp4_240 = 0; } if((tmp = CGI_Find_Parameter(input, "mp4_480"))) { sFileListParam.mp4_480 = atoi(tmp->val); } else { sFileListParam.mp4_480 = 0; } if((tmp = CGI_Find_Parameter(input, "mp4_1080"))) { sFileListParam.mp4_1080 = atoi(tmp->val); } else { sFileListParam.mp4_1080 = 0; } // for special video file if((tmp = CGI_Find_Parameter(input, "filename"))) { strncpy(sFileListParam.filename, tmp->val, (MAX_FILE_LENGTH-1)); } if((tmp = CGI_Find_Parameter(input, "sort"))) { strncpy(sFileListParam.sort, tmp->val, WFM2_SORT_MODE_LEN); } if((tmp = CGI_Find_Parameter(input, "dir"))) { strncpy(sFileListParam.dir, tmp->val, WFM2_SORT_TYPE_LEN); } if((tmp = CGI_Find_Parameter(input, "hidden_file"))) { sFileListParam.hidden_file = atoi(tmp->val); } else { sFileListParam.hidden_file = 0; } nRet = handle_file_list(&sFileListParam, NULL); //MARK: original handle list function //sFileListParam.end = sFileListParam.start + sFileListParam.limit - 1; //_get_file_list(full_path, &sFileListParam, NULL); if(full_path) __ax_free(full_path); if(sFileListParam.node != NULL) __ax_free(sFileListParam.node); return nRet; }
void add_header(struct cgi_state *cgi, const char *line) { if (cgi->num_headers == MAX_HEADERS) return; cgi->extra_headers[cgi->num_headers++] = __ax_strdup(line); }