aboutsummaryrefslogtreecommitdiff
path: root/src/protocolhttp.cpp
blob: 6f2ae680e4b35c63355c36588b59c564e6096639 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <dirent.h>
#include <sys/wait.h>
#include <errno.h>
#include "protocolhttp.h"

#define CRLF "\x0D\x0A"
#define CR '\x0D'
#define LF '\x0A'

using namespace Bu;

Bu::ProtocolHttp::ProtocolHttp()
{
}

Bu::ProtocolHttp::~ProtocolHttp()
{
}

void Bu::ProtocolHttp::onNewConnection( Bu::Client *pClient )
{
	this->pClient = pClient;

	iState = 0;
}

#define SDB( i ) printf("state %d: %d, \"%s\"\n", i, tt, sToken.getStr() )

void Bu::ProtocolHttp::onNewData( Bu::Client *pClient )
{
	for(;;)
	{
		Bu::FString sToken;
		TokenType tt = getToken( sToken );

		if( tt == ttOutOfData )
			return;

		switch( iState )
		{
			case 0:		// Initial header
				SDB( 0 );
				sMethod = sToken;
				iState = 1;
				break;

			case 1:		// Misc headers
				SDB( 1 );
				sPath = sToken;
				iState = 2;
				break;

			case 2:		// Content
				SDB( 2 );
				if( strncmp( sToken.getStr(), "HTTP/", 5 ) )
				{
					printf("not http, disconnect.\n");
					pClient->disconnect();
					return;
				}
				else
				{
					char *s, *s2;
					s = sToken.getStr()+5;
					iMajor = strtol( s, &s2, 10 );
					iMinor = strtol( s2+1, NULL, 10 );
					printf("HTTP: %d.%d\n", iMajor, iMinor );
					iState = 3;
				}
				break;

			case 3:
				SDB( 3 );
				pClient->disconnect();
				return;
				break;
		}
	}
}

Bu::ProtocolHttp::TokenType Bu::ProtocolHttp::getToken( Bu::FString &line )
{
	char s;
	int jmax = pClient->getInputSize();
	bool bNonWS = false;
	
	for( int j = 0; j < jmax; j++ )
	{
		pClient->peek( &s, 1, j );
		if( iState > 2 && isSeperator( s ) )
		{
			if( j == 0 )
			{
				line += s;
				pClient->seek( 1 );
				return ttSeperator;
			}
			else
			{
				pClient->seek( j );
				return ttString;
			}
		}
		else if( isWS( s ) )
		{
			if( bNonWS )
			{
				pClient->seek( j );
				return ttString;
			}
		}
		else if( s == CR )
		{
			if( pClient->getInputSize() < 3 )
				return ttOutOfData;
			
			char ss[2];
			pClient->peek( ss, 2, j+1 );
			if( ss[0] == LF && ss[1] != ' ' && ss[1] != '\t' )
			{
				if( bNonWS )
				{
					pClient->seek( j );
					return ttString;
				}
				else
				{
					pClient->seek( 2 );
					return ttNewline;
				}
			}
			
			j += 2;
			if( bNonWS )
			{
				pClient->seek( j );
				return ttString;
			}
		}
		else
		{
			line += s;
			bNonWS = true;
		}
	}

	return ttOutOfData;
}

bool Bu::ProtocolHttp::isWS( char buf )
{
	return (buf == ' ' || buf == '\t');
}

bool Bu::ProtocolHttp::isSeperator( char buf )
{
	return (buf == '(' || buf == ')' || buf == '<' || buf == '>' ||
			buf == '@' || buf == ',' || buf == ';' || buf == ':' ||
			buf == '\\' || buf == '\"' || buf == '/' || buf == '[' ||
			buf == ']' || buf == '?' || buf == '=' || buf == '{' ||
			buf == '}' );
}