Panda3D
 All Classes Functions Variables Enumerations
http_connection.cxx
1 #include "http_connection.h"
2 
3 TypeHandle HttpConnection::_type_handle;
4 
5 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
6 HttpConnection::HttpConnection(SOCKET sck,Socket_Address &inaddr) :
7  _Timer(Time_Span(10,0)) ,
8  _MyAddress(inaddr),
9  _state(READING_HEADER)
10 {
11  SetSocket(sck);
12  SetNonBlocking();
13  SetReuseAddress();
14 
15  _writer.reserve(102400);
16 }
17 
18 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
19 HttpConnection::~HttpConnection(void)
20 {
21 }
22 
23 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
24 const Socket_Address & HttpConnection::GetMyAddress(void)
25 {
26  return _MyAddress;
27 };
28 
29 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
30 int HttpConnection::DoReadHeader(char * message, int buffersize,Time_Clock &currentTime)
31 {
32  int ans = _Reader.PumpHTTPHeaderRead(message,buffersize,*this);
33 
34  if(ans != 0)
35  {
36  if(ans > 0)
37  _headerDetail.assign(message,buffersize);
38 
39  return ans;
40  }
41 
42  if(_Timer.Expired(currentTime) == true)
43  {
44  return -1;
45  }
46 
47  return 0;
48 }
49 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
50 int HttpConnection::DoReadBody(char * message1, int buffersize,Time_Clock &currentTime)
51 {
52  int ans = _Reader.PumpSizeRead(_bodyDetail,*this);
53 
54  if(ans != 0)
55  {
56 
57  return ans;
58  }
59 
60  if(_Timer.Expired(currentTime) == true)
61  {
62  return -1;
63  }
64 
65  // ok lets process this thing..
66  _state = WAITING_TO_FINALIZE;
67 
68  return 0;
69 }
70 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
71 int HttpConnection::ReadIt(char * message, int buffersize,Time_Clock &currentTime)
72 {
73  switch (_state)
74  {
75  case(READING_HEADER):
76  return DoReadHeader(message, buffersize,currentTime);
77  break;
78 
79  case(READING_POST):
80  return DoReadBody(message, buffersize,currentTime);
81  break;
82 
83  case(WAITING_TO_FINALIZE):
84  return TryAndFinalize();
85  break;
86 
87  case(WRITING_DATA):
88  return CloseStateWriter(currentTime);
89  break;
90  default:
91  break;
92 
93  };
94  return ConnectionDoClose;
95 }
96 
97 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
98 CloseState HttpConnection::ProcessMessage(char * message,Time_Clock &currentTime)
99 {
100  if(_state == READING_POST )
101  {
102  _state = WAITING_TO_FINALIZE;
103  return ConnectionDoClose;
104  }
105 
106 
107  if(_parser.ParseThis(message) != true)
108  {
109  Reset();
110  return ConnectionDoClose;
111  }
112  // if it is a post go into read details mode and
113  // wait to get the post data..
114  // we do not support any other methoid today
115  if(_parser.GetRequestType() == "POST")
116  {
117  int context_length = _parser.getContentLength();
118  if(context_length > 0)
119  {
120  //_DoingExtraRead = true;
121  _state = READING_POST;
122  _bodyDetail.SetDataSize(context_length);
123  return ConnectionDoNotClose;
124  }
125  }
126 
127  _state = WAITING_TO_FINALIZE;
128 
129  _parser.SetBodyText(_bodyDetail);
130  _Timer.ResetTime(currentTime);
131 
132  if(BuildPage(_writer,_parser) != true)
133  return ConnectionDoClose;
134 
135  if(_state == WRITING_DATA)
136  {
137  if(CloseStateWriter(currentTime) <0)
138  return ConnectionDoClose;
139  }
140 
141  return ConnectionDoNotClose;
142 }
143 
144 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
145 int HttpConnection::CloseStateWriter(Time_Clock &currentTime)
146 {
147  int fans = _writer.Flush(*this); // write error
148  if(fans < 0)
149  return -1;
150 
151  if(_writer.AmountBuffered() <= 0) // all done
152  return -1;
153 
154  if(_Timer.Expired(currentTime) == true) // too long
155  return -1;
156 
157  return 0; // keep trying
158 }
159 
160 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
161 void HttpConnection::Reset()
162 {
163  _state = ABORTING;
164  Close();
165  _Timer.ForceToExpired();
166 }
167 
168 
bool Expired(const Time_Clock &tm, bool reset=false)
Return type : bool Argument : const Time_Clock &amp;tm.
Definition: time_out.h:120
int AmountBuffered(void)
Return type : inline int Argument : void.
A simple place to store and munipulate tcp and port address for communication layer.
int Flush(Socket_TCP &sck)
Return type : inline int Argument : SocketTCP_Gm &amp;sck.
void ResetTime(const Time_Clock &tm)
Return type : void Argument : const Time_Clock &amp; tm.
Definition: time_out.h:109
void Close()
closes a socket if it is open (allocated)
Definition: socket_ip.h:141
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85