internal Range(string input) { this._firstByte = 0; this._lastByte = 0; Tokenizer values = new Tokenizer(input, "-"); try { this._firstByte = (long)long.Parse(values.nextToken()); } catch (Exception) { } try { this._lastByte = (long)long.Parse(values.nextToken()); } catch (Exception) { } }
/// <summary> /// Construct a Request object that contains the information received from the client /// </summary> /// <param name="stream">The stream associated to the TCPClient</param> /// <param name="server">The server that handled the request</param> internal Request(Stream stream, Server server) { this.server = server; this.m_ClientIP = ""; // Receive max 16k from the client Byte[] receiveBuffer = new Byte[16384]; int i = stream.Read(receiveBuffer, 0 , receiveBuffer.Length); // Convert the data received to UTF8 this.sBuffer = Encoding.UTF8.GetString(receiveBuffer); this.m_Cookies = new ArrayList(); this.m_Ranges = new ArrayList(); this.parameters = new Hashtable(); Tokenizer toki = new Tokenizer(sBuffer, "\r\n"); Tokenizer fullRequest = new Tokenizer(toki.nextToken(), " "); m_Method = fullRequest.nextToken(); m_Path = fullRequest.nextToken(); Tokenizer path = new Tokenizer(m_Path.Substring(1), "?"); Page = path.nextToken(); parseParameters(path.nextToken()); m_HTTPVersion = fullRequest.nextToken(); while (toki.hasMoreTokens()) { string temp = toki.nextToken(); if (temp != null) { if (temp.IndexOf("Accept:") == 0) m_Accept = substract(temp); else if (temp.IndexOf("Accept-Language:") == 0) m_AcceptLanguage = substract(temp); else if (temp.IndexOf("Accept-Encoding:") == 0) m_AcceptEncoding = substract(temp); else if (temp.IndexOf("User-Agent:") == 0) m_UserAgent = substract(temp); else if (temp.IndexOf("Host:") == 0) m_Host = substract(temp); else if (temp.IndexOf("Connection:") == 0) m_Connection = substract(temp); else if (temp.IndexOf("Authorization:") == 0) m_Authorization = substract(temp); else if (temp.IndexOf("Cookie:") == 0) { string cookiesTemp = substract(temp); Tokenizer cookiesToki = new Tokenizer(cookiesTemp, "; "); while (cookiesToki.hasMoreTokens()) { string cookieTemp = cookiesToki.nextToken(); Tokenizer inCookie = new Tokenizer(cookieTemp, "="); this.m_Cookies.Add(new Cookie(inCookie.nextToken(), inCookie.nextToken())); } } else if (temp.IndexOf("Range:") == 0) { string rangesTemp = substract(temp); Tokenizer rangeValues = new Tokenizer(rangesTemp, "="); string rangeType = rangeValues.nextToken(); Tokenizer differentRanges = new Tokenizer(rangeValues.nextToken(), ","); while (differentRanges.hasMoreTokens()) { string range = differentRanges.nextToken(); this.m_Ranges.Add(new Range(range)); } } else if (temp == "") { if (Method == "POST") { parseParameters(toki.nextToken()); } } } } }
void parseParameters(string parameters_tokenized) { Tokenizer parameters_token = new Tokenizer(parameters_tokenized, "&"); while (parameters_token.hasMoreTokens()) { Tokenizer tempi = new Tokenizer(parameters_token.nextToken(), "="); parameters.Add(tempi.nextToken(), HTMLHelper.decode(tempi.nextToken())); } }
string substract(string temp) { Tokenizer toki = new Tokenizer(temp, ": "); toki.nextToken(); string toReturn = toki.nextToken(); return toReturn; }
private void startHandling(object threadInformation) { try { Tokenizer ip = new Tokenizer((string)client.Client.RemoteEndPoint.ToString(), ":"); // Create the request object Request request = new Request(this.stream, server); try { request.m_ClientIP = ip.nextToken(); } catch (Exception e) { request.server.handleVerbosity(e); } Response response = new Response(ref stream, request); Servlet instance; server.handleVerbosity("Client requested " + request.Page); if (request.Page == "") request.Page = "index"; Type type = Servlet.getServlet(request.Page); if (type != null) { try { instance = (Servlet)Activator.CreateInstance(type); try { if (request.getParameter("method") != null) instance.invokeMethod(type, request.getParameter("method"), request, response); else if (request.Method == "GET") instance.doGet(request, response); else if (request.Method == "POST") instance.doPost(request, response); } catch (Exception) { } } catch (Exception) { request.server.handleVerbosity("Resource " + request.Page + " not found."); } } else if (request.Page == "index") { if (fileExists("index.htm")) response.printFile(server.rootPath + "index.htm", false); else if (fileExists("Index.htm")) response.printFile(server.rootPath + "Index.htm", false); else if (fileExists("index.html")) response.printFile(server.rootPath + "index.html", false); else if (fileExists("Index.html")) response.printFile(server.rootPath + "Index.html", false); else Servlet.doGetIndex(request, response); } else if (fileExists(HTMLHelper.decode(request.Page))) { if (server.rootPath!=null) response.printFile(server.rootPath + HTMLHelper.decode(request.Page), false); } else if (request.Page.ToUpper() == "FAVICON.ICO") { Servlet.doGetFavicon(request, response); } else if (request.Page.ToUpper() == "TOPEKA.PNG") { Servlet.doGetTopekaLogo(request, response); } else if (server.resources.Count>0 && Servlet.findResourceStream(server, request.Page.Replace("/", ".")) != null) { string extension = request.Page.Substring(request.Page.LastIndexOf(".")+1); ResourceAssembly resource_assembly = Servlet.findResourceStream(server, request.Page.Replace("/", ".")); string resource = resource_assembly.assembly_namespace + "." + request.Page.Replace("/", "."); Servlet.printResource(resource_assembly.assembly, resource, extension, request, response); } else { response.statusCode = "404"; response.println("<font face='verdana'>Resource <b>\"" + HTMLHelper.decode(request.Page) + "\"</b> not found.</font>"); } response.flush(); request = null; response = null; } catch (Exception e) { server.handleVerbosity(e); } finally { this.client.Close(); } }