aboutsummaryrefslogtreecommitdiff
path: root/src/network/httprequesthandler/IHTTPRequestHandler.h
blob: b1cde577ada549843ec2b14c52188f23052d819c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#pragma once
/*
 *      Copyright (C) 2011-2013 Team XBMC
 *      http://xbmc.org
 *
 *  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, 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 XBMC; see the file COPYING.  If not, see
 *  <http://www.gnu.org/licenses/>.
 *
 */

#include <string>
#include <map>

#include <sys/types.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <microhttpd.h>

class CWebServer;

enum HTTPMethod
{
  UNKNOWN,
  POST,
  GET,
  HEAD
};

enum HTTPResponseType
{
  HTTPNone,
  HTTPError,
  HTTPRedirect,
  HTTPFileDownload,
  HTTPMemoryDownloadNoFreeNoCopy,
  HTTPMemoryDownloadNoFreeCopy,
  HTTPMemoryDownloadFreeNoCopy,
  HTTPMemoryDownloadFreeCopy
};

typedef struct HTTPRequest
{
  struct MHD_Connection *connection;
  std::string url;
  HTTPMethod method;
  std::string version;
  CWebServer *webserver;
} HTTPRequest;

class IHTTPRequestHandler
{
public:
  virtual ~IHTTPRequestHandler() { }

  virtual IHTTPRequestHandler* GetInstance() = 0;
  virtual bool CheckHTTPRequest(const HTTPRequest &request) = 0;
  virtual int HandleHTTPRequest(const HTTPRequest &request) = 0;
  
  virtual void* GetHTTPResponseData() const { return NULL; };
  virtual size_t GetHTTPResonseDataLength() const { return 0; }
  virtual std::string GetHTTPRedirectUrl() const { return ""; }
  virtual std::string GetHTTPResponseFile() const { return ""; }

  // The higher the more important
  virtual int GetPriority() const { return 0; }

  void AddPostField(const std::string &key, const std::string &value);
#if (MHD_VERSION >= 0x00040001)
  bool AddPostData(const char *data, size_t size);
#else
  bool AddPostData(const char *data, unsigned int size);
#endif

  int GetHTTPResonseCode() const { return m_responseCode; }
  HTTPResponseType GetHTTPResponseType() const { return m_responseType; }
  const std::multimap<std::string, std::string>& GetHTTPResponseHeaderFields() const { return m_responseHeaderFields; };

protected:
#if (MHD_VERSION >= 0x00040001)
  virtual bool appendPostData(const char *data, size_t size)
#else
  virtual bool appendPostData(const char *data, unsigned int size)
#endif
  { return true; }

  int m_responseCode;
  HTTPResponseType m_responseType;
  std::multimap<std::string, std::string> m_responseHeaderFields;

  std::map<std::string, std::string> m_postFields;
};