c語言http請求解析表單內(nèi)容
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() {
//獲得表單發(fā)送方法
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方法,通過環(huán)境變量得到內(nèi)容
buffer = (char *)malloc(strlen(get_str) + 1);
strcpy(buffer, get_str);
//對內(nèi)容進(jìn)行解析,以鏈表的形式存在
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方法,通過標(biāo)準(zhǔn)輸入讀取內(nèi)容
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方法獲取的內(nèi)容進(jìn)行解析
Node *analyze(char *buffer)
{
//獲取內(nèi)容格式
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) {
//該格式表明獲取內(nèi)容為"name=value"形式
return analy_a(buffer);
} else if (strcmp("text/plain", c_type) == 0) {
//此種編碼格式暫不討論
} else {
//編碼格式為multipart/form-data,適用大流量數(shù)據(jù)傳送
//獲取等號后面的分隔符
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'的內(nèi)容
Node *analy_a(char *buffer)
{
//創(chuàng)建第一個節(jié)點
Node *head = (Node *)malloc(sizeof(Node));
Node *temp = head;
temp -> name = buffer;
char *b_temp = buffer;
//通過移動、改變部分字符來分離字符串
while (*buffer != 0) {
if (*buffer == '=') {
//'=',則表示name已經(jīng)結(jié)束,value將開始
*b_temp = 0;
temp -> value = b_temp + 1;
}else if (*buffer == '+') {
//'+'代表空格
*b_temp = ' ';
}else if (*buffer == '%') {
//'%'則緊跟兩位十六進(jìn)制表示的特殊字符
*b_temp = fun1(*(buffer + 1)) * 16 + fun1(*(buffer + 2));
buffer += 2;
}
else if (*buffer == '&') {
//'&'表示value已經(jīng)結(jié)束,name即將開始
*b_temp = 0;
//重新申請內(nèi)存,存儲新內(nèi)容地址
temp -> next = (Node *)malloc(sizeof(Node));
temp = temp -> next;
temp -> name = b_temp + 1;
}else {
*b_temp = *buffer;
}
buffer++;
b_temp++;
}
//最后一個結(jié)束符
*b_temp = 0;
return head;
}
analy_m.c
#include "cgi.h"
//編碼格式為'multipart/form-data'的內(nèi)容
Node *analy_m(char *buffer, char *bound)
{
char *start;
char *end;
//第一個節(jié)點
Node *head = (Node*)malloc(sizeof(Node));
Node *temp = head;
fprintf(stderr,"debug:buffer is %s\n", buffer);
//開始解析內(nèi)容,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) {
//循環(huán)獲取name與value地址,直到?jīng)]有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
//將十六進(jìn)制字符轉(zhuǎn)化為十進(jìn)制數(shù)
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"
//根據(jù)name獲取相應(yīng)的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"
//釋放動態(tài)獲取的內(nèi)存
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);
}
相關(guān)文章
Spark SQL 中對 Map 類型的操作函數(shù)示例詳解
這篇文章主要介紹了SparkSQL中對Map類型的操作函數(shù),包括創(chuàng)建、訪問、修改、合并、鍵值操作等功能,通過這些函數(shù),可以方便地進(jìn)行復(fù)雜鍵值對數(shù)據(jù)的處理,感興趣的朋友跟隨小編一起看看吧2025-01-01
sqlserver replace函數(shù) 批量替換數(shù)據(jù)庫中指定字段內(nèi)指定字符串參考方法
SQL Server有 replace函數(shù),可以直接使用;Access數(shù)據(jù)庫的replace函數(shù)只能在Access環(huán)境下用,不能用在Jet SQL中,所以對ASP沒用,在ASP中調(diào)用該函數(shù)會提示錯誤.2010-05-05
SQL SERVER 2012新增函數(shù)之邏輯函數(shù)IIF
這篇文章主要介紹了關(guān)于SQL SERVER 2012新增函數(shù)之邏輯函數(shù)IIF的相關(guān)資料,網(wǎng)上關(guān)于邏輯函數(shù)IIF的資料比較少,本文中的介紹的還是相對詳細(xì)的,需要的朋友可以參考借鑒,下面來一起看看吧。2017-03-03
SQL Server修改標(biāo)識列方法 如自增列的批量化修改
最近在運(yùn)行系統(tǒng)時需要對所有服務(wù)器上數(shù)據(jù)結(jié)構(gòu)進(jìn)行批量修改某個字段的自增屬性改成非自增2012-05-05
sqlserver查找括號()中字符串內(nèi)容的方法實現(xiàn)
本文主要介紹了sqlserver查找括號()中字符串內(nèi)容的方法實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
SQL Server 2012 sa用戶登錄錯誤18456的解決方法
這篇文章主要為大家詳細(xì)介紹了SQL Server 2012 sa用戶登錄錯誤18456的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09
精妙的SQL和SQL SERVER 與ACCESS、EXCEL的數(shù)據(jù)導(dǎo)入導(dǎo)出轉(zhuǎn)換
sqlserver 與access,excel互相導(dǎo)入導(dǎo)出代碼2008-03-03
安裝sqlserver2022提示缺少msodbcsql.msi錯誤消息的解決
本文主要介紹了安裝sqlserver2022提示缺少msodbcsql.msi錯誤消息,msoledbsql.msi文件是Microsoft OLE DB Provider for SQL Server的安裝文件,下面就來介紹一下解決方法2024-05-05

