protected override void DispatchHttpRequest(HttpListenerContext context, string endpoint_name, string url) { var method = context.Request.HttpMethod; uint handler_id = 0; if (!s_HttpHandlerLookupTable.TryGetValue(endpoint_name, out handler_id)) { CommonHttpHandlers.PageNotFound(context); RootHttpHandler(context); return; } var querystring_idx = url.IndexOf('?'); switch (handler_id) { } }
/// <summary> /// The handler that processes requests on the root endpoint. /// The default handler responds with a list of available endpoints. /// This method can be overridden for custom behaviors. /// </summary> /// <param name="ctx">The context object.</param> protected override void RootHttpHandler(HttpListenerContext ctx) { CommonHttpHandlers.ListAvailableEndpoints(ctx, new List <string> { }, this.GetType()); }
protected override void DispatchHttpRequest(HttpListenerContext context, string endpoint_name, string url) { var method = context.Request.HttpMethod; uint handler_id = 0; if (!s_HttpHandlerLookupTable.TryGetValue(endpoint_name, out handler_id)) { CommonHttpHandlers.PageNotFound(context); RootHttpHandler(context); return; } var querystring_idx = url.IndexOf('?'); switch (handler_id) { /*FOREACH*/ /*USE_LIST("t_protocol")*/ /*IF("$t_protocol->is_http_protocol()")*/ case /*MUTE*/ 0 /*MUTE_END*/ /*GET_ITERATOR_VALUE*/: { IF("$t_protocol->pt_request == PT_STRUCT_REQUEST"); MAP_VAR("t_struct_name", "META_OUTPUT(tsl->find_struct_or_cell($t_protocol->request_message_struct)->name)"); string json_string; t_struct_name request_struct; if (method == "GET") { if (querystring_idx == -1) { json_string = url; } else { json_string = url.Substring(0, querystring_idx); } } else if (method == "POST") { using (var sr = new System.IO.StreamReader(context.Request.InputStream)) json_string = sr.ReadToEnd(); } else { context.Response.StatusCode = (int)HttpStatusCode.BadRequest; return; } if (!t_struct_name.TryParse(json_string, out request_struct)) { context.Response.StatusCode = (int)HttpStatusCode.BadRequest; return; } END(); IF("$t_protocol->pt_response == PT_STRUCT_RESPONSE"); MAP_VAR("t_struct_name", "META_OUTPUT(tsl->find_struct_or_cell($t_protocol->response_message_struct)->name)"); t_struct_name response_struct /*MUTE*/ = null /*MUTE_END*/; END(); t_protocol_nameHandler(/*META_OUTPUT("get_http_handler_calling_parameters($t_protocol)")*/); IF("$t_protocol->pt_response == PT_STRUCT_RESPONSE"); context.Response.ContentType = "application/json"; string jsonp_callback = context.Request.QueryString["jsonp_callback"] ?? context.Request.QueryString["callback"]; string iframe_callback = context.Request.QueryString["iframe_callback"]; using (var sw = new System.IO.StreamWriter(context.Response.OutputStream)) { if (jsonp_callback != null) { sw.Write("{0}(", jsonp_callback); sw.Write(Serializer.ToString(response_struct)); sw.Write(");", jsonp_callback); } else if (iframe_callback != null) { context.Response.ContentType = "text/html"; sw.Write("<script language=\"javascript\" type=\"text/javascript\">window.top.window.{0}(", iframe_callback); sw.Write(Serializer.ToString(response_struct)); sw.Write(");</script>"); } else { sw.Write(Serializer.ToString(response_struct)); } } END(); } break; /*END*/ /*END*/ } }
[END] //FOREACH /// <summary> /// Processes requests on the root endpoint. By default it /// will list available API endpoints in html. /// Override this method to get custom behaviors. /// </summary> /// <param name="ctx">A <see cref="HttpListenerContext"/> object.</param> protected override void RootHttpHandler(HttpListenerContext ctx) { CommonHttpHandlers.ListAvailableEndpoints(ctx, s_HttpHandlerLookupTable.Keys, this.GetType()); }