public void ProcessRequest(HttpContext context) { string output = string.Empty; output += "<b>Request Path:</b><br/>"; output += context.Request.Path; output += "<br/><b>Request Params:</b><br/>"; if (context.Request.Params != null) { for (int x = 0; x < context.Request.Params.Keys.Count; x++) { var s = context.Request.Params.Keys[x] + ": " + context.Request.Params[x]; output += s + "<br/>"; } } output = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>Netduino Web Server</title></head><body>" + output + "</body></html>"; context.Response.ContentType = "text/html"; context.Response.HttpStatus = HttpStatusCode.OK; context.Response.WriteLine(output); var a = context.Request.Path.Split('/'); if (a.Length == 3) { if (a[1] == "led") this.Led.Write(a[2] == "true"); } }
public void ProcessRequest(HttpContext context) { var a = context.Request.Path.Split('/'); if (a.Length == 3) { if (a[1] == "servo") { try { var val = Convert.ToDouble(a[2]); this.Servo.Degree = val; } catch { /* a human passed an NaN, probably */ } } } var output = this.Servo.Degree.ToString(); context.Response.ContentType = "text/plain"; context.Response.Write(output); }
internal void ProcessRequest() { #if(LOG && !MF && !WindowsCE) Console.WriteLine((_client.RemoteEndPoint as IPEndPoint).ToString()); #endif using (_client) { while (true) { #region Wait for first byte (used for keep-alive, too) int avail = 0; DateTime maxWait = DateTime.Now.AddMilliseconds(2000); do { try { avail = _client.Available; if (avail == 0) Thread.Sleep(10); } catch { break; } } while (avail == 0 && DateTime.Now <= maxWait); #endregion if (avail == 0) break; DateTime begin = DateTime.Now; HttpRequest httpRequest = new HttpRequest(); HttpResponse httpResponse = null; Stream stream; #if(SSL) if (_server.IsSecure && _server.Certificate != null) { SslStream ssl = null; try { #if(!MF) ssl = new SslStream(new NetworkStream(_client)); ssl.AuthenticateAsServer(_server.Certificate, false, SslProtocols.Default, false); #else ssl = new SslStream(_client); ssl.AuthenticateAsServer(_server.Certificate, SslVerification.NoVerification, SslProtocols.Default); #endif stream = ssl; } catch (Exception) { Close(); return; } } else #endif { stream = new NetworkStream(_client); } stream.ReadTimeout = 200; stream.WriteTimeout = 1000; try { if (!httpRequest.Read(stream, (_client.RemoteEndPoint as IPEndPoint))) { httpResponse = new HttpResponse(); httpResponse.RaiseError(HttpStatusCode.ServiceUnavailable); httpResponse.AddHeader("Connection", "close"); } } catch (HttpException ex) { httpResponse = new HttpResponse(); httpResponse.RaiseError(ex.Message, ex.Code); httpResponse.AddHeader("Connection", "close"); } catch (Exception) { httpResponse = new HttpResponse(); httpResponse.RaiseError(); httpResponse.AddHeader("Connection", "close"); } if (httpResponse == null) { httpResponse = new HttpResponse(); httpResponse.HttpVersion = httpRequest.HttpVersion; HttpContext ctx = new HttpContext(); ctx.Request = httpRequest; ctx.Response = httpResponse; try { _handler.ProcessRequest(ctx); } catch (HttpException ex) { httpResponse = new HttpResponse(); httpResponse.RaiseError(ex.Message, ex.Code); httpResponse.AddHeader("Connection", "close"); } catch (Exception) { httpResponse = new HttpResponse(); httpResponse.RaiseError(); httpResponse.AddHeader("Connection", "close"); } } httpResponse.Write(stream); stream.Flush(); LogAccess log = new LogAccess(); log.ClientIP = httpRequest.UserHostAddress; log.BytesReceived = httpRequest.totalBytes; log.BytesSent = httpResponse.totalBytes; log.Date = begin; log.Method = httpRequest.HttpMethod; log.RawUrl = httpRequest.RawUrl; log.UserAgent = httpRequest.UserAgent; log.HttpReferer = httpRequest.Referer; log.Duration = (DateTime.Now.Ticks - begin.Ticks) / TimeSpan.TicksPerMillisecond; _server.OnLogAccess(log); if (httpResponse.Connection == null || httpResponse.Connection != "Keep-Alive") break; Thread.Sleep(15); } Close(); } }