Server 0.0.1
HTTP/REST server implementation

include/http/response.h File Reference

#include <time.h>
#include <sys/types.h>
#include "class.h"
#include "http/message.h"
#include "session.h"
Include dependency graph for response.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  HttpResponse

Functions

HttpResponse httpResponse304 (const char *, size_t, const char *, size_t, const char *, size_t)
HttpResponse httpResponse404 ()
HttpResponse httpResponse403 ()
HttpResponse httpResponseMe ()
HttpResponse httpResponseLoginForm ()
HttpResponse httpResponseRandval (time_t, int)
HttpResponse httpResponseSession (Session)
HttpResponse httpResponseAsset (const char *, const char *, size_t, const char *, size_t)

Detailed Description

Represents one HTTP response.

Author:
Georg Hopp

Copyright © 2012 Georg Hopp

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

Definition in file response.h.


Function Documentation

HttpResponse httpResponse304 ( const char *  ,
size_t  ,
const char *  ,
size_t  ,
const char *  ,
size_t   
)

Definition at line 35 of file 304.c.

{
        HttpResponse response;
        HttpMessage  message;

        response = new(HttpResponse, "HTTP/1.1", 304, "Not Modified");
        message  = (HttpMessage)response;

        message->type  = HTTP_MESSAGE_BUFFERED;
        message->nbody = 0;
        message->body  = NULL;

        hashAdd(message->header,
                        new(HttpHeader, CSTRA("Content-Type"), mime, nmime));
        hashAdd(message->header,
                        new(HttpHeader, CSTRA("ETag"), etag, netag));
        hashAdd(message->header,
                        new(HttpHeader, CSTRA("Last-Modified"), mtime, nmtime));

        return response;
}

Here is the call graph for this function:

Here is the caller graph for this function:

HttpResponse httpResponse403 ( )

Definition at line 36 of file 403.c.

{
        HttpResponse response;
        HttpMessage  message;

        response = new(HttpResponse, "HTTP/1.1", 403, "Forbidden");
        message  = (HttpMessage)response;

        message->type  = HTTP_MESSAGE_BUFFERED;
        message->nbody = 0;
        message->body  = NULL;

        return response;
}

Here is the caller graph for this function:

HttpResponse httpResponse404 ( )

Definition at line 47 of file 404.c.

{
        HttpResponse response;
        HttpMessage  message;

        response = new(HttpResponse, "HTTP/1.1", 404, "Not Found");
        message  = (HttpMessage)response;

        hashAdd(message->header,
                        new(HttpHeader, CSTRA("Content-Type"), CSTRA("text/html")));

        message->type  = HTTP_MESSAGE_BUFFERED;
        message->nbody = sizeof(RESP_DATA) - 1;
        message->body  = malloc(sizeof(RESP_DATA));
        memcpy(message->body, RESP_DATA, sizeof(RESP_DATA));

        return response;
}

Here is the call graph for this function:

Here is the caller graph for this function:

HttpResponse httpResponseAsset ( const char *  ,
const char *  ,
size_t  ,
const char *  ,
size_t   
)

Definition at line 41 of file asset.c.

{
        struct tm *  tmp;
        char         etag[200];
        size_t       netag;
        char         mtime[200];
        size_t       nmtime;
        struct stat  st;
        HttpResponse response;
        HttpMessage  message;
        int          handle;

        handle = open(fname, O_RDONLY);
        fstat(handle, &st);

        tmp    = localtime(&(st.st_mtime));
        netag  = strftime(etag, sizeof(etag), "%s", tmp);
        nmtime = strftime(mtime, sizeof(mtime), "%a, %d %b %Y %T %Z", tmp);

        if (netag == nmatch && 0 == memcmp(etag, match, netag)) {
                return httpResponse304(mime, nmime, etag, netag, mtime, nmtime);
        }

        response = new(HttpResponse, "HTTP/1.1", 200, "OK");
        message  = (HttpMessage)response;

        message->type   = HTTP_MESSAGE_PIPED;
        message->handle = new(Stream, STREAM_FD, handle);
        message->nbody  = st.st_size;

        hashAdd(message->header,
                        new(HttpHeader, CSTRA("Content-Type"), mime, nmime));
        hashAdd(message->header,
                        new(HttpHeader, CSTRA("ETag"), etag, netag));
        hashAdd(message->header,
                        new(HttpHeader, CSTRA("Last-Modified"), mtime, nmtime));

        return response;
}

Here is the call graph for this function:

Here is the caller graph for this function:

HttpResponse httpResponseLoginForm ( )

Definition at line 44 of file login_form.c.

{
        HttpResponse response;
        HttpMessage  message;

        response = new(HttpResponse, "HTTP/1.1", 200, "OK");
        message  = (HttpMessage)response;

        hashAdd(message->header,
                        new(HttpHeader, CSTRA("Content-Type"), CSTRA("text/html")));

        message->type  = HTTP_MESSAGE_BUFFERED;

        message->nbody = sizeof(RESP_DATA)-1;
        message->body  = malloc(message->nbody);
        memcpy(message->body, RESP_DATA, message->nbody);

        return response;
}

Here is the call graph for this function:

HttpResponse httpResponseMe ( )
HttpResponse httpResponseRandval ( time_t  ,
int   
)

Definition at line 41 of file randval.c.

{
        char         buffer[200];
        HttpResponse response;
        HttpMessage  message;
        size_t       nbuf;
        time_t       remaining;

        response = new(HttpResponse, "HTTP/1.1", 200, "OK");
        message  = (HttpMessage)response;

        hashAdd(message->header,
                        new(HttpHeader, CSTRA("Content-Type"), CSTRA("application/json")));

        message->type  = HTTP_MESSAGE_BUFFERED;

        remaining = 10 - (time(NULL) - ctime);

        nbuf = sprintf(buffer, RESP_DATA, ctime, remaining, value);

        message->nbody = nbuf;
        message->body  = malloc(nbuf);
        memcpy(message->body, buffer, nbuf);

        return response;
}

Here is the call graph for this function:

Here is the caller graph for this function:

HttpResponse httpResponseSession ( Session  )

Definition at line 42 of file session.c.

{
        char         buffer[200];
        HttpResponse response;
        HttpMessage  message;
        size_t       nbuf;

        response = new(HttpResponse, "HTTP/1.1", 200, "OK");
        message  = (HttpMessage)response;

        hashAdd(message->header,
                        new(HttpHeader, CSTRA("Content-Type"), CSTRA("application/json")));

        message->type  = HTTP_MESSAGE_BUFFERED;

        nbuf = sprintf(buffer, RESP_DATA,
                        (NULL != session)? session->id : 0,
                        (NULL != session)? SESSION_LIVETIME : 0,
                        (NULL != session)? session->livetime - time(NULL) : 0,
                        (NULL != session)? session->username : "");

        message->nbody = nbuf;
        message->body  = malloc(nbuf);
        memcpy(message->body, buffer, nbuf);

        return response;
}

Here is the call graph for this function:

Here is the caller graph for this function:

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines