[Modify] Polish it

This commit is contained in:
sta 2017-12-29 22:00:57 +09:00
parent 3651d12408
commit 86a473caef

View File

@ -528,35 +528,44 @@ namespace WebSocketSharp.Net
internal void FinishInitialization () internal void FinishInitialization ()
{ {
var host = _headers["Host"]; var host = _headers["Host"];
var nohost = host == null || host.Length == 0; var hasHost = host != null && host.Length > 0;
if (_version > HttpVersion.Version10 && nohost) { if (_version > HttpVersion.Version10 && !hasHost) {
_context.ErrorMessage = "Invalid Host header"; _context.ErrorMessage = "Invalid Host header";
return; return;
} }
if (nohost) _url = HttpUtility.CreateRequestUrl (
host = UserHostAddress; _uri,
hasHost ? host : UserHostAddress,
IsWebSocketRequest,
IsSecureConnection
);
_url = HttpUtility.CreateRequestUrl (_uri, host, IsWebSocketRequest, IsSecureConnection);
if (_url == null) { if (_url == null) {
_context.ErrorMessage = "Invalid request url"; _context.ErrorMessage = "Invalid request url";
return; return;
} }
var enc = Headers["Transfer-Encoding"]; var transferEnc = _headers["Transfer-Encoding"];
if (_version > HttpVersion.Version10 && enc != null && enc.Length > 0) { if (transferEnc != null) {
_chunked = enc.ToLower () == "chunked"; if (_version < HttpVersion.Version11) {
if (!_chunked) { _context.ErrorMessage = "Invalid Transfer-Encoding header";
return;
}
var comparison = StringComparison.OrdinalIgnoreCase;
if (!transferEnc.Equals ("chunked", comparison)) {
_context.ErrorMessage = String.Empty; _context.ErrorMessage = String.Empty;
_context.ErrorStatus = 501; _context.ErrorStatus = 501;
return; return;
} }
_chunked = true;
} }
if (!_chunked && !_contentLengthSet) { if (!_chunked && !_contentLengthSet) {
var method = _method.ToLower (); if (_method == "POST" || _method == "PUT") {
if (method == "post" || method == "put") {
_context.ErrorMessage = String.Empty; _context.ErrorMessage = String.Empty;
_context.ErrorStatus = 411; _context.ErrorStatus = 411;
@ -564,8 +573,14 @@ namespace WebSocketSharp.Net
} }
} }
var expect = Headers["Expect"]; var expect = _headers["Expect"];
if (expect != null && expect.Length > 0 && expect.ToLower () == "100-continue") { if (_version > HttpVersion.Version10 && expect != null) {
var comparison = StringComparison.OrdinalIgnoreCase;
if (!expect.Equals ("100-continue", comparison)) {
_context.ErrorMessage = "Invalid Expect header";
return;
}
var output = _context.Connection.GetResponseStream (); var output = _context.Connection.GetResponseStream ();
output.InternalWrite (_100continue, 0, _100continue.Length); output.InternalWrite (_100continue, 0, _100continue.Length);
} }