本文整理汇总了C#中HttpMachine.HttpParser类的典型用法代码示例。如果您正苦于以下问题:C# HttpParser类的具体用法?C# HttpParser怎么用?C# HttpParser使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HttpParser类属于HttpMachine命名空间,在下文中一共展示了HttpParser类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HttpServerSocketDelegate
public HttpServerSocketDelegate(IHttpServerTransaction transaction, IHttpServerTransactionDelegate transactionDelegate)
{
this.transaction = transaction;
this.transactionDelegate = transactionDelegate;
transactionTransform = new ParserToTransactionTransform(transaction, transactionDelegate);
parser = new HttpParser(new ParserDelegate(transactionTransform));
}
示例2: OnBody
public void OnBody(HttpParser parser, ArraySegment<byte> data)
{
if (_headersComplete && data != null && data.Count > 0 && _onRequestBodyReceivedHandler != null)
{
_request.ReceivedLength += data.Count;
_onRequestBodyReceivedHandler(data);
}
}
示例3: OnHeaderValue
public void OnHeaderValue(HttpParser parser, string value)
{
string existingValue = _request.Headers[_headerName];
if (string.IsNullOrWhiteSpace(existingValue))
_request.Headers[_headerName] = value;
else
_request.Headers[_headerName] = existingValue + "," + value;
}
示例4: OnBody
public void OnBody(HttpParser parser, ArraySegment<byte> data)
{
Debug.WriteLine("OnBody");
// XXX can we defer this check to the parser?
if (data.Count > 0)
{
del.OnRequestBody(data);
}
}
示例5: HttpRequestParser
public HttpRequestParser(Action<HttpRequest> onRequestBeginHandler, Action<ArraySegment<byte>> onRequestBodyReceivedHandler, Action onRequestEndHandler, Action<string> onFragmentReceivedHandler, Action<Exception> onParserException, bool secure)
{
_parser = new HttpParser(this);
_secure = secure;
_onRequestBeginHandler = onRequestBeginHandler;
_onRequestBodyReceivedHandler = onRequestBodyReceivedHandler;
_onRequestEndHandler = onRequestEndHandler;
_onParserException = onParserException;
}
示例6: OnQueryString
public void OnQueryString(HttpParser parser, string queryString)
{
var kv = HttpUtility.ParseQueryString(queryString);
foreach(var k in kv.AllKeys)
{
Console.WriteLine("{0}:{1}", k, kv[k]);
data[k] = kv[k];
}
}
示例7: OnHeaderName
public void OnHeaderName(HttpParser parser, string name)
{
if (headers == null)
headers = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
if (!string.IsNullOrEmpty(headerValue))
CommitHeader();
headerName = name;
}
示例8: OnMessageBegin
public async Task OnMessageBegin(HttpParser parser)
{
//Console.WriteLine("OnMessageBegin");
// TODO: this used to work, but i removed the StringBuffers. so work around maybe
// defer creation of buffers until message is created so
// NullRef will be thrown if OnMessageBegin is not called.
headers = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
body = new List<ArraySegment<byte>>();
}
示例9: ParseAsync
public async Task<MiddlemanRequest> ParseAsync(InboundConnection conn, Stream stream)
{
var del = new ParseDelegate();
var parser = new HttpParser(del);
int read;
var readTotal = 0;
var buffer = new byte[8192];
Log.Debug("{0}: RequestParser starting", conn.RemoteEndPoint);
var requestString = "";
while (stream != null && (read = await stream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) > 0)
{
requestString += Encoding.ASCII.GetString(buffer.Where(x => x != 0).ToArray(), 0, read);
readTotal += read;
if (parser.Execute(new ArraySegment<byte>(buffer, 0, read)) != read)
throw new FormatException("Parse error in request");
if (del.HeaderComplete)
break;
}
conn.MustClose = del.Request.Headers.AllKeys.Any(h => h.Equals("Connection", StringComparison.InvariantCultureIgnoreCase) && del.Request.Headers[h].Equals("Close", StringComparison.InvariantCultureIgnoreCase));
Log.Debug("{0}: RequestParser read enough ({1} bytes)", conn.RemoteEndPoint, readTotal);
Log.Info("ORIGINAL REQUEST: " + Environment.NewLine + requestString + Environment.NewLine);
if (readTotal == 0)
return null;
if (!del.HeaderComplete)
throw new FormatException("Parse error in request");
var request = del.Request;
request.ProtocolVersion = new Version(parser.MajorVersion, parser.MinorVersion);
conn.RequestVersion = request.ProtocolVersion;
var cl = request.ContentLength;
if (cl > 0 && stream != null)
{
request.RequestBody = del.RequestBodyStart.Count > 0
? new MaxReadStream(new StartAvailableStream(del.RequestBodyStart, stream), cl)
: new MaxReadStream(stream, cl);
}
return request;
}
示例10: OnHeadersEnd
public void OnHeadersEnd(HttpParser parser)
{
string upgradeType;
_headersComplete = true;
_request.Version = new Version(parser.MajorVersion, parser.MinorVersion);
_request.KeepAlive = parser.ShouldKeepAlive;
_request.Upgrade = _request.Headers.TryGetValue("upgrade", out upgradeType);
_request.UpgradeType = upgradeType;
_request.Secure = _secure;
if (_onRequestBeginHandler != null)
_onRequestBeginHandler(_request);
}
示例11: ParseAsync
public async Task<SwitchboardRequest> ParseAsync(InboundConnection conn, Stream stream)
{
var del = new ParseDelegate();
var parser = new HttpParser(del);
int read;
int readTotal = 0;
byte[] buffer = new byte[8192];
Debug.WriteLine(string.Format("{0}: RequestParser starting", conn.RemoteEndPoint));
while ((read = await stream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) > 0)
{
readTotal += read;
if (parser.Execute(new ArraySegment<byte>(buffer, 0, read)) != read)
throw new FormatException("Parse error in request");
if (del.headerComplete)
break;
}
Debug.WriteLine(string.Format("{0}: RequestParser read enough ({1} bytes)", conn.RemoteEndPoint, readTotal));
if (readTotal == 0)
return null;
if (!del.headerComplete)
throw new FormatException("Parse error in request");
var request = del.request;
request.ProtocolVersion = new Version(parser.MajorVersion, parser.MinorVersion);
int cl = request.ContentLength;
if (cl > 0)
{
if (del.requestBodyStart.Count > 0)
{
request.RequestBody = new MaxReadStream(new StartAvailableStream(del.requestBodyStart, stream), cl);
}
else
{
request.RequestBody = new MaxReadStream(stream, cl);
}
}
return request;
}
示例12: RequestsWithDigits
public void RequestsWithDigits()
{
foreach (var request in TestRequest.Requests.Where(r => r.Name.StartsWith("digits in "))) {
var handler = new Handler();
var parser = new HttpParser(handler);
Console.WriteLine("----- Testing request: '" + request.Name + "' -----");
var parsed = parser.Execute(new ArraySegment<byte>(request.Raw));
if (parsed != request.Raw.Length)
Assert.Fail("Error while parsing.");
AssertRequest(new TestRequest[] { request }, handler.Requests.ToArray(), parser);
}
}
示例13: OnHeadersEnd
public void OnHeadersEnd(HttpParser parser)
{
Debug.WriteLine("OnHeadersEnd");
if (!string.IsNullOrEmpty(headerValue))
CommitHeader();
var request = new HttpRequestHeaders()
{
// TODO path, query, fragment?
Method = method,
Uri = requestUri,
Headers = headers,
Version = new Version(parser.MajorVersion, parser.MinorVersion)
};
del.OnRequestBegan(request, parser.ShouldKeepAlive);
}
示例14: AssertRequest
static void AssertRequest(TestRequest[] expected, TestRequest[] actual, HttpParser machine)
{
for (int i = 0; i < expected.Length; i++)
{
Assert.IsTrue(i <= actual.Length - 1, "Expected more requests than received");
var expectedRequest = expected[i];
var actualRequest = actual[i];
//Console.WriteLine("Asserting request " + expectedRequest.Name);
Assert.AreEqual(expectedRequest.Method, actualRequest.Method, "Unexpected method.");
Assert.AreEqual(expectedRequest.RequestUri, actualRequest.RequestUri, "Unexpected request URI.");
Assert.AreEqual(expectedRequest.VersionMajor, actualRequest.VersionMajor, "Unexpected major version.");
Assert.AreEqual(expectedRequest.VersionMinor, actualRequest.VersionMinor, "Unexpected minor version.");
Assert.AreEqual(expectedRequest.RequestPath, actualRequest.RequestPath, "Unexpected request path.");
Assert.AreEqual(expectedRequest.QueryString, actualRequest.QueryString, "Unexpected query string.");
Assert.AreEqual(expectedRequest.Fragment, actualRequest.Fragment, "Unexpected fragment.");
//Assert.AreEqual(expected.RequestPath, test.RequestPath, "Unexpected path.");
Assert.IsTrue(actualRequest.OnHeadersEndCalled, "OnHeadersEnd was not called.");
Assert.AreEqual(expectedRequest.ShouldKeepAlive, actualRequest.ShouldKeepAlive, "Wrong value for ShouldKeepAlive");
foreach (var pair in expectedRequest.Headers)
{
Assert.IsTrue(actualRequest.Headers.ContainsKey(pair.Key), "Actual headers did not contain key '" + pair.Key + "'");
Assert.AreEqual(pair.Value, actualRequest.Headers[pair.Key], "Actual headers had wrong value for key '" + pair.Key + "'");
}
foreach (var pair in actualRequest.Headers)
{
Assert.IsTrue(expectedRequest.Headers.ContainsKey(pair.Key), "Unexpected header named '" + pair.Key + "'");
}
if (expectedRequest.Body != null)
{
var expectedBody = Encoding.UTF8.GetString(expectedRequest.Body);
Assert.IsNotNull(actualRequest.Body, "Expected non-null request body");
var actualBody = Encoding.UTF8.GetString(actualRequest.Body);
Assert.AreEqual(expectedBody, actualBody, "Body differs");
}
else
Assert.IsNull(actualRequest.Body);
}
}
示例15: OnMessageEnd
public async Task OnMessageEnd(HttpParser parser)
{
//Console.WriteLine("OnMessageEnd");
Assert.AreEqual(shouldKeepAlive, parser.ShouldKeepAlive,
"Differing values for parser.ShouldKeepAlive between OnHeadersEnd and OnMessageEnd");
TestRequest request = new TestRequest();
request.VersionMajor = versionMajor;
request.VersionMinor = versionMinor;
request.ShouldKeepAlive = shouldKeepAlive;
request.Method = method;
request.RequestUri = requestUri;
request.RequestPath = path;
request.QueryString = queryString;
request.Fragment = fragment;
request.Headers = headers;
request.OnHeadersEndCalled = onHeadersEndCalled;
// aggregate body chunks into one big chunk
var length = body.Aggregate(0, (s, b) => s + b.Count);
if (length > 0)
{
request.Body = new byte[length];
int where = 0;
foreach (var buf in body)
{
Buffer.BlockCopy(buf.Array, buf.Offset, request.Body, where, buf.Count);
where += buf.Count;
}
}
// add it to the list of requests recieved.
Requests.Add(request);
// reset our internal state
versionMajor = versionMinor = -1;
method = requestUri = queryString = fragment = headerName = headerValue = null;
headers = null;
body = null;
shouldKeepAlive = false;
onHeadersEndCalled = false;
}