parse_8c.tex 7.68 KB
\hypertarget{parse_8c}{
\section{src/http/request/parser/parse.c File Reference}
\label{parse_8c}\index{src/http/request/parser/parse.c@{src/http/request/parser/parse.c}}
}
{\ttfamily \#include $<$stdlib.h$>$}\par
{\ttfamily \#include $<$string.h$>$}\par
{\ttfamily \#include $<$unistd.h$>$}\par
{\ttfamily \#include $<$ctype.h$>$}\par
{\ttfamily \#include \char`\"{}http/request\_\-parser.h\char`\"{}}\par
{\ttfamily \#include \char`\"{}interface/class.h\char`\"{}}\par
Include dependency graph for parse.c:
\subsection*{Defines}
\begin{DoxyCompactItemize}
\item 
\#define \hyperlink{parse_8c_a3d501f4eabec1eb9a20aed0a116b69a3}{REMAINS}(pars, done)~((pars)-\/$>$buffer\_\-used -\/ ((done) -\/ (pars)-\/$>$buffer))
\end{DoxyCompactItemize}
\subsection*{Functions}
\begin{DoxyCompactItemize}
\item 
void \hyperlink{parse_8c_a187bf52da83090068bafea54589f6f69}{httpRequestParserGetRequestLine} (\hyperlink{structHttpRequest}{HttpRequest}, char $\ast$)
\item 
void \hyperlink{parse_8c_a9bb74bc564fe961a7ab5459ef5806de6}{httpRequestParserParse} (\hyperlink{structHttpRequestParser}{HttpRequestParser} this)
\end{DoxyCompactItemize}


\subsection{Define Documentation}
\hypertarget{parse_8c_a3d501f4eabec1eb9a20aed0a116b69a3}{
\index{parse.c@{parse.c}!REMAINS@{REMAINS}}
\index{REMAINS@{REMAINS}!parse.c@{parse.c}}
\subsubsection[{REMAINS}]{\setlength{\rightskip}{0pt plus 5cm}\#define REMAINS(
\begin{DoxyParamCaption}
\item[{}]{pars, }
\item[{}]{done}
\end{DoxyParamCaption}
)~((pars)-\/$>$buffer\_\-used -\/ ((done) -\/ (pars)-\/$>$buffer))}}
\label{parse_8c_a3d501f4eabec1eb9a20aed0a116b69a3}


Definition at line 10 of file parse.c.



\subsection{Function Documentation}
\hypertarget{parse_8c_a187bf52da83090068bafea54589f6f69}{
\index{parse.c@{parse.c}!httpRequestParserGetRequestLine@{httpRequestParserGetRequestLine}}
\index{httpRequestParserGetRequestLine@{httpRequestParserGetRequestLine}!parse.c@{parse.c}}
\subsubsection[{httpRequestParserGetRequestLine}]{\setlength{\rightskip}{0pt plus 5cm}void httpRequestParserGetRequestLine (
\begin{DoxyParamCaption}
\item[{{\bf HttpRequest}}]{, }
\item[{char $\ast$}]{}
\end{DoxyParamCaption}
)}}
\label{parse_8c_a187bf52da83090068bafea54589f6f69}


Definition at line 7 of file get\_\-request\_\-line.c.


\begin{DoxyCode}
{
        char * method, * uri, * version;

        method = line;

        uri    = strchr(line, ' ');
        *uri++ = 0;
        for (; *uri == ' ' && *uri != 0; uri++);

        version    = strchr(uri, ' ');
        *version++ = 0;
        for (; *version == ' ' && *version != 0; version++);

        request->method  = malloc(strlen(method) + 1);
        strcpy(request->method, method);
        request->uri     = malloc(strlen(uri) + 1);
        strcpy(request->uri, uri);
        request->version = malloc(strlen(version) + 1);
        strcpy(request->version, method);
}
\end{DoxyCode}


Here is the caller graph for this function:


\hypertarget{parse_8c_a9bb74bc564fe961a7ab5459ef5806de6}{
\index{parse.c@{parse.c}!httpRequestParserParse@{httpRequestParserParse}}
\index{httpRequestParserParse@{httpRequestParserParse}!parse.c@{parse.c}}
\subsubsection[{httpRequestParserParse}]{\setlength{\rightskip}{0pt plus 5cm}void httpRequestParserParse (
\begin{DoxyParamCaption}
\item[{{\bf HttpRequestParser}}]{this}
\end{DoxyParamCaption}
)}}
\label{parse_8c_a9bb74bc564fe961a7ab5459ef5806de6}


enqueue current request

remove processed stuff from input buffer.

dont continue loop if input buffer is empty

prepare for next request



Definition at line 43 of file parse.c.


\begin{DoxyCode}
{
        static HttpRequest request  = NULL;
        static char *      data; // static pointer to unprocessed data
        char *             line;
        int                cont = 1;

        while(cont) {
                switch(this->state) {
                        case HTTP_REQUEST_GARBAGE:
                                data = this->buffer; // initialize static pointer
      
                                httpRequestSkip(&data);
                                request = new(HttpRequest);

                                this->state = HTTP_REQUEST_START;
                                break;

                        case HTTP_REQUEST_START:
                                if (NULL == (line = httpRequestParserGetLine(&dat
      a))) {
                                        cont = 0;
                                        break;
                                }
                                
                                httpRequestParserGetRequestLine(request, line);

                                this->state = HTTP_REQUEST_REQUEST_LINE_DONE;
                                break;

                        case HTTP_REQUEST_REQUEST_LINE_DONE:
                                if (NULL == (line = httpRequestParserGetLine(&dat
      a))) {
                                        cont = 0;
                                        break;
                                }

                                if (0 == strlen(line)) {
                                        this->state = HTTP_REQUEST_HEADERS_DONE;
                                        break;
                                }

                                httpRequestParserGetHeader(request, line);
                                break;

                        case HTTP_REQUEST_HEADERS_DONE:
                                httpHeaderSort(request->header, request->nheader)
      ;

                                {
                                        char * nbody;

                                        if (0 == request->nbody) {
                                                nbody = httpHeaderGet(
                                                                request->header,
                                                                request->nheader,
      
                                                                "Content-Length")
      ;

                                                if (NULL == nbody) {
                                                        this->state = 
      HTTP_REQUEST_DONE;
                                                        break;
                                                }
                                                else {
                                                        request->nbody = atoi(nbo
      dy);
                                                }
                                        }

                                        if (REMAINS(this, data) >= request->
      nbody) {
                                                request->body = calloc(1, request
      ->nbody + 1);
                                                memcpy(request->body, data, reque
      st->nbody);
                                                data += request->nbody;
                                                this->state = HTTP_REQUEST_DONE;
                                        }
                                }
                                        
                                break;

                        case HTTP_REQUEST_DONE:
                                this->request_queue->requests[(this->request_queu
      e->nrequests)++] =
                                        request;

                                memmove(this->buffer, data, REMAINS(this, data));
      

                                this->buffer_used -= data - this->buffer;

                                if (0 == this->buffer_used) {
                                        cont = 0;
                                }

                                this->state = HTTP_REQUEST_GARBAGE;

                                break;

                        default:
                                break;
                }
        }
}
\end{DoxyCode}


Here is the call graph for this function: