Beispiel #1
0
        public static HTTPRequest Parse(byte[] rawReq)
        {
            HTTPRequest result = null;

            byte[] headerRaw = ExtractHeader(rawReq);

            HTTPHeader header = HTTPHeader.Parse(headerRaw);

            if (header != null)
            {
                result         = new HTTPRequest();
                result.RawBody = new byte[rawReq.Length - headerRaw.Length];
                Array.Copy(rawReq, headerRaw.Length, result.RawBody, 0, rawReq.Length - headerRaw.Length);
                result.RawHeaderSize = headerRaw.Length;
                result.Header        = header;
            }

            return(result);
        }
Beispiel #2
0
        public static HTTPHeader Parse(byte[] rawHeader)
        {
            HTTPHeader header    = null;
            string     headerStr = ASCIIEncoding.GetEncoding(0).GetString(rawHeader);

            string[] strSplitors = new string[] { "\r\n" };
            string[] lines       = headerStr.Split(strSplitors, StringSplitOptions.RemoveEmptyEntries);
            if (lines.Length > 0)
            {
                char[]   charSplitors     = new char[] { ' ' };
                string[] commandLineItems = lines[0].Split(charSplitors, StringSplitOptions.RemoveEmptyEntries);
                if (commandLineItems.Length == 3)
                {
                    header                  = new HTTPHeader();
                    header.Method           = (HTTPRequestMethod)Enum.Parse(typeof(HTTPRequestMethod), commandLineItems[0]);
                    header.ResourceLocation = commandLineItems[1];
                    header.ProtocolVersion  = commandLineItems[2];

                    for (int i = 1; i < lines.Length; i++)
                    {
                        int index = lines[i].IndexOf(':');
                        if (index > 1)
                        {
                            if (index + 1 < lines[i].Length)
                            {
                                header.Headers.Add(lines[i].Substring(0, index), lines[i].Substring(index + 1));
                            }
                            else
                            {
                                header = null;
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }

            return(header);
        }
Beispiel #3
0
 public HTTPRequest()
 {
     Header = new HTTPHeader();
 }