/* * Parse out the callback method and * add it to the req struct. */ int parseCallback(struct extRequest *req) { int return_status = 0; int callbackLength = 0; if(cgiFormStringSpaceNeeded(CALLBACK_PARAM_NAME, &callbackLength) == cgiFormSuccess) { if(callbackLength > 0) { req->callback = (char*)malloc(sizeof(char)*callbackLength); if(cgiFormString(CALLBACK_PARAM_NAME, req->callback, callbackLength) == cgiFormSuccess) { return_status = 1; } else { free(req->callback); } } } /* * Set req->callback to NULL * so that it's not printed * in curl.c */ if(!return_status) { req->callback = NULL; } return return_status; }
/* * Function: processAddForm * Parameters: (void) * Returns: int * * This function is used to process the data that was entered in the adding * form. The function allocates memory and if it fails memory allocation * error will be printed to the user. It also checks there were no errors * in new artists id and if there are errors the appropriate message will * be shown to the user */ static Boolean processAddForm(int *errors, artistNode_t * formdata) { int result = 0; int size = -1; /* Check arguments */ if (errors == NULL || formdata == NULL) { return FALSE; } /* Get artist name */ result = cgiFormStringSpaceNeeded("artname", &size); if (result != cgiFormSuccess) { errors[0] = E_FORM; } else if (size > MAXLEN_ARTISTNAME + 1) { errors[0] = E_TOOBIG; } else { formdata->name = malloc(sizeof(char) * size); if (formdata->name == NULL) { errors[0] = E_MALLOC_FAILED; } else { result = cgiFormStringNoNewlines("artname", formdata->name, size); if (result != cgiFormSuccess) { errors[0] = E_FORM; } else if (checkString2(formdata->name) == FALSE) { errors[0] = E_INVALID_PARAM; } else { /* check whether artist already exists */ { /*pointer to list of artists */ int *allArtists = getArtists(); int i; /*counter */ for (i = 0; allArtists[i] != LAST_ID_IN_ARRAY; i++) { char *artistName = getArtistName(allArtists[i]); if (strcmp(artistName, formdata->name) == 0) { /*artist added is 'the same' as another in database */ errors[0] = ALREADY_ADDED; } free(artistName); } free(allArtists); } } } } if (errors[0] != E_NOERROR) { return FALSE; } return TRUE; }
/* * Determine the URL to send data to. */ int parseURL(struct extRequest *req) { int return_status = 0; int urlLength = 0; if(cgiFormStringSpaceNeeded(URL_PARAM_NAME, &urlLength) == cgiFormSuccess) { if(urlLength > 0) { req->url = (char*)malloc(sizeof(char)*urlLength); if(cgiFormString(URL_PARAM_NAME, req->url, urlLength) == cgiFormSuccess) { /* Success */ return_status = 1; } else { free(req->url); } } } return return_status; }
void setFieldValue(base *self, char *field_name, int not_form, void *value, field_type type, field_mark mark) { int length = 0; int name_length = strlen(field_name); if (not_form) { length = strlen(value); } else { cgiFormStringSpaceNeeded(field_name, &length); } self->form_datas[self->current] = (form_data*) malloc(sizeof(form_data)); self->form_datas[self->current]->field_type = type; self->form_datas[self->current]->field_mark = mark; self->form_datas[self->current]->field_value = (char *) malloc( sizeof(char) * (length + 1)); self->form_datas[self->current]->field_value_length = length; //fprintf(cgiOut, "%s length %ld", field_name, strlen(field_name)); self->form_datas[self->current]->field_name = (char *) malloc( sizeof(char) * (name_length + 1)); strncpy(self->form_datas[self->current]->field_name, field_name, name_length + 1); self->form_datas[self->current]->field_name_length = name_length; if (not_form) { strcpy(self->form_datas[self->current]->field_value, value); } else { cgiFormString(field_name, self->form_datas[self->current]->field_value, length); } self->field_tables[self->current] = (char *) malloc( sizeof(char) * (strlen(field_name) + 10)); strncpy(self->field_tables[self->current], field_name, strlen(field_name) + 1); #ifdef DEBUG fprintf(cgiOut, " <h1>file = %s , %s = %s , origin = %s</h1>", __FILE__, self->field_tables[self->current], self->form_datas[self->current]->field_name, field_name ); #endif self->current++; }
/* * Iterate through the passed name=value pairs * and construct extArg structs for each of them. * * Add them to the array of extArg structs inside * the req struct */ int parseArguments(struct extRequest *req) { char **array, **arrayStep; if (cgiFormEntries(&array) != cgiFormSuccess) { return 0; } int num_args = countArguments(); int arg_num = 0; req->numargs = num_args; /* make req->args big enough to hold all the extArg structs */ req->args = (struct extArg **)malloc(sizeof(struct extArg)*num_args); arrayStep = array; int val_size = 0; char *val; while (*arrayStep) { /* If the name is not a URL, method, or callback, we need to add it */ if(strcmp(*arrayStep, URL_PARAM_NAME) != 0 && strcmp(*arrayStep, CALLBACK_PARAM_NAME) != 0) { /* Get the length of the passed string so our buffer is big enough. */ cgiFormStringSpaceNeeded(*arrayStep, &val_size); val = (char*)malloc(sizeof(char)*val_size); cgiFormString(*arrayStep, val, val_size); req->args[arg_num++] = makeArg(*arrayStep, val); /* The val has been passed to makeArg, so free it */ free(val); } arrayStep++; } cgiStringArrayFree(array); return 1; }
/** * 显示登录页面以及处理用户登录 */ int login_process() { char* username = NULL; char* password = NULL; char* buffer = NULL; int field_buffer_size = 0; REPLACEABLE_TAG_LIST tag_list = (REPLACEABLE_TAG_LIST) malloc( sizeof(REPLACEABLE_TAG) * 2); // 获取用户提交的用户名和密码,如果没有提交数据,则 username 和 password 为空字符串 cgiFormStringSpaceNeeded("username", &field_buffer_size); username = (char*) malloc(field_buffer_size); memset(username, 0, field_buffer_size); cgiFormString("username", username, field_buffer_size); cgiFormStringSpaceNeeded("password", &field_buffer_size); password = (char*) malloc(field_buffer_size); cgiFormString("password", password, field_buffer_size); if (strcmp(username, g_login_username) == 0 && strcmp(password, g_login_password) == 0) { // 登录验证通过,设置 session 数据后重定向浏览器 session_set("USERNAME", g_login_username); session_write_close(); cgiHeaderLocation(cgiScriptName); free(username); free(password); return 0; } // 输出 ContentType header // 作为 CGI 程序必须输出这个 header,否则会导致 500 错误 fprintf(cgiOut, "Pragma: no-cache\n"); cgiHeaderContentType("text/html"); // 显示登录页面 tag_list[0].tag = "{%ERROR_MESSAGE%}"; if (strlen(username) != 0 || strlen(password) != 0) { tag_list[0].value = "您输入的用户名或者密码错误。"; } else { tag_list[0].value = ""; } tag_list[1].tag = "{%USERNAME%}"; tag_list[1].value = username; buffer = read_template(g_login_template); if (buffer == NULL) { fprintf(cgiOut, "Warning: Can't read template file: %s.<br />\n", g_login_template); free(username); free(password); return -1; } parse_template(&buffer, tag_list, 2); fprintf(cgiOut, "%s", buffer); free(buffer); free(username); free(password); // session_destroy(); return 0; }