keepalived源码浅析——Html

来源:互联网 发布:淘宝药品评价语50字 编辑:程序博客网 时间:2024/06/11 06:06

Html.h 源码:

parse.c中包含 check中的http方式中用到

#ifndef _HTML_H#define _HTML_H/* HTTP header tag */#define CONTENT_LENGTH"Content-Length:"    // HTTP头标志  例如Content-Length:682L 后面接着换行  /* Prototypes */extern int extract_content_length(char *buffer, int size);extern int extract_status_code(char *buffer, int size);extern char *extract_html(char *buffer, int size_buffer);#endif


Html.c 源码:

#include <string.h>#include <stdlib.h>#include "html.h"#include "memory.h"/* Return the http header content length */  //用于计算HTTP 头部内容的长度int extract_content_length(char *buffer, int size){char *clen = strstr(buffer, CONTENT_LENGTH);char *content_buffer = NULL;char *buf_len;int inc = 0;int i;/* Allocate the room */buf_len = (char *)MALLOC(40);/* Pattern not found */if (!clen)return 0;/* Content-Length extraction */while (*(clen++) != ':');content_buffer = clen;while (*(clen++) != '\r' && *clen != '\n')inc++;for (i = 0; i < inc; i++)strncat(buf_len, content_buffer+i, 1);i = atoi(buf_len);FREE(buf_len);return i;}/* * Return the http header error code. According * to rfc2616.6.1 status code is between HTTP_Version * and Reason_Phrase, separated by space caracter. */int extract_status_code(char *buffer, int size)  //返回状态码 版本后面是状态码 两边有空格分割 {char *buf_code;char *begin;char *end = buffer + size;int inc = 0;/* Allocate the room */buf_code = (char *)MALLOC(10);/* Status-Code extraction */while (buffer < end && *buffer++ != ' ') ;begin = buffer;while (buffer < end && *buffer++ != ' ')inc++;strncat(buf_code, begin, inc);inc = atoi(buf_code);FREE(buf_code);return inc;}/* simple function returning a pointer to the html buffer begin */ //返回指向html字符开始的指针char *extract_html(char *buffer, int size_buffer){char *end = buffer + size_buffer;char *cur;for (cur = buffer; cur + 3 < end; cur++)if (*cur == '\r' && *(cur+1) == '\n'    && *(cur+2) == '\r' && *(cur+3) == '\n')return cur + 4;return NULL;}


调用位置 Check_http.c

req->status_code = extract_status_code(req->buffer, req->len);