summaryrefslogtreecommitdiff
path: root/src/protocolhttp.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2007-10-04 10:46:46 +0000
committerMike Buland <eichlan@xagasoft.com>2007-10-04 10:46:46 +0000
commit86f9fbefa58d91e151190c969216c751573bc664 (patch)
tree4e13de2666bfb063964877986dc7b8d310714483 /src/protocolhttp.cpp
parentb3eef5b0b82c20a9f11868ba376f6bb2d94faae4 (diff)
downloadlibbu++-86f9fbefa58d91e151190c969216c751573bc664.tar.gz
libbu++-86f9fbefa58d91e151190c969216c751573bc664.tar.bz2
libbu++-86f9fbefa58d91e151190c969216c751573bc664.tar.xz
libbu++-86f9fbefa58d91e151190c969216c751573bc664.zip
Discovered that the Bu::Client::disconnect() function didn't do anything. That
has been fixed, it now safely disconnects after emptying the Client's outgoing buffer. Added some more helpers to Bu::FString. Added the beginings of ProtocolHttp using a new method for processing protocols that's based more strongly on an NFA state machine, this makes sense, but I never had the desire to actually try implementing it before. It's working pretty well.
Diffstat (limited to 'src/protocolhttp.cpp')
-rw-r--r--src/protocolhttp.cpp163
1 files changed, 163 insertions, 0 deletions
diff --git a/src/protocolhttp.cpp b/src/protocolhttp.cpp
new file mode 100644
index 0000000..6f2ae68
--- /dev/null
+++ b/src/protocolhttp.cpp
@@ -0,0 +1,163 @@
1#include <dirent.h>
2#include <sys/wait.h>
3#include <errno.h>
4#include "protocolhttp.h"
5
6#define CRLF "\x0D\x0A"
7#define CR '\x0D'
8#define LF '\x0A'
9
10using namespace Bu;
11
12Bu::ProtocolHttp::ProtocolHttp()
13{
14}
15
16Bu::ProtocolHttp::~ProtocolHttp()
17{
18}
19
20void Bu::ProtocolHttp::onNewConnection( Bu::Client *pClient )
21{
22 this->pClient = pClient;
23
24 iState = 0;
25}
26
27#define SDB( i ) printf("state %d: %d, \"%s\"\n", i, tt, sToken.getStr() )
28
29void Bu::ProtocolHttp::onNewData( Bu::Client *pClient )
30{
31 for(;;)
32 {
33 Bu::FString sToken;
34 TokenType tt = getToken( sToken );
35
36 if( tt == ttOutOfData )
37 return;
38
39 switch( iState )
40 {
41 case 0: // Initial header
42 SDB( 0 );
43 sMethod = sToken;
44 iState = 1;
45 break;
46
47 case 1: // Misc headers
48 SDB( 1 );
49 sPath = sToken;
50 iState = 2;
51 break;
52
53 case 2: // Content
54 SDB( 2 );
55 if( strncmp( sToken.getStr(), "HTTP/", 5 ) )
56 {
57 printf("not http, disconnect.\n");
58 pClient->disconnect();
59 return;
60 }
61 else
62 {
63 char *s, *s2;
64 s = sToken.getStr()+5;
65 iMajor = strtol( s, &s2, 10 );
66 iMinor = strtol( s2+1, NULL, 10 );
67 printf("HTTP: %d.%d\n", iMajor, iMinor );
68 iState = 3;
69 }
70 break;
71
72 case 3:
73 SDB( 3 );
74 pClient->disconnect();
75 return;
76 break;
77 }
78 }
79}
80
81Bu::ProtocolHttp::TokenType Bu::ProtocolHttp::getToken( Bu::FString &line )
82{
83 char s;
84 int jmax = pClient->getInputSize();
85 bool bNonWS = false;
86
87 for( int j = 0; j < jmax; j++ )
88 {
89 pClient->peek( &s, 1, j );
90 if( iState > 2 && isSeperator( s ) )
91 {
92 if( j == 0 )
93 {
94 line += s;
95 pClient->seek( 1 );
96 return ttSeperator;
97 }
98 else
99 {
100 pClient->seek( j );
101 return ttString;
102 }
103 }
104 else if( isWS( s ) )
105 {
106 if( bNonWS )
107 {
108 pClient->seek( j );
109 return ttString;
110 }
111 }
112 else if( s == CR )
113 {
114 if( pClient->getInputSize() < 3 )
115 return ttOutOfData;
116
117 char ss[2];
118 pClient->peek( ss, 2, j+1 );
119 if( ss[0] == LF && ss[1] != ' ' && ss[1] != '\t' )
120 {
121 if( bNonWS )
122 {
123 pClient->seek( j );
124 return ttString;
125 }
126 else
127 {
128 pClient->seek( 2 );
129 return ttNewline;
130 }
131 }
132
133 j += 2;
134 if( bNonWS )
135 {
136 pClient->seek( j );
137 return ttString;
138 }
139 }
140 else
141 {
142 line += s;
143 bNonWS = true;
144 }
145 }
146
147 return ttOutOfData;
148}
149
150bool Bu::ProtocolHttp::isWS( char buf )
151{
152 return (buf == ' ' || buf == '\t');
153}
154
155bool Bu::ProtocolHttp::isSeperator( char buf )
156{
157 return (buf == '(' || buf == ')' || buf == '<' || buf == '>' ||
158 buf == '@' || buf == ',' || buf == ';' || buf == ':' ||
159 buf == '\\' || buf == '\"' || buf == '/' || buf == '[' ||
160 buf == ']' || buf == '?' || buf == '=' || buf == '{' ||
161 buf == '}' );
162}
163