/* parse a multipart encoded form (for file upload) see rfc1867 */ static void load_multipart(struct cgi_state *cgi) { char *boundary; FILE *f = stdin; int len = cgi->content_length; char *line; if (!cgi->content_type) return; boundary = strstr(cgi->content_type, "boundary="); if (!boundary) return; boundary += 9; trim_tail(boundary, CRLF); line = grab_line(f, CRLF, &len); if (strncmp(line,"--", 2) != 0 || strncmp(line+2,boundary,strlen(boundary)) != 0) { fprintf(stderr,"Malformed multipart?\n"); free(line); return; } if (strcmp(line+2+strlen(boundary), "--") == 0) { /* the end is only the beginning ... */ free(line); return; } free(line); while (load_one_part(cgi, f, &len, boundary) == 0) ; }
void mfc_term_view::send_or_copy() // bound to Enter in term_view { long i, m; GetRichEditCtrl().GetSel(i, m); m = GetRichEditCtrl().LineIndex(GetRichEditCtrl().LineFromChar(mark)); if (i >= m) { // send new input as command line m = eob(); //if (GetRichEditCtrl().LineLength(m)) { GetRichEditCtrl().SetSel(m, m); GetRichEditCtrl().ReplaceSel("\r\n"); GetRichEditCtrl().GetSel(i, m); //} GetRichEditCtrl().SetSel(mark, m); CString txt = GetRichEditCtrl().GetSelText(); if (mfc_deliver(txt, txt.GetLength())) { hist_view->save_line(txt); mark = m; } GetRichEditCtrl().SetSel(m, m); } else { // copy current line as pending new command line CString txt = grab_line(); set_command(w_deprompt((char *)(const char *)txt)); } }
void mfc_term_view::recall_line() // bound to Enter in hist_view { CString txt = grab_line(); term_view->set_command(txt); long i = bol(0); // get first char of current line if (i == bol(1)) { // this line typed at end: send it term_view->send_or_copy(); i = eob(); } else if (term_view->is_visible) { CMDIFrameWnd *fw = (CMDIFrameWnd *)(the_boss.m_pMainWnd); fw->MDIActivate(term_view->GetParent()); } GetRichEditCtrl().SetSel(i, i); }
/* parse a url encoded form */ static void load_urlencoded(struct cgi_state *cgi) { int len = cgi->content_length; char *line; char *p; FILE *f = stdin; while (len && (line=grab_line(f, "&", &len))) { p = strchr(line,'='); if (p) { *p = 0; put(cgi, line, p+1); } free(line); } }
/* parse a single element of a multipart encoded form It's rather more complex than I would like :( */ static int load_one_part(struct cgi_state *cgi, FILE *f, int *len, char *boundary) { char *line; char *name=NULL; char *content; char *filename=NULL; unsigned content_len=0, content_alloc=1024; unsigned boundary_len = strlen(boundary); int c; int raw_data = 0; while (*len && (line=grab_line(f, CRLF, len))) { if (*line == 0) break; if (strcmp(line,"--") == 0) return 1; if (strncasecmp(line, CONTENT_TYPE, strlen(CONTENT_TYPE)) == 0) { raw_data = 1; } if (strncasecmp(line, CONTENT_DISPOSITION, strlen(CONTENT_DISPOSITION)) == 0) { char *p = strstr(line,"; name="); if (!p) continue; p += 7; if (*p == '"') p++; name = strndup(p, strcspn(p, "\";")); p = strstr(line,"; filename=\""); if (p) { p += 12; filename = strndup(p, strcspn(p, "\";")); } } } content = malloc(content_alloc); while (*len && (c = fgetc(f)) != EOF) { (*len)--; if (content_len >= (content_alloc-1)) { content_alloc *= 2; content = realloc(content, content_alloc); } content[content_len++] = c; /* we keep grabbing content until we hit a boundary */ if (memcmp(boundary, &content[content_len-boundary_len], boundary_len) == 0 && memcmp("--", &content[content_len-boundary_len-2], 2) == 0) { content_len -= boundary_len+4; if (name) { if (raw_data || filename) { put(cgi, name, filename?filename:""); cgi->variables->content = content; cgi->variables->content_len = content_len; } else { content[content_len] = 0; put(cgi, name, content); free(name); free(content); } } else { free(content); } fgetc(f); fgetc(f); (*len) -= 2; return 0; } } if (filename) free(filename); return 1; }
/*************************************************************************** load all the variables passed to the CGI program. May have multiple variables with the same name and the same or different values. Takes a file parameter for simulating CGI invocation eg loading saved preferences. ***************************************************************************/ void cgi_load_variables(FILE *f1) { FILE *f = f1; static char *line; char *p, *s, *tok; int len; #ifdef DEBUG_COMMENTS char dummy[100]=""; print_title(dummy); printf("<!== Start dump in cgi_load_variables() %s ==>\n",__FILE__); #endif if (!f1) { f = stdin; if (!content_length) { p = getenv("CONTENT_LENGTH"); len = p?atoi(p):0; } else { len = content_length; } } else { fseek(f, 0, SEEK_END); len = ftell(f); fseek(f, 0, SEEK_SET); } if (len > 0 && (f1 || request_post || ((s=getenv("REQUEST_METHOD")) && strcasecmp(s,"POST")==0))) { while (len && (line=grab_line(f, &len))) { p = strchr(line,'='); if (!p) continue; *p = 0; variables[num_variables].name = strdup(line); variables[num_variables].value = strdup(p+1); free(line); if (!variables[num_variables].name || !variables[num_variables].value) continue; unescape(variables[num_variables].value); unescape(variables[num_variables].name); #ifdef DEBUG_COMMENTS printf("<!== POST var %s has value \"%s\" ==>\n", variables[num_variables].name, variables[num_variables].value); #endif num_variables++; if (num_variables == MAX_VARIABLES) break; } } if (f1) { #ifdef DEBUG_COMMENTS printf("<!== End dump in cgi_load_variables() ==>\n"); #endif return; } fclose(stdin); if ((s=query_string) || (s=getenv("QUERY_STRING"))) { for (tok=strtok(s,"&;");tok;tok=strtok(NULL,"&;")) { p = strchr(tok,'='); if (!p) continue; *p = 0; variables[num_variables].name = strdup(tok); variables[num_variables].value = strdup(p+1); if (!variables[num_variables].name || !variables[num_variables].value) continue; unescape(variables[num_variables].value); unescape(variables[num_variables].name); #ifdef DEBUG_COMMENTS printf("<!== Commandline var %s has value \"%s\" ==>\n", variables[num_variables].name, variables[num_variables].value); #endif num_variables++; if (num_variables == MAX_VARIABLES) break; } } #ifdef DEBUG_COMMENTS printf("<!== End dump in cgi_load_variables() ==>\n"); #endif }
/*************************************************************************** load all the variables passed to the CGI program. May have multiple variables with the same name and the same or different values. Takes a file parameter for simulating CGI invocation eg loading saved preferences. ***************************************************************************/ void cgi_load_variables(void) { static char *line; char *p, *s, *tok; int len, i; FILE *f = stdin; #ifdef DEBUG_COMMENTS char dummy[100]=""; print_title(dummy); d_printf("<!== Start dump in cgi_load_variables() %s ==>\n",__FILE__); #endif if (!content_length) { p = getenv("CONTENT_LENGTH"); len = p?atoi(p):0; } else { len = content_length; } if (len > 0 && (request_post || ((s=getenv("REQUEST_METHOD")) && strequal(s,"POST")))) { while (len && (line=grab_line(f, &len))) { p = strchr_m(line,'='); if (!p) continue; *p = 0; variables[num_variables].name = SMB_STRDUP(line); variables[num_variables].value = SMB_STRDUP(p+1); SAFE_FREE(line); if (!variables[num_variables].name || !variables[num_variables].value) continue; plus_to_space_unescape(variables[num_variables].value); rfc1738_unescape(variables[num_variables].value); plus_to_space_unescape(variables[num_variables].name); rfc1738_unescape(variables[num_variables].name); #ifdef DEBUG_COMMENTS printf("<!== POST var %s has value \"%s\" ==>\n", variables[num_variables].name, variables[num_variables].value); #endif num_variables++; if (num_variables == MAX_VARIABLES) break; } } fclose(stdin); open("/dev/null", O_RDWR); if ((s=query_string) || (s=getenv("QUERY_STRING"))) { char *saveptr; for (tok=strtok_r(s, "&;", &saveptr); tok; tok=strtok_r(NULL, "&;", &saveptr)) { p = strchr_m(tok,'='); if (!p) continue; *p = 0; variables[num_variables].name = SMB_STRDUP(tok); variables[num_variables].value = SMB_STRDUP(p+1); if (!variables[num_variables].name || !variables[num_variables].value) continue; plus_to_space_unescape(variables[num_variables].value); rfc1738_unescape(variables[num_variables].value); plus_to_space_unescape(variables[num_variables].name); rfc1738_unescape(variables[num_variables].name); #ifdef DEBUG_COMMENTS printf("<!== Commandline var %s has value \"%s\" ==>\n", variables[num_variables].name, variables[num_variables].value); #endif num_variables++; if (num_variables == MAX_VARIABLES) break; } } #ifdef DEBUG_COMMENTS printf("<!== End dump in cgi_load_variables() ==>\n"); #endif /* variables from the client are in UTF-8 - convert them to our internal unix charset before use */ for (i=0;i<num_variables;i++) { TALLOC_CTX *frame = talloc_stackframe(); char *dest = NULL; size_t dest_len; convert_string_allocate(frame, CH_UTF8, CH_UNIX, variables[i].name, strlen(variables[i].name), &dest, &dest_len, True); SAFE_FREE(variables[i].name); variables[i].name = SMB_STRDUP(dest ? dest : ""); dest = NULL; convert_string_allocate(frame, CH_UTF8, CH_UNIX, variables[i].value, strlen(variables[i].value), &dest, &dest_len, True); SAFE_FREE(variables[i].value); variables[i].value = SMB_STRDUP(dest ? dest : ""); TALLOC_FREE(frame); } }
/* parse a single element of a multipart encoded form It's rather more complex than I would like :( */ int load_one_part(struct cgi_state *cgi, FILE *f, int *len, char *boundary) { char *line; char *name=NULL; char *content; char *filename=NULL; unsigned content_len=0, content_alloc=1024; unsigned boundary_len = strlen(boundary); int c; int raw_data = 0; int towrite = 0; const char *chunked_upload = get(cgi, "func"); const char *upload_root_dir = get(cgi, "upload_root_dir"); const char *upload_id = get(cgi, "upload_id"); char tmp_path[2048] = ""; FILE *file = NULL; int is_chunked_upload = 0; if (chunked_upload){ if (!strcmp(chunked_upload, "chunked_upload")) { is_chunked_upload = 1; // Write file with upload_id : Combine the path to upload_id temp file strcpy(tmp_path, "/tmp/mnt"); strcat(tmp_path, upload_root_dir); strcat(tmp_path, "/"); strcat(tmp_path, ".upload_cache"); strcat(tmp_path, "/"); strcat(tmp_path, upload_id); // Open file file = fopen(tmp_path, "a"); } } while (*len && (line=grab_line(f, CRLF, len))) { if (*line == 0) break; if (strcmp(line,"--") == 0) return 1; if (strncasecmp(line, CONTENT_TYPE, strlen(CONTENT_TYPE)) == 0) { raw_data = 1; } if (strncasecmp(line, CONTENT_DISPOSITION, strlen(CONTENT_DISPOSITION)) == 0) { char *p = strstr(line,"; name="); if (!p) continue; p += 7; if (*p == '"') p++; name = strndup(p, strcspn(p, "\";")); p = strstr(line,"; filename=\""); if (p) { p += 12; filename = strndup(p, strcspn(p, "\";")); } } } if (raw_data && name && (tmp_path[0] != '\0') && file) { // If you want to write file towrite = write_one_part(cgi, f, len, boundary, filename, name, tmp_path, file); } else { // If you don't want to write file content = __ax_malloc(content_alloc); while (*len && (c = fgetc(f)) != EOF) { (*len)--; if (content_len >= (content_alloc-1)) { content_alloc *= 2; content = __ax_realloc(content, content_alloc); } content[content_len++] = c; /* we keep grabbing content until we hit a boundary */ if (memcmp(boundary, &content[content_len-boundary_len], boundary_len) == 0 && memcmp("--", &content[content_len-boundary_len-2], 2) == 0) { content_len -= boundary_len+4; if (name) { if (raw_data || filename) { put(cgi, name, filename?filename:""); cgi->variables->content = content; cgi->variables->content_len = content_len; } else { content[content_len] = 0; put(cgi, name, content); __ax_free(name); __ax_free(content); } } else { __ax_free(content); } fgetc(f); fgetc(f); (*len) -= 2; return 0; } } } if (filename) __ax_free(filename); if (file) fclose(file); return 1; }