public void Process() { myReadBuffer = new byte[client.ReceiveBufferSize]; String myCompleteMessage = ""; int numberOfBytesRead = 0; Parent.WriteLog("Connection accepted. Buffer: " + client.ReceiveBufferSize.ToString()); NetworkStream ns = client.GetStream(); string hValue = ""; string hKey = ""; try { // binary data buffer index int bfndx = 0; string pathTmp = ""; // Incoming message may be larger than the buffer size. do { numberOfBytesRead = ns.Read(myReadBuffer, 0, myReadBuffer.Length); myCompleteMessage = String.Concat(myCompleteMessage, Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead)); // read buffer index int ndx = 0; do { switch (ParserState) { case RState.METHOD: if (myReadBuffer[ndx] != ' ') HTTPRequest.Method += (char)myReadBuffer[ndx++]; else { ndx++; ParserState = RState.URL; } break; case RState.URL: if (myReadBuffer[ndx] == '?') { ndx++; hKey = ""; HTTPRequest.Execute = true; HTTPRequest.Args = new Hashtable(); ParserState = RState.URLPARM; } else if (myReadBuffer[ndx] != ' ') pathTmp += (char)myReadBuffer[ndx++]; else { ndx++; ParserState = RState.VERSION; } break; case RState.URLPARM: if (myReadBuffer[ndx] == '=') { ndx++; hValue = ""; ParserState = RState.URLVALUE; } else if (myReadBuffer[ndx] == ' ') { ndx++; ParserState = RState.VERSION; } else { hKey += (char)myReadBuffer[ndx++]; } break; case RState.URLVALUE: if (myReadBuffer[ndx] == '&') { ndx++; hKey = HttpUtility.UrlDecode(hKey); hValue = HttpUtility.UrlDecode(hValue); HTTPRequest.Args[hKey] = HTTPRequest.Args[hKey] != null ? HTTPRequest.Args[hKey] + ", " + hValue : hValue; hKey = ""; ParserState = RState.URLPARM; } else if (myReadBuffer[ndx] == ' ') { ndx++; hKey = HttpUtility.UrlDecode(hKey); hValue = HttpUtility.UrlDecode(hValue); HTTPRequest.Args[hKey] = HTTPRequest.Args[hKey] != null ? HTTPRequest.Args[hKey] + ", " + hValue : hValue; ParserState = RState.VERSION; } else { hValue += (char)myReadBuffer[ndx++]; } break; case RState.VERSION: if (myReadBuffer[ndx] == '\r') ndx++; else if (myReadBuffer[ndx] != '\n') HTTPRequest.Version += (char)myReadBuffer[ndx++]; else { ndx++; hKey = ""; HTTPRequest.Headers = new Hashtable(); ParserState = RState.HEADERKEY; } break; case RState.HEADERKEY: if (myReadBuffer[ndx] == '\r') ndx++; else if (myReadBuffer[ndx] == '\n') { ndx++; if (HTTPRequest.Headers["Content-Length"] != null) { HTTPRequest.BodySize = Convert.ToInt32(HTTPRequest.Headers["Content-Length"]); this.HTTPRequest.BodyData = new byte[this.HTTPRequest.BodySize]; ParserState = RState.BODY; } else ParserState = RState.OK; } else if (myReadBuffer[ndx] == ':') ndx++; else if (myReadBuffer[ndx] != ' ') hKey += (char)myReadBuffer[ndx++]; else { ndx++; hValue = ""; ParserState = RState.HEADERVALUE; } break; case RState.HEADERVALUE: if (myReadBuffer[ndx] == '\r') ndx++; else if (myReadBuffer[ndx] != '\n') hValue += (char)myReadBuffer[ndx++]; else { ndx++; HTTPRequest.Headers.Add(hKey, hValue); hKey = ""; ParserState = RState.HEADERKEY; } break; case RState.BODY: // Append to request BodyData Array.Copy(myReadBuffer, ndx, this.HTTPRequest.BodyData, bfndx, numberOfBytesRead - ndx); bfndx += numberOfBytesRead - ndx; ndx = numberOfBytesRead; if (this.HTTPRequest.BodySize <= bfndx) { ParserState = RState.OK; } break; default: ndx++; break; } } while (ndx < numberOfBytesRead); } while (ns.DataAvailable); HTTPRequest.URL = new Uri("http://localhost" + pathTmp); // Print out the received message to the console. Parent.WriteLog("You received the following message : \n" + myCompleteMessage); HTTPResponse.version = "HTTP/1.1"; if (ParserState != RState.OK) HTTPResponse.status = (int)RespState.BAD_REQUEST; else HTTPResponse.status = (int)RespState.OK; this.HTTPResponse.Headers = new Hashtable(); this.HTTPResponse.Headers.Add("Server", Parent.Name); this.HTTPResponse.Headers.Add("Date", DateTime.Now.ToString("r")); // if (HTTPResponse.status == (int)RespState.OK) this.Parent.OnResponse(ref this.HTTPRequest, ref this.HTTPResponse); string HeadersString = this.HTTPResponse.version + " " + this.Parent.respStatus[this.HTTPResponse.status] + "\r\n"; foreach (DictionaryEntry Header in this.HTTPResponse.Headers) { HeadersString += Header.Key + ": " + Header.Value + "\r\n"; } HeadersString += "\r\n"; byte[] bHeadersString = Encoding.ASCII.GetBytes(HeadersString); // Send headers ns.Write(bHeadersString, 0, bHeadersString.Length); // Send body if (this.HTTPResponse.BodyData != null) ns.Write(this.HTTPResponse.BodyData, 0, this.HTTPResponse.BodyData.Length); if (this.HTTPResponse.fs != null) using (this.HTTPResponse.fs) { byte[] b = new byte[client.SendBufferSize]; int bytesRead; while ((bytesRead = this.HTTPResponse.fs.Read(b, 0, b.Length)) > 0) { ns.Write(b, 0, bytesRead); } this.HTTPResponse.fs.Close(); } } catch (Exception e) { Parent.WriteLog(e.ToString()); } finally { ns.Close(); client.Close(); if (this.HTTPResponse.fs != null) this.HTTPResponse.fs.Close(); Thread.CurrentThread.Abort(); } }
internal static HTTPRequest ParseRequest(string rawData) { string hValue = ""; string hKey = ""; RState ParserState = RState.METHOD; HTTPRequest request = new HTTPRequest(); int ndx = 0, bfndx = 0; do { switch (ParserState) { case RState.METHOD: if (rawData[ndx] != ' ') { request.Method += (char)rawData[ndx++]; } else { ndx++; ParserState = RState.URL; } break; case RState.URL: if (rawData[ndx] == '?') { ndx++; hKey = ""; request.Execute = true; request.Args = new Hashtable(); ParserState = RState.URLPARM; } else if (rawData[ndx] != ' ') { request.URL += (char)rawData[ndx++]; } else { ndx++; request.URL = UrlDecode(request.URL); ParserState = RState.VERSION; } break; case RState.URLPARM: if (rawData[ndx] == '=') { ndx++; hValue = ""; ParserState = RState.URLVALUE; } else if (rawData[ndx] == ' ') { ndx++; request.URL = UrlDecode(request.URL); ParserState = RState.VERSION; } else { hKey += (char)rawData[ndx++]; } break; case RState.URLVALUE: if (rawData[ndx] == '&') { ndx++; hKey = UrlDecode(hKey); hValue = UrlDecode(hValue); request.Args[hKey] = request.Args[hKey] != null ? request.Args[hKey] + ", " + hValue : hValue; hKey = ""; ParserState = RState.URLPARM; } else if (rawData[ndx] == ' ') { ndx++; hKey = UrlDecode(hKey); hValue = UrlDecode(hValue); request.Args[hKey] = request.Args[hKey] != null ? request.Args[hKey] + ", " + hValue : hValue; request.URL = UrlDecode(request.URL); ParserState = RState.VERSION; } else { hValue += (char)rawData[ndx++]; } break; case RState.VERSION: if (rawData[ndx] == '\r') { ndx++; } else if (rawData[ndx] != '\n') { request.Version += (char)rawData[ndx++]; } else { ndx++; hKey = ""; request.Headers = new Hashtable(); ParserState = RState.HEADERKEY; } break; case RState.HEADERKEY: if (rawData[ndx] == '\r') { ndx++; } else if (rawData[ndx] == '\n') { ndx++; if (request.Headers["Content-Length"] != null) { request.BodySize = Convert.ToInt32(request.Headers["Content-Length"]); request.BodyData = new byte[request.BodySize]; ParserState = RState.BODY; } else { ParserState = RState.OK; } } else if (rawData[ndx] == ':') { ndx++; } else if (rawData[ndx] != ' ') { hKey += (char)rawData[ndx++]; } else { ndx++; hValue = ""; ParserState = RState.HEADERVALUE; } break; case RState.HEADERVALUE: if (rawData[ndx] == '\r') { ndx++; } else if (rawData[ndx] != '\n') { hValue += (char)rawData[ndx++]; } else { ndx++; request.Headers.Add(hKey, hValue); hKey = ""; ParserState = RState.HEADERKEY; } break; case RState.BODY: // Append to request BodyData var body = rawData.Substring(ndx); request.BodyData = body.Select(c => (byte)c).ToArray(); bfndx += rawData.Length - ndx; ndx = rawData.Length; if (request.BodySize <= bfndx) { ParserState = RState.OK; } break; //default: // ndx++; // break; } } while (ndx < rawData.Length); return(request); }
public override void Update() { // 更新动画 base.Update(); // 速度限制 -- 有更方便的限制取值范围方法 float hSpeed = body.LinearVelocity.X; if (hSpeed > MaxRunSpeed) { body.LinearVelocity.X = MaxRunSpeed; } else if (hSpeed < -MaxRunSpeed) { body.LinearVelocity.X = -MaxRunSpeed; } switch (roleState) { case RState.Free: #region FSM // 检查脸的朝向 if (body.LinearVelocity.X > 0.01) { faceRight = true; } else if(body.LinearVelocity.X < -0.01) { faceRight = false; } // 按方向键,进行左右移动 if (InputKeyboards.isKeyPress(Keys.Right)) { body.ClearForce(); this.InnerXForce = runForce; body.ApplyForce(new Vector2(runForce, 0) * BaseGame.ElapsedTimeThisFrameInMilliseconds); } else if (InputKeyboards.isKeyPress(Keys.Left)) { body.ClearForce(); this.InnerXForce = -runForce; body.ApplyForce(new Vector2(-runForce, 0) * BaseGame.ElapsedTimeThisFrameInMilliseconds); } // 按向上键,跳跃 else if (InputKeyboards.isKeyPress(Keys.Up)) { roleState = RState.Jumping; PlaySeq("Jumping"); body.ClearForce(); body.ApplyForce(new Vector2(0, -jumpForce) * BaseGame.ElapsedTimeThisFrameInMilliseconds); } // 按空格键,射击 else if (InputKeyboards.isKeyPress(Keys.Space)) { roleState = RState.UsingGun; PlaySeq("UsingGun"); } // 如果body有速度并且处于地面上,则转换状态为Running if (body.LinearVelocity.Length() > 0.01f && isOnGround()) { roleState = RState.Running; PlaySeq("Running"); } // 如果非OnGround,则获取和地面之间的关系,并转换为对应的Jumping if (!isOnGround()) { roleState = RState.Jumping; } // 如果在地面上、和物体接触,并且合力指向物体,则转换为Pushing if (closeToPushItem())//... { // roleState = RState.Pushing; } #endregion break; case RState.Running: // 检查脸的朝向 if (body.LinearVelocity.X > 0.01) { faceRight = true; } else if (body.LinearVelocity.X < -0.01) { faceRight = false; } // 按方向键,进行左右移动 if (InputKeyboards.isKeyPress(Keys.Right)) { body.ClearForce(); this.InnerXForce = runForce; body.ApplyForce(new Vector2(runForce, 0) * BaseGame.ElapsedTimeThisFrameInMilliseconds); } else if (InputKeyboards.isKeyPress(Keys.Left)) { body.ClearForce(); this.InnerXForce = -runForce; body.ApplyForce(new Vector2(-runForce, 0) * BaseGame.ElapsedTimeThisFrameInMilliseconds); } // 按向上键,跳跃 else if (InputKeyboards.isKeyPress(Keys.Up)) { roleState = RState.Jumping; PlaySeq("Jumping"); body.ClearForce(); body.ApplyForce(new Vector2(0, -jumpForce) * BaseGame.ElapsedTimeThisFrameInMilliseconds); } // 如果body无速度并且处于地面上,则转换状态为Free if ((body.LinearVelocity.Length() <= 0.01f) && isOnGround()) { roleState = RState.Free; PlaySeq("Free"); } // 如果合力不同于内部力+摩擦,则转换状态为Pushing // 临时对摩擦使用硬编码 if ((Math.Abs(InnerXForce) - body.Mass * 980 * 1.0 - Math.Abs(body.Force.X)) <= 0.001f) { // roleState = RState.Pushing; // PlaySeq("UsingGun"); } // 如果body有速度并且不处于地面上,则转换状态为Jumping // 如果UsingGun开关打开,则转换为UsingGun // 如果UsingItem开关打开,则转换为UsingItem // 如果UsingHook开关打开,则转换为UsingHook // 如果GettingItem开关打开,则转换为GettingItem if (InputKeyboards.isKeyPress(Keys.Up)) { roleState = RState.Jumping; PlaySeq("Jumping"); body.ClearForce(); body.ApplyForce(new Vector2(0, -jumpForce) * BaseGame.ElapsedTimeThisFrameInMilliseconds); } if (InputKeyboards.isKeyPress(Keys.Space)) { // ... 射击 } break; case RState.Jumping: // 如果body有速度并且处于地面上,则转换状态为Running // 如果body无速度,则转换状态为Free if (body.LinearVelocity.Length() <= 0.01f) { roleState = RState.Free; PlaySeq("Free"); } // 如果UsingGun开关打开,则转换为JumpingShooting // 如果UsingHook开关打开,则转换为UsingHook // 如果GettingItem开关打开,则转换为GettingItem if (InputKeyboards.isKeyJustPress(Keys.Space)) { // ... 跳跃射击 } break; case RState.Pushing: // 如果不和可推物体分离或者没有指向物体的合力,则转换为Free/Running if ((Math.Abs(InnerXForce) - body.Mass * 980 * 1.0f) <= 0.001f) { roleState = RState.Free; PlaySeq("Free"); } // 如果GettingItem开关打开,则转换为GettingItem break; case RState.GettingItem: #region FSM if (currentSeq.name != "GettingItem") { roleState = RState.Free; PlaySeq("Free"); } #endregion break; case RState.UsingHook: if (currentSeq.name != "UsingHook") { roleState = RState.Free; PlaySeq("Free"); } break; case RState.UsingItem: if (currentSeq.name != "UsingItem") { roleState = RState.Free; PlaySeq("Free"); } break; case RState.UsingGun: if (currentSeq.name != "UsingGun") { roleState = RState.Free; PlaySeq("Free"); } break; case RState.UsingGunInAir: if (currentSeq.name != "UsingGunInAir") { roleState = RState.Free; PlaySeq("Free"); } break; } }
public void Process() { myReadBuffer = new byte[client.ReceiveBufferSize]; String myCompleteMessage = ""; int numberOfBytesRead = 0; Parent.WriteLog("Connection accepted. Buffer: " + client.ReceiveBufferSize.ToString()); NetworkStream ns = client.GetStream(); string hValue = ""; string hKey = ""; try { // binary data buffer index int bfndx = 0; // Incoming message may be larger than the buffer size. do { numberOfBytesRead = ns.Read(myReadBuffer, 0, myReadBuffer.Length); myCompleteMessage = String.Concat(myCompleteMessage, Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead)); // read buffer index int ndx = 0; do { switch (ParserState) { case RState.METHOD: if (myReadBuffer[ndx] != ' ') { HTTPRequest.Method += (char)myReadBuffer[ndx++]; } else { ndx++; ParserState = RState.URL; } break; case RState.URL: if (myReadBuffer[ndx] == '?') { ndx++; hKey = ""; HTTPRequest.Execute = true; HTTPRequest.Args = new Hashtable(); ParserState = RState.URLPARM; } else if (myReadBuffer[ndx] != ' ') { HTTPRequest.URL += (char)myReadBuffer[ndx++]; } else { ndx++; HTTPRequest.URL = HttpUtility.UrlDecode(HTTPRequest.URL); ParserState = RState.VERSION; } break; case RState.URLPARM: if (myReadBuffer[ndx] == '=') { ndx++; hValue = ""; ParserState = RState.URLVALUE; } else if (myReadBuffer[ndx] == ' ') { ndx++; HTTPRequest.URL = HttpUtility.UrlDecode(HTTPRequest.URL); ParserState = RState.VERSION; } else { hKey += (char)myReadBuffer[ndx++]; } break; case RState.URLVALUE: if (myReadBuffer[ndx] == '&') { ndx++; hKey = HttpUtility.UrlDecode(hKey); hValue = HttpUtility.UrlDecode(hValue); HTTPRequest.Args[hKey] = HTTPRequest.Args[hKey] != null ? HTTPRequest.Args[hKey] + ", " + hValue : hValue; hKey = ""; ParserState = RState.URLPARM; } else if (myReadBuffer[ndx] == ' ') { ndx++; hKey = HttpUtility.UrlDecode(hKey); hValue = HttpUtility.UrlDecode(hValue); HTTPRequest.Args[hKey] = HTTPRequest.Args[hKey] != null ? HTTPRequest.Args[hKey] + ", " + hValue : hValue; HTTPRequest.URL = HttpUtility.UrlDecode(HTTPRequest.URL); ParserState = RState.VERSION; } else { hValue += (char)myReadBuffer[ndx++]; } break; case RState.VERSION: if (myReadBuffer[ndx] == '\r') { ndx++; } else if (myReadBuffer[ndx] != '\n') { HTTPRequest.Version += (char)myReadBuffer[ndx++]; } else { ndx++; hKey = ""; HTTPRequest.Headers = new Hashtable(); ParserState = RState.HEADERKEY; } break; case RState.HEADERKEY: if (myReadBuffer[ndx] == '\r') { ndx++; } else if (myReadBuffer[ndx] == '\n') { ndx++; if (HTTPRequest.Headers["Content-Length"] != null) { HTTPRequest.BodySize = Convert.ToInt32(HTTPRequest.Headers["Content-Length"]); this.HTTPRequest.BodyData = new byte[this.HTTPRequest.BodySize]; ParserState = RState.BODY; } else { ParserState = RState.OK; } } else if (myReadBuffer[ndx] == ':') { ndx++; } else if (myReadBuffer[ndx] != ' ') { hKey += (char)myReadBuffer[ndx++]; } else { ndx++; hValue = ""; ParserState = RState.HEADERVALUE; } break; case RState.HEADERVALUE: if (myReadBuffer[ndx] == '\r') { ndx++; } else if (myReadBuffer[ndx] != '\n') { hValue += (char)myReadBuffer[ndx++]; } else { ndx++; HTTPRequest.Headers.Add(hKey, hValue); hKey = ""; ParserState = RState.HEADERKEY; } break; case RState.BODY: // Append to request BodyData Array.Copy(myReadBuffer, ndx, this.HTTPRequest.BodyData, bfndx, numberOfBytesRead - ndx); bfndx += numberOfBytesRead - ndx; ndx = numberOfBytesRead; if (this.HTTPRequest.BodySize <= bfndx) { ParserState = RState.OK; } break; //default: // ndx++; // break; } }while(ndx < numberOfBytesRead); }while(ns.DataAvailable); // Print out the received message to the console. Parent.WriteLog("You received the following message : \n" + myCompleteMessage); HTTPResponse.version = "HTTP/1.1"; if (ParserState != RState.OK) { HTTPResponse.status = (int)RespState.BAD_REQUEST; } else { HTTPResponse.status = (int)RespState.OK; } this.HTTPResponse.Headers = new Hashtable(); this.HTTPResponse.Headers.Add("Server", Parent.Name); this.HTTPResponse.Headers.Add("Date", DateTime.Now.ToString("r")); // if (HTTPResponse.status == (int)RespState.OK) this.Parent.OnResponse(ref this.HTTPRequest, ref this.HTTPResponse); string HeadersString = this.HTTPResponse.version + " " + this.Parent.respStatus[this.HTTPResponse.status] + "\n"; foreach (DictionaryEntry Header in this.HTTPResponse.Headers) { HeadersString += Header.Key + ": " + Header.Value + "\n"; } HeadersString += "\n"; byte[] bHeadersString = Encoding.ASCII.GetBytes(HeadersString); // Send headers ns.Write(bHeadersString, 0, bHeadersString.Length); // Send body if (this.HTTPResponse.BodyData != null) { ns.Write(this.HTTPResponse.BodyData, 0, this.HTTPResponse.BodyData.Length); } if (this.HTTPResponse.fs != null) { using (this.HTTPResponse.fs) { byte[] b = new byte[client.SendBufferSize]; int bytesRead; while ((bytesRead = this.HTTPResponse.fs.Read(b, 0, b.Length)) > 0) { ns.Write(b, 0, bytesRead); } this.HTTPResponse.fs.Close(); } } } catch (Exception e) { Parent.WriteLog(e.ToString()); } finally { ns.Close(); client.Close(); if (this.HTTPResponse.fs != null) { this.HTTPResponse.fs.Close(); } Thread.CurrentThread.Abort(); } }
private void DoTrashAction(GameStateContext c, string s) { var trashName = actionDictionary[s]; var trashCard = game.ActivePlayer.Hand.Remove(trashName); game.Trash.Add(trashCard); gainValue = 2 + trashCard.Price; remodelState = RState.PICK_GAIN; c.State = this; }
/// <summary> /// 使用RoleData序列化类进行构造 /// </summary> /// <param name="roleData"></param> public Role(RoleData roleData) { // ItemBase texture = LoadHelper.LoadTexture2D(roleData.textureName); layer = roleData.layer; // AnimItem frameWidth = roleData.frameWidth; frameHeight = roleData.frameHeight; frameNumber = roleData.frameNumber; animSeqList = roleData.animSeqList; // PhysicsItem Body body = BodyFactory.Instance.CreateRectangleBody(PhysicsSys.Instance.PhysicsSimulator, roleData.size.X, roleData.size.Y, roleData.mass); body.Position = roleData.position; Geom geom = GeomFactory.Instance.CreateRectangleGeom(PhysicsSys.Instance.PhysicsSimulator, body, roleData.size.X, roleData.size.Y); // Role health = roleData.health; roleState = roleData.roleState; faceRight = roleData.facingRight; runForce = roleData.runForce; jumpForce = roleData.jumpForce; MaxRunSpeed = roleData.maxRunSpeed; foreach (KeyValuePair<String, String> itemPair in roleData.itemDic) { itemDic.Add(itemPair.Value, ItemFactory.CreateItem(itemPair.Key)); } }
private void strParsing(string str, byte[] myReadBuffer, int numberOfBytesRead) { // read buffer index int ndx = 0; do { switch (ParserState) { case RState.METHOD: if (myReadBuffer[ndx] != ' ') { HTTPRequest.Method += (char)myReadBuffer[ndx++]; } else { ndx++; ParserState = RState.URL; } break; case RState.URL: if (myReadBuffer[ndx] == '?') { ndx++; hKey = ""; HTTPRequest.Execute = true; HTTPRequest.Args = new Hashtable(); ParserState = RState.URLPARM; } else if (myReadBuffer[ndx] != ' ') { HTTPRequest.URL += (char)myReadBuffer[ndx++]; } else { ndx++; HTTPRequest.URL = HttpUtility.UrlDecode(HTTPRequest.URL); ParserState = RState.VERSION; } break; case RState.URLPARM: if (myReadBuffer[ndx] == '=') { ndx++; hValue = ""; ParserState = RState.URLVALUE; } else if (myReadBuffer[ndx] == ' ') { ndx++; HTTPRequest.URL = HttpUtility.UrlDecode(HTTPRequest.URL); ParserState = RState.VERSION; } else { hKey += (char)myReadBuffer[ndx++]; } break; case RState.URLVALUE: if (myReadBuffer[ndx] == '&') { ndx++; hKey = HttpUtility.UrlDecode(hKey); hValue = HttpUtility.UrlDecode(hValue); HTTPRequest.Args[hKey] = HTTPRequest.Args[hKey] != null ? HTTPRequest.Args[hKey] + ", " + hValue : hValue; hKey = ""; ParserState = RState.URLPARM; } else if (myReadBuffer[ndx] == ' ') { ndx++; hKey = HttpUtility.UrlDecode(hKey); hValue = HttpUtility.UrlDecode(hValue); HTTPRequest.Args[hKey] = HTTPRequest.Args[hKey] != null ? HTTPRequest.Args[hKey] + ", " + hValue : hValue; HTTPRequest.URL = HttpUtility.UrlDecode(HTTPRequest.URL); ParserState = RState.VERSION; } else { hValue += (char)myReadBuffer[ndx++]; } break; case RState.VERSION: if (myReadBuffer[ndx] == '\r') { ndx++; } else if (myReadBuffer[ndx] != '\n') { HTTPRequest.Version += (char)myReadBuffer[ndx++]; } else { ndx++; hKey = ""; HTTPRequest.Headers = new Hashtable(); ParserState = RState.HEADERKEY; } break; case RState.HEADERKEY: if (myReadBuffer[ndx] == '\r') { ndx++; } else if (myReadBuffer[ndx] == '\n') { ndx++; if (HTTPRequest.Headers["Content-Length"] != null) { HTTPRequest.BodySize = Convert.ToInt32(HTTPRequest.Headers["Content-Length"]); this.HTTPRequest.BodyData = new byte[this.HTTPRequest.BodySize]; ParserState = RState.BODY; } else { ParserState = RState.OK; } } else if (myReadBuffer[ndx] == ':') { ndx++; } else if (myReadBuffer[ndx] != ' ') { hKey += (char)myReadBuffer[ndx++]; } else { ndx++; hValue = ""; ParserState = RState.HEADERVALUE; } break; case RState.HEADERVALUE: if (myReadBuffer[ndx] == '\r') { ndx++; } else if (myReadBuffer[ndx] != '\n') { hValue += (char)myReadBuffer[ndx++]; } else { ndx++; if (!HTTPRequest.Headers.Contains(hKey)) { HTTPRequest.Headers.Add(hKey, hValue); } hKey = ""; ParserState = RState.HEADERKEY; } break; case RState.BODY: // Append to request BodyData Array.Copy(myReadBuffer, ndx, this.HTTPRequest.BodyData, bfndx, numberOfBytesRead - ndx); bfndx += numberOfBytesRead - ndx; ndx = numberOfBytesRead; if (this.HTTPRequest.BodySize <= bfndx) { ParserState = RState.OK; } break; //default: // ndx++; // break; } }while (ndx < numberOfBytesRead); }
/// <summary> /// Processes actually the HTTP Request. /// </summary> /// <remarks>Documented by Dev10, 2008-08-07</remarks> public void Process() { myReadBuffer = new byte[client.ReceiveBufferSize]; String myCompleteMessage = ""; int numberOfBytesRead = 0; #if DEBUG && debug_output Debug.WriteLine("Connection accepted. Buffer: " + client.ReceiveBufferSize.ToString()); #endif NetworkStream ns = client.GetStream(); string hValue = ""; string hKey = ""; try { // binary data buffer index int bfndx = 0; // Incoming message may be larger than the buffer size. do { numberOfBytesRead = ns.Read(myReadBuffer, 0, myReadBuffer.Length); myCompleteMessage = String.Concat(myCompleteMessage, Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead)); // read buffer index int ndx = 0; do { switch (ParserState) { case RState.METHOD: if (myReadBuffer[ndx] != ' ') HTTPRequest.Method += (char)myReadBuffer[ndx++]; else { ndx++; ParserState = RState.URL; } break; case RState.URL: if (myReadBuffer[ndx] == '?') { ndx++; hKey = ""; HTTPRequest.Execute = true; HTTPRequest.Args = new Hashtable(); ParserState = RState.URLPARM; } else if (myReadBuffer[ndx] != ' ') HTTPRequest.URL += (char)myReadBuffer[ndx++]; else { ndx++; HTTPRequest.URL = Uri.UnescapeDataString(HTTPRequest.URL); ParserState = RState.VERSION; } break; case RState.URLPARM: if (myReadBuffer[ndx] == '=') { ndx++; hValue = ""; ParserState = RState.URLVALUE; } else if (myReadBuffer[ndx] == ' ') { ndx++; HTTPRequest.URL = Uri.UnescapeDataString(HTTPRequest.URL); ParserState = RState.VERSION; } else { hKey += (char)myReadBuffer[ndx++]; } break; case RState.URLVALUE: if (myReadBuffer[ndx] == '&') { ndx++; hKey = Uri.UnescapeDataString(hKey); hValue = Uri.UnescapeDataString(hValue); HTTPRequest.Args[hKey] = HTTPRequest.Args[hKey] != null ? HTTPRequest.Args[hKey] + ", " + hValue : hValue; hKey = ""; ParserState = RState.URLPARM; } else if (myReadBuffer[ndx] == ' ') { ndx++; hKey = Uri.UnescapeDataString(hKey); hValue = Uri.UnescapeDataString(hValue); HTTPRequest.Args[hKey] = HTTPRequest.Args[hKey] != null ? HTTPRequest.Args[hKey] + ", " + hValue : hValue; HTTPRequest.URL = Uri.UnescapeDataString(HTTPRequest.URL); ParserState = RState.VERSION; } else { hValue += (char)myReadBuffer[ndx++]; } break; case RState.VERSION: if (myReadBuffer[ndx] == '\r') ndx++; else if (myReadBuffer[ndx] != '\n') HTTPRequest.Version += (char)myReadBuffer[ndx++]; else { ndx++; hKey = ""; HTTPRequest.Headers = new Hashtable(); ParserState = RState.HEADERKEY; } break; case RState.HEADERKEY: if (myReadBuffer[ndx] == '\r') ndx++; else if (myReadBuffer[ndx] == '\n') { ndx++; if (HTTPRequest.Headers["Content-Length"] != null) { HTTPRequest.BodySize = Convert.ToInt32(HTTPRequest.Headers["Content-Length"]); this.HTTPRequest.BodyData = new byte[this.HTTPRequest.BodySize]; ParserState = RState.BODY; } else ParserState = RState.OK; } else if (myReadBuffer[ndx] == ':') ndx++; else if (myReadBuffer[ndx] != ' ') hKey += (char)myReadBuffer[ndx++]; else { ndx++; hValue = ""; ParserState = RState.HEADERVALUE; } break; case RState.HEADERVALUE: if (myReadBuffer[ndx] == '\r') ndx++; else if (myReadBuffer[ndx] != '\n') hValue += (char)myReadBuffer[ndx++]; else { ndx++; if (!HTTPRequest.Headers.ContainsKey(hKey)) HTTPRequest.Headers.Add(hKey, hValue); hKey = ""; ParserState = RState.HEADERKEY; } break; case RState.BODY: // Append to request BodyData Array.Copy(myReadBuffer, ndx, this.HTTPRequest.BodyData, bfndx, numberOfBytesRead - ndx); bfndx += numberOfBytesRead - ndx; ndx = numberOfBytesRead; if (this.HTTPRequest.BodySize <= bfndx) { ParserState = RState.OK; } break; default: ndx++; break; } } while (ndx < numberOfBytesRead); } while (ns.DataAvailable); #if DEBUG && debug_output // Print out the received message to the console. Debug.WriteLine("Media server received request: " + Environment.NewLine + myCompleteMessage); #endif //Build up the HTTPResponse HTTPResponse.version = "HTTP/1.1"; if (ParserState != RState.OK) HTTPResponse.status = (int)RespState.BAD_REQUEST; else HTTPResponse.status = (int)RespState.OK; this.HTTPResponse.Headers = new Hashtable(); this.HTTPResponse.Headers.Add("Server", Parent.Name); this.HTTPResponse.Headers.Add("Date", DateTime.Now.ToString("r")); this.HTTPResponse.Headers.Add("Cache-Control", "no-cache"); this.HTTPResponse.Headers.Add("Pragma", "no-cache"); this.HTTPResponse.Headers.Add("Expires", "-1"); #if DEBUG && debug_output //Call the overriden SubClass Method to complete the HTTPResponse Content Debug.WriteLine("Preparing reponse."); #endif this.Parent.OnResponse(ref this.HTTPRequest, ref this.HTTPResponse); //Create the Header String string HeadersString = this.HTTPResponse.version + " " + this.Parent.respStatus[this.HTTPResponse.status] + "\r\n"; foreach (DictionaryEntry Header in this.HTTPResponse.Headers) { HeadersString += Header.Key + ": " + Header.Value + "\r\n"; } HeadersString += "\r\n"; byte[] bHeadersString = Encoding.ASCII.GetBytes(HeadersString); #if DEBUG && debug_output // Send headers Debug.WriteLine("Response headers: " + Environment.NewLine + HeadersString); #endif ns.Write(bHeadersString, 0, bHeadersString.Length); ns.Flush(); // Send body (File) if (this.HTTPResponse.mediaStream != null) using (this.HTTPResponse.mediaStream) { byte[] b = new byte[client.SendBufferSize]; int bytesRead; int totalBytes = 0; int totalLength = Convert.ToInt32(this.HTTPResponse.mediaStream.Length); while ((bytesRead = this.HTTPResponse.mediaStream.Read(b, 0, b.Length)) > 0) { ns.Write(b, 0, bytesRead); totalBytes += bytesRead; #if DEBUG && debug_output Debug.WriteLine(string.Format("Sent {0:0,0} / {1:0,0} KBytes ({2:0.0%}).", 1.0 * totalBytes / 1024, 1.0 * totalLength / 1024, 1.0 * totalBytes / totalLength)); #endif } ns.Flush(); this.HTTPResponse.mediaStream.Close(); } } catch (Exception e) { if (e is WebException) Trace.WriteLine("A Webexception in CsHTTPRequest was thrown. URI: " + ((WebException)e).Response.ResponseUri.ToString() + Environment.NewLine + e.ToString()); else Trace.WriteLine("An Exception in CsHTTPRequest was thrown: " + e.ToString()); if (e.InnerException != null && e.InnerException is SocketException) { SocketException se = ((SocketException)e.InnerException); Trace.WriteLine("Socket exception: " + se.ToString()); Trace.WriteLine("Error code: " + se.ErrorCode + " SocketErrorCode: " + se.SocketErrorCode); } } finally { ns.Close(); client.Close(); if (this.HTTPResponse.mediaStream != null) this.HTTPResponse.mediaStream.Close(); } }
/// <summary> /// Processes actually the HTTP Request. /// </summary> /// <remarks>Documented by Dev10, 2008-08-07</remarks> public void Process() { myReadBuffer = new byte[client.ReceiveBufferSize]; String myCompleteMessage = ""; int numberOfBytesRead = 0; #if DEBUG && debug_output Debug.WriteLine("Connection accepted. Buffer: " + client.ReceiveBufferSize.ToString()); #endif NetworkStream ns = client.GetStream(); string hValue = ""; string hKey = ""; try { // binary data buffer index int bfndx = 0; // Incoming message may be larger than the buffer size. do { numberOfBytesRead = ns.Read(myReadBuffer, 0, myReadBuffer.Length); myCompleteMessage = String.Concat(myCompleteMessage, Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead)); // read buffer index int ndx = 0; do { switch (ParserState) { case RState.METHOD: if (myReadBuffer[ndx] != ' ') { HTTPRequest.Method += (char)myReadBuffer[ndx++]; } else { ndx++; ParserState = RState.URL; } break; case RState.URL: if (myReadBuffer[ndx] == '?') { ndx++; hKey = ""; HTTPRequest.Execute = true; HTTPRequest.Args = new Hashtable(); ParserState = RState.URLPARM; } else if (myReadBuffer[ndx] != ' ') { HTTPRequest.URL += (char)myReadBuffer[ndx++]; } else { ndx++; HTTPRequest.URL = Uri.UnescapeDataString(HTTPRequest.URL); ParserState = RState.VERSION; } break; case RState.URLPARM: if (myReadBuffer[ndx] == '=') { ndx++; hValue = ""; ParserState = RState.URLVALUE; } else if (myReadBuffer[ndx] == ' ') { ndx++; HTTPRequest.URL = Uri.UnescapeDataString(HTTPRequest.URL); ParserState = RState.VERSION; } else { hKey += (char)myReadBuffer[ndx++]; } break; case RState.URLVALUE: if (myReadBuffer[ndx] == '&') { ndx++; hKey = Uri.UnescapeDataString(hKey); hValue = Uri.UnescapeDataString(hValue); HTTPRequest.Args[hKey] = HTTPRequest.Args[hKey] != null ? HTTPRequest.Args[hKey] + ", " + hValue : hValue; hKey = ""; ParserState = RState.URLPARM; } else if (myReadBuffer[ndx] == ' ') { ndx++; hKey = Uri.UnescapeDataString(hKey); hValue = Uri.UnescapeDataString(hValue); HTTPRequest.Args[hKey] = HTTPRequest.Args[hKey] != null ? HTTPRequest.Args[hKey] + ", " + hValue : hValue; HTTPRequest.URL = Uri.UnescapeDataString(HTTPRequest.URL); ParserState = RState.VERSION; } else { hValue += (char)myReadBuffer[ndx++]; } break; case RState.VERSION: if (myReadBuffer[ndx] == '\r') { ndx++; } else if (myReadBuffer[ndx] != '\n') { HTTPRequest.Version += (char)myReadBuffer[ndx++]; } else { ndx++; hKey = ""; HTTPRequest.Headers = new Hashtable(); ParserState = RState.HEADERKEY; } break; case RState.HEADERKEY: if (myReadBuffer[ndx] == '\r') { ndx++; } else if (myReadBuffer[ndx] == '\n') { ndx++; if (HTTPRequest.Headers["Content-Length"] != null) { HTTPRequest.BodySize = Convert.ToInt32(HTTPRequest.Headers["Content-Length"]); this.HTTPRequest.BodyData = new byte[this.HTTPRequest.BodySize]; ParserState = RState.BODY; } else { ParserState = RState.OK; } } else if (myReadBuffer[ndx] == ':') { ndx++; } else if (myReadBuffer[ndx] != ' ') { hKey += (char)myReadBuffer[ndx++]; } else { ndx++; hValue = ""; ParserState = RState.HEADERVALUE; } break; case RState.HEADERVALUE: if (myReadBuffer[ndx] == '\r') { ndx++; } else if (myReadBuffer[ndx] != '\n') { hValue += (char)myReadBuffer[ndx++]; } else { ndx++; if (!HTTPRequest.Headers.ContainsKey(hKey)) { HTTPRequest.Headers.Add(hKey, hValue); } hKey = ""; ParserState = RState.HEADERKEY; } break; case RState.BODY: // Append to request BodyData Array.Copy(myReadBuffer, ndx, this.HTTPRequest.BodyData, bfndx, numberOfBytesRead - ndx); bfndx += numberOfBytesRead - ndx; ndx = numberOfBytesRead; if (this.HTTPRequest.BodySize <= bfndx) { ParserState = RState.OK; } break; default: ndx++; break; } }while (ndx < numberOfBytesRead); }while (ns.DataAvailable); #if DEBUG && debug_output // Print out the received message to the console. Debug.WriteLine("Media server received request: " + Environment.NewLine + myCompleteMessage); #endif //Build up the HTTPResponse HTTPResponse.version = "HTTP/1.1"; if (ParserState != RState.OK) { HTTPResponse.status = (int)RespState.BAD_REQUEST; } else { HTTPResponse.status = (int)RespState.OK; } this.HTTPResponse.Headers = new Hashtable(); this.HTTPResponse.Headers.Add("Server", Parent.Name); this.HTTPResponse.Headers.Add("Date", DateTime.Now.ToString("r")); this.HTTPResponse.Headers.Add("Cache-Control", "no-cache"); this.HTTPResponse.Headers.Add("Pragma", "no-cache"); this.HTTPResponse.Headers.Add("Expires", "-1"); #if DEBUG && debug_output //Call the overriden SubClass Method to complete the HTTPResponse Content Debug.WriteLine("Preparing reponse."); #endif this.Parent.OnResponse(ref this.HTTPRequest, ref this.HTTPResponse); //Create the Header String string HeadersString = this.HTTPResponse.version + " " + this.Parent.respStatus[this.HTTPResponse.status] + "\r\n"; foreach (DictionaryEntry Header in this.HTTPResponse.Headers) { HeadersString += Header.Key + ": " + Header.Value + "\r\n"; } HeadersString += "\r\n"; byte[] bHeadersString = Encoding.ASCII.GetBytes(HeadersString); #if DEBUG && debug_output // Send headers Debug.WriteLine("Response headers: " + Environment.NewLine + HeadersString); #endif ns.Write(bHeadersString, 0, bHeadersString.Length); ns.Flush(); // Send body (File) if (this.HTTPResponse.mediaStream != null) { using (this.HTTPResponse.mediaStream) { byte[] b = new byte[client.SendBufferSize]; int bytesRead; int totalBytes = 0; int totalLength = Convert.ToInt32(this.HTTPResponse.mediaStream.Length); while ((bytesRead = this.HTTPResponse.mediaStream.Read(b, 0, b.Length)) > 0) { ns.Write(b, 0, bytesRead); totalBytes += bytesRead; #if DEBUG && debug_output Debug.WriteLine(string.Format("Sent {0:0,0} / {1:0,0} KBytes ({2:0.0%}).", 1.0 * totalBytes / 1024, 1.0 * totalLength / 1024, 1.0 * totalBytes / totalLength)); #endif } ns.Flush(); this.HTTPResponse.mediaStream.Close(); } } } catch (Exception e) { if (e is WebException) { Trace.WriteLine("A Webexception in CsHTTPRequest was thrown. URI: " + ((WebException)e).Response.ResponseUri.ToString() + Environment.NewLine + e.ToString()); } else { Trace.WriteLine("An Exception in CsHTTPRequest was thrown: " + e.ToString()); } if (e.InnerException != null && e.InnerException is SocketException) { SocketException se = ((SocketException)e.InnerException); Trace.WriteLine("Socket exception: " + se.ToString()); Trace.WriteLine("Error code: " + se.ErrorCode + " SocketErrorCode: " + se.SocketErrorCode); } } finally { ns.Close(); client.Close(); if (this.HTTPResponse.mediaStream != null) { this.HTTPResponse.mediaStream.Close(); } } }
public void Process() { myReadBuffer = new byte[client.ReceiveBufferSize]; String myCompleteMessage = ""; int numberOfBytesRead = 0; //Connection accepted. Buffering... NetworkStream ns = client.GetStream(); string hValue = ""; string hKey = ""; try { // binary data buffer index int bfndx = 0; // Incoming message may be larger than the buffer size. do { numberOfBytesRead = ns.Read(myReadBuffer, 0, myReadBuffer.Length); myCompleteMessage = String.Concat(myCompleteMessage, Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead)); // read buffer index int ndx = 0; do { switch ( ParserState ) { case RState.METHOD: if (myReadBuffer[ndx] != ' ') _HTTPRequest.Method += (char)myReadBuffer[ndx++]; else { ndx++; ParserState = RState.URL; } break; case RState.URL: if (myReadBuffer[ndx] == '?') { ndx++; hKey = ""; _HTTPRequest.Execute = true; _HTTPRequest.Args = new Hashtable(); ParserState = RState.URLPARM; } else if (myReadBuffer[ndx] != ' ') _HTTPRequest.URL += (char)myReadBuffer[ndx++]; else { ndx++; _HTTPRequest.URL = HttpUtility.UrlDecode(_HTTPRequest.URL); ParserState = RState.VERSION; } break; case RState.URLPARM: if (myReadBuffer[ndx] == '=') { ndx++; hValue=""; ParserState = RState.URLVALUE; } else if (myReadBuffer[ndx] == ' ') { ndx++; _HTTPRequest.URL = HttpUtility.UrlDecode(_HTTPRequest.URL); ParserState = RState.VERSION; } else { hKey += (char)myReadBuffer[ndx++]; } break; case RState.URLVALUE: if (myReadBuffer[ndx] == '&') { ndx++; hKey=HttpUtility.UrlDecode(hKey); hValue=HttpUtility.UrlDecode(hValue); _HTTPRequest.Args[hKey] = _HTTPRequest.Args[hKey] != null ? _HTTPRequest.Args[hKey] + ", " + hValue : hValue; hKey=""; ParserState = RState.URLPARM; } else if (myReadBuffer[ndx] == ' ') { ndx++; hKey=HttpUtility.UrlDecode(hKey); hValue=HttpUtility.UrlDecode(hValue); _HTTPRequest.Args[hKey] = _HTTPRequest.Args[hKey] != null ? _HTTPRequest.Args[hKey] + ", " + hValue : hValue; _HTTPRequest.URL = HttpUtility.UrlDecode(_HTTPRequest.URL); ParserState = RState.VERSION; } else { hValue += (char)myReadBuffer[ndx++]; } break; case RState.VERSION: if (myReadBuffer[ndx] == '\r') ndx++; else if (myReadBuffer[ndx] != '\n') _HTTPRequest.Version += (char)myReadBuffer[ndx++]; else { ndx++; hKey = ""; _HTTPRequest.Headers = new Hashtable(); ParserState = RState.HEADERKEY; } break; case RState.HEADERKEY: if (myReadBuffer[ndx] == '\r') ndx++; else if (myReadBuffer[ndx] == '\n') { ndx++; if (_HTTPRequest.Headers["Content-Length"] != null) { _HTTPRequest.BodySize = Convert.ToInt32(_HTTPRequest.Headers["Content-Length"]); this._HTTPRequest.BodyData = new byte[this._HTTPRequest.BodySize]; ParserState = RState.BODY; } else ParserState = RState.OK; } else if (myReadBuffer[ndx] == ':') ndx++; else if (myReadBuffer[ndx] != ' ') hKey += (char)myReadBuffer[ndx++]; else { ndx++; hValue = ""; ParserState = RState.HEADERVALUE; } break; case RState.HEADERVALUE: if (myReadBuffer[ndx] == '\r') ndx++; else if (myReadBuffer[ndx] != '\n') hValue += (char)myReadBuffer[ndx++]; else { ndx++; _HTTPRequest.Headers.Add(hKey, hValue); hKey = ""; ParserState = RState.HEADERKEY; } break; case RState.BODY: // Append to request BodyData Array.Copy(myReadBuffer, ndx, this._HTTPRequest.BodyData, bfndx, numberOfBytesRead - ndx); bfndx += numberOfBytesRead - ndx; ndx = numberOfBytesRead; if ( this._HTTPRequest.BodySize <= bfndx) { ParserState = RState.OK; } break; //default: // ndx++; // break; } } while(ndx < numberOfBytesRead); } while(ns.DataAvailable); _HTTPResponse.version = "HTTP/1.1"; if (ParserState != RState.OK) _HTTPResponse.status = (int)RespState.BAD_REQUEST; else _HTTPResponse.status = (int)RespState.OK; this._HTTPResponse.Headers = new Hashtable(); this._HTTPResponse.Headers.Add("Server", Parent.Name); this._HTTPResponse.Headers.Add("Date", DateTime.Now.ToString("r")); HTTPRequestHandler.OnRequest(this, ref ns); } catch (Exception e) { //Exception! Console.WriteLine(e.Message); } finally { ns.Close(); client.Close(); if (this._HTTPResponse.fs != null) this._HTTPResponse.fs.Close(); Thread.CurrentThread.Abort(); } }
private void strParsing(string str, byte[] myReadBuffer, int numberOfBytesRead) { // read buffer index int ndx = 0; do { switch (ParserState) { case RState.METHOD: if (myReadBuffer[ndx] != ' ') HTTPRequest.Method += (char)myReadBuffer[ndx++]; else { ndx++; ParserState = RState.URL; } break; case RState.URL: if (myReadBuffer[ndx] == '?') { ndx++; hKey = ""; HTTPRequest.Execute = true; HTTPRequest.Args = new Hashtable(); ParserState = RState.URLPARM; } else if (myReadBuffer[ndx] != ' ') HTTPRequest.URL += (char)myReadBuffer[ndx++]; else { ndx++; HTTPRequest.URL = HttpUtility.UrlDecode(HTTPRequest.URL); ParserState = RState.VERSION; } break; case RState.URLPARM: if (myReadBuffer[ndx] == '=') { ndx++; hValue = ""; ParserState = RState.URLVALUE; } else if (myReadBuffer[ndx] == ' ') { ndx++; HTTPRequest.URL = HttpUtility.UrlDecode(HTTPRequest.URL); ParserState = RState.VERSION; } else { hKey += (char)myReadBuffer[ndx++]; } break; case RState.URLVALUE: if (myReadBuffer[ndx] == '&') { ndx++; hKey = HttpUtility.UrlDecode(hKey); hValue = HttpUtility.UrlDecode(hValue); HTTPRequest.Args[hKey] = HTTPRequest.Args[hKey] != null ? HTTPRequest.Args[hKey] + ", " + hValue : hValue; hKey = ""; ParserState = RState.URLPARM; } else if (myReadBuffer[ndx] == ' ') { ndx++; hKey = HttpUtility.UrlDecode(hKey); hValue = HttpUtility.UrlDecode(hValue); HTTPRequest.Args[hKey] = HTTPRequest.Args[hKey] != null ? HTTPRequest.Args[hKey] + ", " + hValue : hValue; HTTPRequest.URL = HttpUtility.UrlDecode(HTTPRequest.URL); ParserState = RState.VERSION; } else { hValue += (char)myReadBuffer[ndx++]; } break; case RState.VERSION: if (myReadBuffer[ndx] == '\r') ndx++; else if (myReadBuffer[ndx] != '\n') HTTPRequest.Version += (char)myReadBuffer[ndx++]; else { ndx++; hKey = ""; HTTPRequest.Headers = new Hashtable(); ParserState = RState.HEADERKEY; } break; case RState.HEADERKEY: if (myReadBuffer[ndx] == '\r') ndx++; else if (myReadBuffer[ndx] == '\n') { ndx++; if (HTTPRequest.Headers["Content-Length"] != null) { HTTPRequest.BodySize = Convert.ToInt32(HTTPRequest.Headers["Content-Length"]); this.HTTPRequest.BodyData = new byte[this.HTTPRequest.BodySize]; ParserState = RState.BODY; } else ParserState = RState.OK; } else if (myReadBuffer[ndx] == ':') ndx++; else if (myReadBuffer[ndx] != ' ') hKey += (char)myReadBuffer[ndx++]; else { ndx++; hValue = ""; ParserState = RState.HEADERVALUE; } break; case RState.HEADERVALUE: if (myReadBuffer[ndx] == '\r') ndx++; else if (myReadBuffer[ndx] != '\n') hValue += (char)myReadBuffer[ndx++]; else { ndx++; if (!HTTPRequest.Headers.Contains(hKey)) HTTPRequest.Headers.Add(hKey, hValue); hKey = ""; ParserState = RState.HEADERKEY; } break; case RState.BODY: // Append to request BodyData Array.Copy(myReadBuffer, ndx, this.HTTPRequest.BodyData, bfndx, numberOfBytesRead - ndx); bfndx += numberOfBytesRead - ndx; ndx = numberOfBytesRead; if (this.HTTPRequest.BodySize <= bfndx) { ParserState = RState.OK; } break; //default: // ndx++; // break; } } while (ndx < numberOfBytesRead); }