c說話http要求解析表單內容。本站提示廣大學習愛好者:(c說話http要求解析表單內容)文章只能為提供參考,不一定能成為您想要的結果。以下是c說話http要求解析表單內容正文
cgi.h
#ifndef CGI_H
#define CGI_H
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Node{
char *name;
char *value;
struct Node *next;
}Node;
typedef struct Index{
Node *head;
char *buffer;
}Index;
Index *get_input();
void free_input(Index *);
Node *analyze(char *);
Node *analy_a(char *);
Node *analy_m(char *, char *);
char *get_value(Node *, char *);
char fun1(char);
#endif
get_input.c
#include "cgi.h"
Index *get_input() {
//取得表單發送辦法
char *get_method = getenv("REQUEST_METHOD");
Index *input = (Index *)malloc(sizeof(Index));
Node *head;
char *buffer;
if (strcmp(get_method,"GET") == 0) {
char *get_str = getenv("QUERY_STRING");
if (get_str == NULL || *get_str == 0) {
return NULL;
}
//get辦法,經由過程情況變量獲得內容
buffer = (char *)malloc(strlen(get_str) + 1);
strcpy(buffer, get_str);
//對內容停止解析,以鏈表的情勢存在
head = analy_a(buffer);
} else if (strcmp(get_method,"POST") == 0){
int get_len = atoi(getenv("CONTENT_LENGTH"));
if (get_len == 0) {
return NULL;
}
//post辦法,經由過程尺度輸出讀取內容
buffer = (char *)malloc(get_len + 1);
memset(buffer,0,get_len + 1);
int n = fread(buffer, 1,get_len, stdin);
if (n != get_len) {
fprintf(stderr,"Read error!");
}
head = analyze(buffer);
}
//鏈表頭
input -> head = head;
//接收到的字符串
input -> buffer = buffer;
return input;
}
analyze.c
#include "cgi.h"
//post辦法獲得的內容停止解析
Node *analyze(char *buffer)
{
//獲得內容格局
char *c_type = getenv("CONTENT_TYPE");
char *bound;
fprintf(stderr,"debug:c_type is %s\n",c_type);
if (strcmp("application/x-www-form-urlencoded",c_type) == 0) {
//該格局注解獲得內容為"name=value"情勢
return analy_a(buffer);
} else if (strcmp("text/plain", c_type) == 0) {
//此種編碼格局暫不評論辯論
} else {
//編碼格局為multipart/form-data,實用年夜流量數據傳送
//獲得等號前面的分隔符
bound = index(c_type,'=') + 1;
fprintf(stderr,"debug:bound is %s\n",bound);
return analy_m(buffer, bound);
}
}
analy_a.c
#include "cgi.h"
//編碼格局為'application/x-www-form-urlencoded'的內容
Node *analy_a(char *buffer)
{
//創立第一個節點
Node *head = (Node *)malloc(sizeof(Node));
Node *temp = head;
temp -> name = buffer;
char *b_temp = buffer;
//經由過程挪動、轉變部門字符來分別字符串
while (*buffer != 0) {
if (*buffer == '=') {
//'=',則表現name曾經停止,value將開端
*b_temp = 0;
temp -> value = b_temp + 1;
}else if (*buffer == '+') {
//'+'代表空格
*b_temp = ' ';
}else if (*buffer == '%') {
//'%'則緊跟兩位十六進制表現的特別字符
*b_temp = fun1(*(buffer + 1)) * 16 + fun1(*(buffer + 2));
buffer += 2;
}
else if (*buffer == '&') {
//'&'表現value曾經停止,name行將開端
*b_temp = 0;
//從新請求內存,存儲新內容地址
temp -> next = (Node *)malloc(sizeof(Node));
temp = temp -> next;
temp -> name = b_temp + 1;
}else {
*b_temp = *buffer;
}
buffer++;
b_temp++;
}
//最初一個停止符
*b_temp = 0;
return head;
}
analy_m.c
#include "cgi.h"
//編碼格局為'multipart/form-data'的內容
Node *analy_m(char *buffer, char *bound)
{
char *start;
char *end;
//第一個節點
Node *head = (Node*)malloc(sizeof(Node));
Node *temp = head;
fprintf(stderr,"debug:buffer is %s\n", buffer);
//開端解析內容,name在兩個雙引號之間(詳見編碼格局)
temp -> name = index(buffer, '"') + 1;
end = index(temp -> name, '"');
*end = 0;
fprintf(stderr,"debug:temp->name is %s\n", temp -> name);
//中央距離了兩個"\r\n"
temp -> value = end + 5;
buffer = strstr(temp -> value, bound);
//到下一個距離符,下面距離兩個"\r\n"
*(buffer - 4) = 0;
fprintf(stderr,"debug:temp->valu is %s\n", temp -> value);
while ((start = strstr(buffer,"name=")) != NULL) {
//輪回獲得name與value地址,直到沒有name為止
temp -> next = (Node *)malloc(sizeof(Node));
temp = temp -> next;
temp -> name = index(start, '"') + 1;
end = index(temp -> name, '"');
*end = 0;
fprintf(stderr,"debug:temp->name is %s\n", temp -> name);
temp -> value = end + 5;
buffer = strstr(temp -> value, bound);
*(buffer - 4) = 0;
fprintf(stderr,"debug:temp->valu is %s\n", temp -> value);
}
return head;
}
fun1.c
//將十六進制字符轉化為十進制數
char fun1(char ch)
{
char buffer;
if (ch < 'A') {
buffer = ch - 48;
}else if (ch < 'a'){
buffer = ch - 55;
}else {
buffer = ch - 87;
}
return buffer;
}
get_value.c
#include "cgi.h"
//依據name獲得響應的value
char *get_value(Node *head, char *name)
{
Node *p;
while (head != NULL) {
if (strcmp(head -> name, name) == 0) {
return head -> value;
}
p = head -> next;
head = p;
}
return NULL;
}
free_input.c
#include "cgi.h"
//釋放靜態獲得的內存
void free_input(Index *index)
{
Node *temp = index -> head;
Node *p;
while (temp != NULL) {
p = temp -> next;
free(temp);
temp = p;
}
free(index -> buffer);
free(index);
}