summaryrefslogtreecommitdiffstats
path: root/src/ogClient.py
blob: 3f7a752661830a348ecc82b2f547d5f7930f2065 (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
import errno
import select
import socket
import time

from HTTPParser import *
from ogProcess import *
from enum import Enum

class State(Enum):
	CONNECTING = 0
	RECEIVING = 1

class ogClient:
	def __init__(self, ip, port):
		self.ip = ip
		self.port = port

	def get_socket(self):
		return self.sock

	def get_state(self):
		return self.state

	def connect(self):
		print "connecting"
		self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		self.sock.setblocking(0)
		self.state = State.CONNECTING
		self.data = ""
		self.trailer = False
		self.content_len = 0

		try:
			self.sock.connect((self.ip, self.port))
		except socket.error, err:
			if err.errno == errno.EINPROGRESS:
				return
			elif err.errno == errno.ECONNREFUSED:
				return

			print "Error connect " + str(err)

	def connect2(self):
		try:
			self.sock.connect((self.ip, self.port))
		except socket.error, err:
			if err.errno == errno.EISCONN:
				print "connected"
				self.state = State.RECEIVING
			else:
				print "connection refused, retrying..."
				self.state = State.CONNECTING
				self.sock.close()
				self.connect()

	def receive(self):
		print "receiving"
		try:
			data = self.sock.recv(1024)
		except socket.err, err:
			print "Error3 " + str(err)

		if len(data) == 0:
			self.state = State.CONNECTING
			self.sock.close()
			self.connect()

		self.data = self.data + data
		httpparser = HTTPParser()
		ogprocess = ogProcess()

		if not self.trailer:
			if self.data.find("\r\n") > 0:
				# https://stackoverflow.com/questions/4685217/parse-raw-http-headers
				request_line, headers_alone = self.data.split('\n', 1)
				headers = Message(StringIO(headers_alone))

				if 'content-length' in headers.keys():
					self.content_len = int(headers['content-length'])

				self.trailer = True

		if self.trailer and len(self.data) >= self.content_len:
			httpparser.parser(self.data)
			print httpparser.getRequestOP()
			print httpparser.getURI()
			if not ogprocess.processOperation(httpparser.getRequestOP(), httpparser.getURI()):
				self.sock.send("HTTP/1.0 400 Bad request\r\n\r\n")
			else:
				self.sock.send("HTTP/1.0 200 OK\r\n\r\n")

			# Cleanup state information from request
			self.data = ""
			self.content_len = 0
			self.trailer = False

	def run(self):
		while 1:
			sock = self.get_socket()
			state = self.get_state()

			if state == State.CONNECTING:
				readset = [ sock ]
				writeset = [ sock ]
			else:
				readset = [ sock ]
				writeset = [ ]

			readable, writable, exception = select.select(readset, writeset, [ ])
			if state == State.CONNECTING and sock in writable:
				self.connect2()
			elif state == State.RECEIVING and sock in readable:
				self.receive()
			else:
				print "bad state" + str(state)