In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
What this article shares to you is about how to use http request to parse the form content in the c language, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
The code is as follows:
# ifndef CGI_H#define CGI_H
# include # include # include
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
The code is as follows:
# include "cgi.h"
Index * get_input () {/ / get the form delivery method 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 method, through the environment variable to get the content buffer = (char *) malloc (strlen (get_str) + 1); strcpy (buffer, get_str); / / A pair of content is parsed, and head = analy_a (buffer) exists in the form of a linked list
} else if (strcmp (get_method, "POST") = = 0) {int get_len = atoi (getenv ("CONTENT_LENGTH")); if (get_len = = 0) {return NULL;} / / post method, read through standard input buffer = (char *) malloc (get_len + 1); memset (buffer,0,get_len + 1); int n = fread (buffer, 1m GetWorlen, stdin); if (n! = get_len) {fprintf (stderr, "Read error!") } head = analyze (buffer);} / / chain header input-> head = head;// received string input-> buffer = buffer;return input;}
Analyze.c
The code is as follows:
Parse the content obtained by # include "cgi.h" / / post method Node * analyze (char * buffer) {/ / get the content format 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) {/ / this format indicates that the acquired content is in the form of "name=value" return analy_a (buffer) } else if (strcmp ("text/plain", c_type) = = 0) {/ / this encoding format will not be discussed for the time being} else {/ / the encoding format is multipart/form-data, which is suitable for large traffic data transmission / / acquisition of the delimiter bound = index after the equal sign (stderr, "debug:bound is% s\ n", bound); return analy_m (buffer, bound);}}
Analy_a.c
The code is as follows:
# include "cgi.h" / / Encoding the content in 'application/x-www-form-urlencoded' Node * analy_a (char * buffer) {/ / create the first node Node * head = (Node *) malloc (sizeof (Node)); Node * temp = head;temp-> name = buffer;char * b_temp = buffer / / separating the string while (* buffer! = 0) {if (* buffer = ='=') {/ /'=') {/ /'='by moving and changing some characters means that name has ended and value will start * b_temp = 0 else if-> value = b_temp + 1;} else if (* buffer = ='+') {/ /'+ 'represents a space * b_temp ='' } else if (* buffer = ='%') {/ /'%'is followed by the special character * b_temp = fun1 (* (buffer + 1)) * 16 + fun1 (* (buffer + 2)); buffer + = 2;} else if (* buffer = ='&') {/ /'& 'indicates that value is over and name is about to begin * b_temp = 0 / / re-apply for memory and store the new content address temp-> next = (Node *) malloc (sizeof (Node)); temp = temp-> next;temp-> name = breadtemp + 1;} else {* b_temp = * buffer;} buffer++;b_temp++;} / / the last Terminator * b_temp = 0position return head;}
Analy_m.c
The code is as follows:
# include "cgi.h" / / content Node* analy_m (char * buffer, char * bound) {char * start;char * end;// first node Node* head = (Node*) malloc (sizeof (Node)); Node* temp = head;fprintf (stderr, "debug:buffer is% s\ n", buffer) / / start parsing the content, with name between two double quotes (see encoding format) temp- > name = index (buffer,'") + 1 end = index (temp- > name,'"'"); * end = 0 stderr fprintf (stderr," debug:temp- > name is% s\ n ", temp- > name); / / separated by two"\ r\ n "temp- > value = end + 5 buffer = strstr (temp- > value, bound) / / to the next spacer, separated by two "\ r\ n" * (buffer-4) = 0politics fprintf (stderr, "debug:temp- > valu is% s\ n", temp- > value); while ((start = strstr (buffer, "name=")! = NULL) {/ / cycle to obtain name and value addresses until there is no name temp- > next = (Node *) malloc (sizeof (Node); temp = temp- > next) Temp- > name = index (start,'"') + 1 position 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 position fprintf (stderr," debug:temp- > valu is% s\ n ", temp- > value);} return head;}
Fun1.c
The code is as follows:
/ / convert hexadecimal characters to decimal numbers 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获取相应的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
The code is as follows:
# include "cgi.h" / / release dynamically acquired memory void free_input (Index * index) {Node * temp = index-> head;Node * ppolitics while (temp! = NULL) {p = temp-> next;free (temp); temp = p;} free (index-> buffer); free (index);}
The above is how to parse the form content using http request in c language. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.