C Language HTTP request parsing form content _mssql

Source: Internet
Author: User

Cgi.h

Copy Code code as follows:

#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

Copy Code code as follows:

#include "cgi.h"

Index *get_input () {
 //get form Send 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;
&NBSP;&NBSP}
  //get method, get content through environment variable
  buffer = (char *) malloc (strlen (GET_STR) + 1);
  strcpy (buffer, GET_STR); The
  //resolves the content to exist in the form of a linked list
  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 method, reading content through standard input
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);
}
Linked table Header
Input-> head = head;
The string that was received
Input-> buffer = buffer;
return input;
}

Analyze.c

Copy Code code as follows:

#include "cgi.h"
The Post method gets the content to parse
Node *analyze (char *buffer)
{
Get 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) {
The format indicates that the obtained content is "name=value" form
return analy_a (buffer);
else if (strcmp ("Text/plain", c_type) = = 0) {
This encoding format is not currently discussed
} else {
The encoding format is multipart/form-data, suitable for large flow data transmission
Get the separator after the equal sign
bound = index (c_type, ' = ') + 1;
fprintf (stderr, "Debug:bound is%s\n", bound);
Return analy_m (buffer, bound);
}
}

Analy_a.c

Copy Code code as follows:

#include "cgi.h"
The content of the encoded format is ' application/x-www-form-urlencoded '
Node *analy_a (char *buffer)
{
Create a first node
Node *head = (node *) malloc (sizeof (node));
Node *temp = head;
Temp-> name = buffer;
char *b_temp = buffer;
Separating strings by moving or changing part characters
while (*buffer!= 0) {
if (*buffer = = ' = ') {
' = ' to indicate that name has ended and value will begin
*b_temp = 0;
Temp-> value = b_temp + 1;
}else if (*buffer = = ' + ') {
' + ' represents a space
*b_temp = ';
}else if (*buffer = = '% ') {
'% ' is followed by a special character in the two-bit hexadecimal notation
*b_temp = FUN1 (* (buffer + 1)) * + fun1 (* (buffer + 2));
Buffer + 2;
}
else if (*buffer = = ' & ') {
' & ' indicates that value has ended and name is about to begin
*b_temp = 0;
Re-request memory, store new content address
Temp-> Next = (node *) malloc (sizeof (node));
temp = temp-> next;
Temp-> name = b_temp + 1;
}else {
*b_temp = *buffer;
}
buffer++;
b_temp++;
}
Last Terminator
*b_temp = 0;
return head;
}

Analy_m.c

Copy Code code as follows:

#include "cgi.h"
The content of the encoded format is ' Multipart/form-data '
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 content, name is between two double quotes (see encoding format)
Temp-> name = index (buffer, ' ") + 1;
End = Index (temp-> name, ' "');
*end = 0;
fprintf (stderr, "Debug:temp->name is%s\n", temp-> name);
Two "\ r \ n" in the middle interval
Temp-> value = end + 5;
Buffer = STRSTR (temp-> value, bound);
To the next spacer with two "\ r \ n" Intervals above
* (buffer-4) = 0;
fprintf (stderr, "Debug:temp->valu is%s\n", temp-> value);
while ((start = strstr (buffer, "name="))!= NULL) {
Loop to get name and value address until No 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

Copy Code code as follows:

Converts a hexadecimal character to a decimal number
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

Copy Code code as follows:

#include "cgi.h"
Get the corresponding value based on name
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

Copy Code code as follows:

#include "cgi.h"
Free dynamically acquired memory
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);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.