private static bool TryMatch(HttpContext context, IUriRouter uriRouter, bool logHttpRequests, string advertiseAsAddress, int advertiseAsPort) { var tcs = new TaskCompletionSource <bool>(); var httpEntity = new HttpEntity(context, logHttpRequests, advertiseAsAddress, advertiseAsPort, () => tcs.TrySetResult(true)); var request = httpEntity.Request; try { var allMatches = uriRouter.GetAllUriMatches(request.Url); if (allMatches.Count == 0) { NotFound(httpEntity); return(false); } var allowedMethods = GetAllowedMethods(allMatches); if (request.HttpMethod.Equals(HttpMethod.Options, StringComparison.OrdinalIgnoreCase)) { RespondWithOptions(httpEntity, allowedMethods); return(false); } var match = allMatches.LastOrDefault( m => m.ControllerAction.HttpMethod.Equals(request.HttpMethod, StringComparison.OrdinalIgnoreCase)); if (match == null) { MethodNotAllowed(httpEntity, allowedMethods); return(false); } ICodec requestCodec = null; var supportedRequestCodecs = match.ControllerAction.SupportedRequestCodecs; if (supportedRequestCodecs != null && supportedRequestCodecs.Length > 0) { requestCodec = SelectRequestCodec(request.HttpMethod, request.ContentType, supportedRequestCodecs); if (requestCodec == null) { BadContentType(httpEntity, "Invalid or missing Content-Type"); return(false); } } ICodec responseCodec = SelectResponseCodec(request, request.AcceptTypes, match.ControllerAction.SupportedResponseCodecs, match.ControllerAction.DefaultResponseCodec); if (responseCodec == null) { BadCodec(httpEntity, "Requested URI is not available in requested format"); return(false); } try { var manager = httpEntity.CreateManager(requestCodec, responseCodec, allowedMethods, satisfied => { }); context.Items.Add(manager.GetType(), manager); context.Items.Add(match.GetType(), match); context.Items.Add(tcs.GetType(), tcs); return(true); } catch (Exception exc) { Log.Error(exc, "Error while handling HTTP request '{url}'.", request.Url); InternalServerError(httpEntity); } } catch (Exception exc) { Log.Error(exc, "Unhandled exception while processing HTTP request at {url}.", httpEntity.RequestedUrl); InternalServerError(httpEntity); } return(false); }
public static void ReplyInternalServerError(HttpEntity entity) { var manager = entity.CreateManager(); manager.ReplyStatus(HttpStatusCode.InternalServerError, "Internal Server Error", exception => { }); }
private void ProcessRequest(HttpService httpService, HttpEntity httpEntity) { var request = httpEntity.Request; try { var allMatches = httpService.GetAllUriMatches(request.Url); if (allMatches.Count == 0) { NotFound(httpEntity); return; } var allowedMethods = GetAllowedMethods(allMatches); if (request.HttpMethod.Equals(HttpMethod.Options, StringComparison.OrdinalIgnoreCase)) { RespondWithOptions(httpEntity, allowedMethods); return; } var match = allMatches.LastOrDefault( m => m.ControllerAction.HttpMethod.Equals(request.HttpMethod, StringComparison.OrdinalIgnoreCase)); if (match == null) { MethodNotAllowed(httpEntity, allowedMethods); return; } ICodec requestCodec = null; var supportedRequestCodecs = match.ControllerAction.SupportedRequestCodecs; if (supportedRequestCodecs != null && supportedRequestCodecs.Length > 0) { requestCodec = SelectRequestCodec(request.HttpMethod, request.ContentType, supportedRequestCodecs); if (requestCodec == null) { BadContentType(httpEntity, "Invalid or missing Content-Type"); return; } } ICodec responseCodec = SelectResponseCodec(request.QueryString, request.AcceptTypes, match.ControllerAction.SupportedResponseCodecs, match.ControllerAction.DefaultResponseCodec); if (responseCodec == null) { BadCodec(httpEntity, "Requested URI is not available in requested format"); return; } try { var manager = httpEntity.CreateManager(requestCodec, responseCodec, allowedMethods, satisfied => { }); var reqParams = match.RequestHandler(manager, match.TemplateMatch); if (!reqParams.IsDone) { _pending.Add(Tuple.Create(DateTime.UtcNow + reqParams.Timeout, manager)); } } catch (Exception exc) { Log.ErrorException(exc, "Error while handling HTTP request '{0}'.", request.Url); InternalServerError(httpEntity); } } catch (Exception exc) { Log.ErrorException(exc, "Unhandled exception while processing HTTP request at [{0}].", string.Join(", ", httpService.ListenPrefixes)); InternalServerError(httpEntity); } PurgeTimedOutRequests(); }
public static void ReplyUnauthorized(HttpEntity entity) { var manager = entity.CreateManager(); manager.ReplyStatus(HttpStatusCode.Unauthorized, "Unauthorized", exception => { }); }