溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

c語言中怎么利用http請求解析表單內(nèi)容

發(fā)布時間:2021-08-13 16:03:10 來源:億速云 閱讀:180 作者:Leah 欄目:數(shù)據(jù)庫

本篇文章給大家分享的是有關(guān)c語言中怎么利用http請求解析表單內(nèi)容,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

代碼如下:

 #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é)點(diǎn)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é)點(diǎn)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)的valuechar *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);}

以上就是c語言中怎么利用http請求解析表單內(nèi)容,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI