Fixes to HttpProcessor.

This commit is contained in:
2019-05-04 11:01:03 +02:00
parent 37025d7c07
commit a6c0b7c8a3

View File

@@ -85,6 +85,7 @@ namespace VAR.HttpServer
catch (Exception) catch (Exception)
{ {
ResponseServerError(); ResponseServerError();
throw;
} }
_outputStream.Flush(); _outputStream.Flush();
_inputStream = null; _inputStream = null;
@@ -149,7 +150,7 @@ namespace VAR.HttpServer
int separator = line.IndexOf(':'); int separator = line.IndexOf(':');
if (separator == -1) if (separator == -1)
{ {
throw new Exception("invalid http header line: " + line); throw new Exception(string.Format("Invalid HTTP header line: {0}", line));
} }
string name = line.Substring(0, separator); string name = line.Substring(0, separator);
int pos = separator + 1; int pos = separator + 1;
@@ -167,22 +168,20 @@ namespace VAR.HttpServer
private void ReadPostData() private void ReadPostData()
{ {
int content_len = 0; int contentLen = 0;
MemoryStream ms = new MemoryStream(); MemoryStream ms = new MemoryStream();
if (this._httpHeaders.ContainsKey("Content-Length")) if (_httpHeaders.ContainsKey("Content-Length"))
{ {
content_len = Convert.ToInt32(this._httpHeaders["Content-Length"]); contentLen = Convert.ToInt32(_httpHeaders["Content-Length"]);
if (content_len > MaxPostSize) if (contentLen > MaxPostSize)
{ {
throw new Exception( throw new Exception(string.Format("POST Content-Length({0}) > MaxPostSize({1})", contentLen, MaxPostSize));
string.Format("POST Content-Length({0}) too big for this simple server",
content_len));
} }
byte[] buf = new byte[BufferSize]; byte[] buf = new byte[BufferSize];
int to_read = content_len; int to_read = contentLen;
while (to_read > 0) while (to_read > 0)
{ {
int numread = this._inputStream.Read(buf, 0, Math.Min(BufferSize, to_read)); int numread = _inputStream.Read(buf, 0, Math.Min(BufferSize, to_read));
if (numread == 0) if (numread == 0)
{ {
if (to_read == 0) if (to_read == 0)
@@ -191,7 +190,7 @@ namespace VAR.HttpServer
} }
else else
{ {
throw new Exception("client disconnected during post"); throw new Exception("Client disconnected during POST");
} }
} }
to_read -= numread; to_read -= numread;
@@ -237,7 +236,6 @@ namespace VAR.HttpServer
_outputStream.WriteLine(string.Format("Content-Type: {0}", contentType)); _outputStream.WriteLine(string.Format("Content-Type: {0}", contentType));
_outputStream.WriteLine("Connection: close"); _outputStream.WriteLine("Connection: close");
_outputStream.WriteLine(""); _outputStream.WriteLine("");
_outputStream.Flush();
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -252,7 +250,6 @@ namespace VAR.HttpServer
_outputStream.WriteLine("HTTP/1.0 500 Internal server error"); _outputStream.WriteLine("HTTP/1.0 500 Internal server error");
_outputStream.WriteLine("Connection: close"); _outputStream.WriteLine("Connection: close");
_outputStream.WriteLine(""); _outputStream.WriteLine("");
_outputStream.Flush();
} }
catch (Exception ex) catch (Exception ex)
{ {