/***************************************************************************
 *
 * Copyright (c) 2000 BalaBit IT Ltd, Budapest, Hungary
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Id: httpproto.c,v 1.1.4.2 2001/09/04 10:12:47 bazsi Exp $
 *
 * Author: Balazs Scheidler <bazsi@balabit.hu>
 * Auditor: 
 * Last audited version: 
 * Notes:
 *   Based on the code by: Viktor Peter Kovacs <vps__@freemail.hu>
 *
 ***************************************************************************/

#include "http.h"

#include <string.h>

/* FIXME: put these into a hashtable */

//#define HTTP_REQ_TBLSIZE (11)
//#define HTTP_RESP_TBLSIZE (37)

typedef struct _HttpRequest
{
  char name[16];
  int val;
} HttpRequest;

typedef struct _HttpResponse
{
  char name[4];
  int val;
} HttpResponse;

static HttpRequest request_table[] = 
{
  {"GET", HTTP_REQ_FLG_TRANSFER},
  {"HEAD", HTTP_REQ_FLG_TRANSFER | HTTP_REQ_FLG_HEAD},
  {"POST", HTTP_REQ_FLG_TRANSFER | HTTP_REQ_FLG_DATA},
  {"PUT", HTTP_REQ_FLG_TRANSFER | HTTP_REQ_FLG_DATA},
  {"DELETE", HTTP_REQ_FLG_TRANSFER},
  {"TRACE", HTTP_REQ_FLG_TRANSFER},
  {"OPTIONS", HTTP_REQ_FLG_TRANSFER | HTTP_REQ_FLG_ASTERIX},
  {"PATCH", HTTP_REQ_FLG_TRANSFER | HTTP_REQ_FLG_DATA},
  {"LINK", HTTP_REQ_FLG_TRANSFER},
  {"UNLINK", HTTP_REQ_FLG_TRANSFER},
  {"CONNECT", HTTP_REQ_FLG_CONNECT},
  /* WebDAV extensions */
  {"PROPFIND", HTTP_REQ_FLG_TRANSFER},
  {"PROPPATCH", HTTP_REQ_FLG_TRANSFER},
  {"MKCOL", HTTP_REQ_FLG_TRANSFER},
  {"COPY", HTTP_REQ_FLG_TRANSFER},
  {"MOVE", HTTP_REQ_FLG_TRANSFER},
  {"LOCK", HTTP_REQ_FLG_TRANSFER},  
  {"UNLOCK", HTTP_REQ_FLG_TRANSFER}
};

static HttpResponse response_table[] = 
{
  {"100", HTTP_RESP_FLG_CONTINUE},	//unused => hardcoded
  {"101", HTTP_RESP_FLG_STOP},

  {"200", HTTP_RESP_FLG_TRANSFER | HTTP_RESP_FLG_DATA_COND},
  {"201", HTTP_RESP_FLG_TRANSFER | HTTP_RESP_FLG_DATA},
  {"202", HTTP_RESP_FLG_TRANSFER | HTTP_RESP_FLG_DATA},
  {"203", HTTP_RESP_FLG_TRANSFER | HTTP_RESP_FLG_DATA_COND},
  {"204", HTTP_RESP_FLG_TRANSFER},
  {"205", HTTP_RESP_FLG_TRANSFER},
  {"206", HTTP_RESP_FLG_TRANSFER | HTTP_RESP_FLG_DATA},

  {"300", HTTP_RESP_FLG_TRANSFER | HTTP_RESP_FLG_DATA_COND},
  {"301", HTTP_RESP_FLG_TRANSFER | HTTP_RESP_FLG_DATA_COND},
  {"302", HTTP_RESP_FLG_TRANSFER | HTTP_RESP_FLG_DATA_COND},
  {"303", HTTP_RESP_FLG_TRANSFER | HTTP_RESP_FLG_DATA_COND},
  {"304", HTTP_RESP_FLG_TRANSFER},
  {"305", HTTP_RESP_FLG_TRANSFER},

  {"400", HTTP_RESP_FLG_TRANSFER | HTTP_RESP_FLG_DATA_COND},
  {"401", HTTP_RESP_FLG_TRANSFER | HTTP_RESP_FLG_DATA_COND},
  {"402", HTTP_RESP_FLG_STOP},
  {"403", HTTP_RESP_FLG_TRANSFER | HTTP_RESP_FLG_DATA_COND},
  {"404", HTTP_RESP_FLG_TRANSFER | HTTP_RESP_FLG_DATA},
  {"405", HTTP_RESP_FLG_TRANSFER | HTTP_RESP_FLG_DATA_COND},
  {"406", HTTP_RESP_FLG_TRANSFER | HTTP_RESP_FLG_DATA_COND},
  {"407", HTTP_RESP_FLG_TRANSFER | HTTP_RESP_FLG_DATA_COND},
  {"408", HTTP_RESP_FLG_STOP},
  {"409", HTTP_RESP_FLG_TRANSFER | HTTP_RESP_FLG_DATA_COND},
  {"410", HTTP_RESP_FLG_TRANSFER | HTTP_RESP_FLG_DATA_COND},
  {"411", HTTP_RESP_FLG_TRANSFER | HTTP_RESP_FLG_DATA_COND},
  {"412", HTTP_RESP_FLG_TRANSFER | HTTP_RESP_FLG_DATA_COND},
  {"413", HTTP_RESP_FLG_TRANSFER | HTTP_RESP_FLG_DATA_COND},
  {"414", HTTP_RESP_FLG_TRANSFER | HTTP_RESP_FLG_DATA_COND},
  {"415", HTTP_RESP_FLG_TRANSFER | HTTP_RESP_FLG_DATA_COND},

  {"500", HTTP_RESP_FLG_TRANSFER | HTTP_RESP_FLG_DATA_COND},
  {"501", HTTP_RESP_FLG_TRANSFER | HTTP_RESP_FLG_DATA_COND},
  {"502", HTTP_RESP_FLG_TRANSFER | HTTP_RESP_FLG_DATA_COND},
  {"503", HTTP_RESP_FLG_TRANSFER | HTTP_RESP_FLG_DATA_COND},
  {"504", HTTP_RESP_FLG_TRANSFER | HTTP_RESP_FLG_DATA_COND},
  {"505", HTTP_RESP_FLG_TRANSFER | HTTP_RESP_FLG_DATA}
};

int
http_proto_request_lookup(char *req)
{
  int x;
  for (x = 0; x < (sizeof(request_table) / sizeof(HttpRequest)); x++)
    {
      if (strcasecmp(req, request_table[x].name) == 0)
	return (request_table[x].val);
    }
  return (-1);
}

int
http_proto_response_lookup(char *resp)
{
  int x;
  for (x = 0; x < (sizeof(response_table) / sizeof(HttpResponse)); x++)
    {
      if (strcasecmp(resp, response_table[x].name) == 0)
	return (response_table[x].val);
    }
  return (-1);
}
