message_http.h 4.79 KB
/**
 * \file
 *
 * \author	Georg Hopp
 *
 * \copyright
 * Copyright © 2014 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/>.
 */

#ifndef __TR_PROTOCOL_MESSAGE_HTTP_H__
#define __TR_PROTOCOL_MESSAGE_HTTP_H__

#include <sys/types.h>

#include "trbase.h"
#include "trio.h"
#include "trcomm.h"

#include "tr/proto_message.h"
#include "tr/protocol/http/header.h"
#include "tr/protocol/http/data_chunk.h"

#define HTTP_EXTMETH    0x00U
#define HTTP_RESPONSE   0x01U
#define HTTP_GET        0x02U
#define HTTP_HEAD       0x04U
#define HTTP_POST       0x08U
#define HTTP_PUT        0x10U
#define HTTP_DELETE     0x20U
#define HTTP_TRACE      0x40U
#define HTTP_CONNECT    0x80U

extern const char * const TR_http_methods[];

#define TR_IS_SET(x, n)    ((n) == ((x) & (n))) // this is a more common thing and
                                             // may be moved somewhere...

#define TR_HTTP_MSG_IS_REQ(message_p)  ((message_p)->type != HTTP_RESPONSE)

#define TR_HTTP_MSG_HEADER(message_p, key) \
    (httpHeaderGetValue(&((message_p)->headers), (key)))
#define TR_HTTP_MSG_HEADER_BY_IDX(message_p, _idx) \
    ((message_p)->headers.idx[_idx]->header)

#define TR_HTTP_MSG_TYPE_ID(message_p)   ((message_p)->type)
#define TR_HTTP_MSG_VER_MAJ(message_p)   ((message_p)->version.major)
#define TR_HTTP_MSG_VER_MIN(message_p)   ((message_p)->version.minor)
#define TR_HTTP_MSG_BODY_SIZE(message_p) ((message_p)->_body_size)
#define TR_HTTP_MSG_IS_READY(message_p)  ((message_p)->_status == READY)

#define TR_HTTP_MSG_REQ(message_p, field) \
    (TR_HTTP_MSG_IS_REQ(message_p) ? (message_p)->info.request.field : NULL)

#define TR_HTTP_MSG_SET_HEADER(message_p, idx, value) \
    httpHeaderStoreByIdx(&((message_p)->headers), (idx), (value))
#define TR_HTTP_MSG_SET_CUSTOM_HEADER(message_p, key, value) \
    httpHeaderStore(&((message_p)->headers), (key), (value))

#define TR_HTTP_REQ_METHOD(message_p)  (TR_HTTP_MSG_REQ((message_p), method))
#define TR_HTTP_REQ_URI(message_p)     (TR_HTTP_MSG_REQ((message_p), uri))

#define TR_HTTP_RES_STATUS(message_p)  \
    (TR_HTTP_MSG_IS_REQ(message_p) ? -1 : (message_p)->info.response.status)
#define TR_HTTP_RES_MESSAGE(message_p) \
    (TR_HTTP_MSG_IS_REQ(message_p) ? NULL : (message_p)->info.response.message)

#define TR_HTTP_MSG_DATA_CHUNK_SIZE    2048

typedef enum {
	TR_GARBAGE=0,
	TR_TYPE,
	TR_HEAD2,
	TR_HEAD3,
	TR_HEADER,
	TR_BODY,
	TR_READY
} TR_HttpMsgStatus_e;

typedef enum {TR_KEEP_ALIVE, TR_CLOSE} TR_HttpMessageClose_e;

TR_CLASS(TR_ProtoMsgHttp) {
	TR_EXTENDS(TR_ProtoMessage);

    TR_HttpMsgStatus_e _status;
    TR_HttpDataChunk   _data;
    size_t             _body_size;
    size_t             _body_retrieved;
    int                _chunked;
    size_t             _chunk_remains;
    HttpMessageClose_e _close;
    unsigned char      type;

    struct {
        unsigned int major;
        unsigned int minor;
    }                  version;

    union {
        struct {
            char * method;
            char * uri;
//          char * uri_str;
//          struct {
//              char * scheme;
//              char * host;
//              char * path;
//          } uri;
        } request;
        struct {
            unsigned int status;
            char *       message;
        } response;
    }                  info;

    TR_Hash            headers;
	TR_HttpHeader      headers_idx[TR_N_HTTP_HEADER_IDX];
    HttpDataChunk      _body_chunks;
    size_t             _body_chunk_ofs;
};
TR_INSTANCE_INIT(TR_ProtoMsgHttp);
TR_CLASSVARS_DECL(TR_ProtoMsgHttp) {};

char * TR_httpMsgBody(TR_ProtoMsgHttp, char *);

size_t TR_httpMsgSize(TR_ProtoMsgHttp);
void   TR_httpMsgSetHeader(TR_ProtoMsgHttp, unsigned int, const char *);
void   TR_httpMsgSetCustomHeader(TR_ProtoMsgHttp, const char *, const char *);

void   TR_httpMsgHeaderDelete(TR_ProtoMsgHttp, const char * const);
char * TR_httpMsgHeaderGetValue(TR_ProtoMsgHttp, const char * const);
void   TR_httpMsgHeaderDestroy(TR_ProtoMsgHttp);
TR_HttpHeader TR_httpMsgHeaderStore(
		TR_ProtoMsgHttp, const char * const, char * const);
TR_HttpHeader TR_httpMsgHeaderStoreByIdx(
		TR_ProtoMsgHttp, unsigned int, char * const);
TR_HttpHeader TR_httpMsgHeaderGet(
		TR_ProtoMsgHttp, const char * const);

#endif // __TR_PROTOCOL_MESSAGE_HTTP_H__

// vim: set ts=4 sw=4: