aboutsummaryrefslogtreecommitdiff
path: root/src/stable/protocolhttp.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/stable/protocolhttp.h')
-rw-r--r--src/stable/protocolhttp.h106
1 files changed, 106 insertions, 0 deletions
diff --git a/src/stable/protocolhttp.h b/src/stable/protocolhttp.h
new file mode 100644
index 0000000..153a00d
--- /dev/null
+++ b/src/stable/protocolhttp.h
@@ -0,0 +1,106 @@
1/*
2 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#ifndef BU_PROTOCOL_HTTP_H
9#define BU_PROTOCOL_HTTP_H
10
11#include <stdint.h>
12#include <sys/types.h>
13
14#include "bu/protocol.h"
15#include "bu/client.h"
16#include "bu/string.h"
17#include "bu/hash.h"
18
19namespace Bu
20{
21 /**
22 * An HTTP Protocol handler. Yes, I know that HTTP stands for Hyper Text
23 * Transfer Protocol, and that the Protocol part is redundant, but in this
24 * case the word Protocol is refering to the Libbu++ construct Bu::Protocol,
25 * and not a means of encoding conversations. Anyway, this class represents
26 * a general HTTP server processor. Every time a request comes in it calls
27 * the onRequest function in a subclass with the method and URI that were
28 * requested. The sub-class can then do whatever it needs to to send back
29 * a response.
30 *@ingroup Serving
31 */
32 class ProtocolHttp : public Protocol
33 {
34 public: /* Types */
35 typedef Bu::List<Bu::String> TokenList;
36
37 public: /* Interface */
38 ProtocolHttp();
39 virtual ~ProtocolHttp();
40
41 virtual void onNewConnection( Bu::Client *pClient );
42 virtual void onNewData( Bu::Client *pClient );
43
44 virtual void onRequest(
45 const Bu::String &sMethod, const Bu::String &sPath )=0;
46
47 class Response
48 {
49 friend class Bu::ProtocolHttp;
50 public:
51 Response( int iCode );
52 Response( int iCode, const Bu::String &sReason );
53 virtual ~Response();
54
55 void setHeader( const Bu::String &sKey, const Bu::String &sVal );
56 void setContent( const Bu::String &sCont );
57
58 private:
59 int iCode;
60 Bu::String sReason;
61 typedef Bu::Hash<Bu::String,Bu::String> StringHash;
62 StringHash hHeaders;
63 Bu::String sContent;
64 };
65
66 void sendResponse( const Response &rRes );
67
68 private:
69 enum TokenType
70 {
71 ttOutOfData,
72 ttString,
73 ttNewline,
74 ttDoubleNewline,
75 ttSeperator
76 };
77 /**
78 * Read an HTTP line, this is up to the first CRLF that isn't followed
79 * by a continuation character, converting it to one line as it reads.
80 *@param line All data read will be appended to line, even if no
81 * end-of-line is read.
82 *@returns True if an end-of-line is read and the line should be
83 * processed, false if the end-of-line has not been reached, and more
84 * data needs to be read before this operation can continue.
85 */
86 TokenType getToken( Bu::String &line );
87 bool isWS( char buf );
88 bool isSeperator( char buf );
89
90 void earlyResponse();
91 void lateResponse();
92
93 private: /* state */
94 Bu::Client *pClient;
95 TokenList lTokens;
96
97 int iState;
98
99 Bu::String sMethod;
100 Bu::String sPath;
101 int iMajor;
102 int iMinor;
103 };
104}
105
106#endif