cosmetics on http server
parent
e0aff5e640
commit
d9a680703f
|
@ -364,20 +364,18 @@ namespace OSHttpServer
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
m_ReceiveBytesLeft += bytesRead;
|
m_ReceiveBytesLeft += bytesRead;
|
||||||
if (m_ReceiveBytesLeft > m_ReceiveBuffer.Length)
|
|
||||||
throw new BadRequestException("HTTP header Too large: " + m_ReceiveBytesLeft);
|
|
||||||
|
|
||||||
int offset = m_parser.Parse(m_ReceiveBuffer, 0, m_ReceiveBytesLeft);
|
int offset = m_parser.Parse(m_ReceiveBuffer, 0, m_ReceiveBytesLeft);
|
||||||
if (m_stream == null)
|
if (m_stream == null)
|
||||||
return; // "Connection: Close" in effect.
|
return; // "Connection: Close" in effect.
|
||||||
|
|
||||||
// try again to see if we can parse another message (check parser to see if it is looking for a new message)
|
while (offset != 0)
|
||||||
int nextOffset;
|
|
||||||
int nextBytesleft = m_ReceiveBytesLeft - offset;
|
|
||||||
|
|
||||||
while (offset != 0 && nextBytesleft > 0)
|
|
||||||
{
|
{
|
||||||
nextOffset = m_parser.Parse(m_ReceiveBuffer, offset, nextBytesleft);
|
int nextBytesleft = m_ReceiveBytesLeft - offset;
|
||||||
|
if(nextBytesleft <= 0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
int nextOffset = m_parser.Parse(m_ReceiveBuffer, offset, nextBytesleft);
|
||||||
|
|
||||||
if (m_stream == null)
|
if (m_stream == null)
|
||||||
return; // "Connection: Close" in effect.
|
return; // "Connection: Close" in effect.
|
||||||
|
@ -386,7 +384,6 @@ namespace OSHttpServer
|
||||||
break;
|
break;
|
||||||
|
|
||||||
offset = nextOffset;
|
offset = nextOffset;
|
||||||
nextBytesleft = m_ReceiveBytesLeft - offset;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// copy unused bytes to the beginning of the array
|
// copy unused bytes to the beginning of the array
|
||||||
|
|
|
@ -224,21 +224,24 @@ namespace OSHttpServer.Parser
|
||||||
m_log.Write(this, LogPrio.Warning, "HTTP Request is too large.");
|
m_log.Write(this, LogPrio.Warning, "HTTP Request is too large.");
|
||||||
throw new BadRequestException("Too large request line.");
|
throw new BadRequestException("Too large request line.");
|
||||||
}
|
}
|
||||||
if (char.IsLetterOrDigit(ch) && startPos == -1)
|
if (startPos == -1)
|
||||||
startPos = currentPos;
|
|
||||||
if (startPos == -1 && (ch != '\r' || nextCh != '\n'))
|
|
||||||
{
|
{
|
||||||
m_log.Write(this, LogPrio.Warning, "Request line is not found.");
|
if(char.IsLetterOrDigit(ch))
|
||||||
throw new BadRequestException("Invalid request line.");
|
startPos = currentPos;
|
||||||
|
else if (ch != '\r' || nextCh != '\n')
|
||||||
|
{
|
||||||
|
m_log.Write(this, LogPrio.Warning, "Request line is not found.");
|
||||||
|
throw new BadRequestException("Invalid request line.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (startPos != -1 && (ch == '\r' || ch == '\n'))
|
else if(ch == '\r' || ch == '\n')
|
||||||
{
|
{
|
||||||
int size = GetLineBreakSize(buffer, currentPos);
|
int size = GetLineBreakSize(buffer, currentPos);
|
||||||
OnFirstLine(Encoding.UTF8.GetString(buffer, startPos, currentPos - startPos));
|
OnFirstLine(Encoding.UTF8.GetString(buffer, startPos, currentPos - startPos));
|
||||||
CurrentState = CurrentState + 1;
|
|
||||||
currentPos += size - 1;
|
currentPos += size - 1;
|
||||||
handledBytes = currentPos + size - 1;
|
handledBytes = currentPos;
|
||||||
startPos = -1;
|
startPos = -1;
|
||||||
|
CurrentState = RequestParserState.HeaderName;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case RequestParserState.HeaderName:
|
case RequestParserState.HeaderName:
|
||||||
|
@ -273,19 +276,20 @@ namespace OSHttpServer.Parser
|
||||||
}
|
}
|
||||||
m_curHeaderName = Encoding.UTF8.GetString(buffer, startPos, currentPos - startPos);
|
m_curHeaderName = Encoding.UTF8.GetString(buffer, startPos, currentPos - startPos);
|
||||||
handledBytes = currentPos + 1;
|
handledBytes = currentPos + 1;
|
||||||
startPos = -1;
|
startPos = handledBytes;
|
||||||
CurrentState = CurrentState + 1;
|
|
||||||
if (ch == ':')
|
if (ch == ':')
|
||||||
CurrentState = CurrentState + 1;
|
CurrentState = RequestParserState.Between;
|
||||||
|
else
|
||||||
|
CurrentState = RequestParserState.AfterName;
|
||||||
}
|
}
|
||||||
else if (startPos == -1)
|
|
||||||
startPos = currentPos;
|
|
||||||
else if (!char.IsLetterOrDigit(ch) && ch != '-')
|
else if (!char.IsLetterOrDigit(ch) && ch != '-')
|
||||||
{
|
{
|
||||||
m_log.Write(this, LogPrio.Warning, "Invalid character in header name on line " + currentLine);
|
m_log.Write(this, LogPrio.Warning, "Invalid character in header name on line " + currentLine);
|
||||||
throw new BadRequestException("Invalid character in header name on line " + currentLine);
|
throw new BadRequestException("Invalid character in header name on line " + currentLine);
|
||||||
}
|
}
|
||||||
if (startPos != -1 && currentPos - startPos > 200)
|
if (startPos == -1)
|
||||||
|
startPos = currentPos;
|
||||||
|
else if (currentPos - startPos > 200)
|
||||||
{
|
{
|
||||||
m_log.Write(this, LogPrio.Warning, "Invalid header name on line " + currentLine);
|
m_log.Write(this, LogPrio.Warning, "Invalid header name on line " + currentLine);
|
||||||
throw new BadRequestException("Invalid header name on line " + currentLine);
|
throw new BadRequestException("Invalid header name on line " + currentLine);
|
||||||
|
@ -295,85 +299,103 @@ namespace OSHttpServer.Parser
|
||||||
if (ch == ':')
|
if (ch == ':')
|
||||||
{
|
{
|
||||||
handledBytes = currentPos + 1;
|
handledBytes = currentPos + 1;
|
||||||
CurrentState = CurrentState + 1;
|
startPos = currentPos;
|
||||||
|
CurrentState = RequestParserState.Between;
|
||||||
|
}
|
||||||
|
else if(currentPos - startPos > 256)
|
||||||
|
{
|
||||||
|
m_log.Write(this, LogPrio.Warning, "missing header aftername ':' " + currentLine);
|
||||||
|
throw new BadRequestException("missing header aftername ':' " + currentLine);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case RequestParserState.Between:
|
case RequestParserState.Between:
|
||||||
{
|
{
|
||||||
if (ch == ' ' || ch == '\t')
|
if (ch == ' ' || ch == '\t')
|
||||||
continue;
|
|
||||||
int newLineSize = GetLineBreakSize(buffer, currentPos);
|
|
||||||
if (newLineSize > 0 && currentPos + newLineSize < endOfBufferPos &&
|
|
||||||
char.IsWhiteSpace((char)buffer[currentPos + newLineSize]))
|
|
||||||
{
|
{
|
||||||
++currentPos;
|
if (currentPos - startPos > 256)
|
||||||
continue;
|
{
|
||||||
|
m_log.Write(this, LogPrio.Warning, "header value too far" + currentLine);
|
||||||
|
throw new BadRequestException("header value too far" + currentLine);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
startPos = currentPos;
|
else
|
||||||
CurrentState = CurrentState + 1;
|
{
|
||||||
handledBytes = currentPos;
|
int newLineSize = GetLineBreakSize(buffer, currentPos);
|
||||||
continue;
|
if (newLineSize > 0 && currentPos + newLineSize < endOfBufferPos &&
|
||||||
|
char.IsWhiteSpace((char)buffer[currentPos + newLineSize]))
|
||||||
|
{
|
||||||
|
if (currentPos - startPos > 256)
|
||||||
|
{
|
||||||
|
m_log.Write(this, LogPrio.Warning, "header value too" + currentLine);
|
||||||
|
throw new BadRequestException("header value too far" + currentLine);
|
||||||
|
}
|
||||||
|
++currentPos;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
startPos = currentPos;
|
||||||
|
handledBytes = currentPos;
|
||||||
|
CurrentState = RequestParserState.HeaderValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
case RequestParserState.HeaderValue:
|
case RequestParserState.HeaderValue:
|
||||||
{
|
{
|
||||||
if (ch != '\r' && ch != '\n')
|
if (ch == '\r' || ch == '\n')
|
||||||
continue;
|
|
||||||
int newLineSize = GetLineBreakSize(buffer, currentPos);
|
|
||||||
if (startPos == -1)
|
|
||||||
continue; // allow new lines before start of value
|
|
||||||
|
|
||||||
if (m_curHeaderName == string.Empty)
|
|
||||||
throw new BadRequestException("Missing header on line " + currentLine);
|
|
||||||
if (startPos == -1)
|
|
||||||
{
|
{
|
||||||
m_log.Write(this, LogPrio.Warning, "Missing header value for '" + m_curHeaderName);
|
if (m_curHeaderName == string.Empty)
|
||||||
throw new BadRequestException("Missing header value for '" + m_curHeaderName);
|
throw new BadRequestException("Missing header on line " + currentLine);
|
||||||
}
|
|
||||||
if (currentPos - startPos > 8190)
|
if (currentPos - startPos > 8190)
|
||||||
{
|
{
|
||||||
m_log.Write(this, LogPrio.Warning, "Too large header value on line " + currentLine);
|
m_log.Write(this, LogPrio.Warning, "Too large header value on line " + currentLine);
|
||||||
throw new BadRequestException("Too large header value on line " + currentLine);
|
throw new BadRequestException("Too large header value on line " + currentLine);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Header fields can be extended over multiple lines by preceding each extra line with at
|
// Header fields can be extended over multiple lines by preceding each extra line with at
|
||||||
// least one SP or HT.
|
// least one SP or HT.
|
||||||
if (endOfBufferPos > currentPos + newLineSize
|
int newLineSize = GetLineBreakSize(buffer, currentPos);
|
||||||
&& (buffer[currentPos + newLineSize] == ' ' || buffer[currentPos + newLineSize] == '\t'))
|
if (endOfBufferPos > currentPos + newLineSize
|
||||||
{
|
&& (buffer[currentPos + newLineSize] == ' ' || buffer[currentPos + newLineSize] == '\t'))
|
||||||
if (startPos != -1)
|
{
|
||||||
m_curHeaderValue = Encoding.UTF8.GetString(buffer, startPos, currentPos - startPos);
|
if (startPos != -1)
|
||||||
|
m_curHeaderValue += Encoding.UTF8.GetString(buffer, startPos, currentPos - startPos);
|
||||||
|
|
||||||
m_log.Write(this, LogPrio.Trace, "Header value is on multiple lines.");
|
m_log.Write(this, LogPrio.Trace, "Header value is on multiple lines.");
|
||||||
CurrentState = RequestParserState.Between;
|
CurrentState = RequestParserState.Between;
|
||||||
startPos = -1;
|
currentPos += newLineSize - 1;
|
||||||
currentPos += newLineSize - 1;
|
startPos = currentPos;
|
||||||
handledBytes = currentPos + newLineSize - 1;
|
handledBytes = currentPos;
|
||||||
continue;
|
}
|
||||||
}
|
else
|
||||||
|
{
|
||||||
|
m_curHeaderValue += Encoding.UTF8.GetString(buffer, startPos, currentPos - startPos);
|
||||||
|
m_log.Write(this, LogPrio.Trace, "Header [" + m_curHeaderName + ": " + m_curHeaderValue + "]");
|
||||||
|
OnHeader(m_curHeaderName, m_curHeaderValue);
|
||||||
|
|
||||||
m_curHeaderValue += Encoding.UTF8.GetString(buffer, startPos, currentPos - startPos);
|
startPos = -1;
|
||||||
m_log.Write(this, LogPrio.Trace, "Header [" + m_curHeaderName + ": " + m_curHeaderValue + "]");
|
CurrentState = RequestParserState.HeaderName;
|
||||||
OnHeader(m_curHeaderName, m_curHeaderValue);
|
m_curHeaderValue = string.Empty;
|
||||||
|
m_curHeaderName = string.Empty;
|
||||||
|
currentPos += newLineSize - 1;
|
||||||
|
handledBytes = currentPos;
|
||||||
|
|
||||||
startPos = -1;
|
// Check if we got a colon so we can cut header name, or crlf for end of header.
|
||||||
CurrentState = RequestParserState.HeaderName;
|
bool canContinue = false;
|
||||||
m_curHeaderValue = string.Empty;
|
for (int j = currentPos; j < endOfBufferPos; ++j)
|
||||||
m_curHeaderName = string.Empty;
|
{
|
||||||
++currentPos;
|
if (buffer[j] != ':' && buffer[j] != '\r' && buffer[j] != '\n')
|
||||||
handledBytes = currentPos + 1;
|
continue;
|
||||||
|
canContinue = true;
|
||||||
// Check if we got a colon so we can cut header name, or crlf for end of header.
|
break;
|
||||||
bool canContinue = false;
|
}
|
||||||
for (int j = currentPos; j < endOfBufferPos; ++j)
|
if (!canContinue)
|
||||||
{
|
{
|
||||||
if (buffer[j] != ':' && buffer[j] != '\r' && buffer[j] != '\n') continue;
|
m_log.Write(this, LogPrio.Trace, "Cant continue, no colon.");
|
||||||
canContinue = true;
|
return currentPos + 1;
|
||||||
break;
|
}
|
||||||
}
|
}
|
||||||
if (!canContinue)
|
|
||||||
{
|
|
||||||
m_log.Write(this, LogPrio.Trace, "Cant continue, no colon.");
|
|
||||||
return currentPos + 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Reference in New Issue