c言語httpリクエスト解析フォーム内容

6507 ワード

cgi.h
 
  
#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
 
  
#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
",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
",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
", buffer);
 // ,name ( )
 temp -> name = index(buffer, '"') + 1;
 end = index(temp -> name, '"');
 *end = 0;
 fprintf(stderr,"debug:temp->name is %s
", temp -> name);
 // "\r
"
 temp -> value = end + 5;
 buffer = strstr(temp -> value, bound);
 // , "\r
"
 *(buffer - 4) = 0;
 fprintf(stderr,"debug:temp->valu is %s
", 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
", temp -> name);
  temp -> value = end + 5;
  buffer = strstr(temp -> value, bound);
  *(buffer - 4) = 0;
  fprintf(stderr,"debug:temp->valu is %s
", 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);
}