Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C# by Sergej ( 14 years ago )
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace Webserver
{
    public class Request
    {
        private Socket _socket;


        public Request(Socket s)
        {
            _socket = s;
            Header = new Dictionary<string, string>();
        }

        public string Method { get; private set; }
        public Url Url { get; private set; }
        public Dictionary<string, string> Header { get; private set; }

        public bool Parse()
        {
            NetworkStream stream = new NetworkStream(_socket);
            StreamReader sr = new StreamReader(stream);

            while (!sr.EndOfStream)
            {
                string line = sr.ReadLine();

                if (string.IsNullOrEmpty(line))
                    break;
                
                if (string.IsNullOrEmpty(Method))
                {
                    var buffer = line.Split(' ');

                    if (buffer.Length == 3)
                    {
                        Method = buffer[0];
                        Url = new Url(buffer[1]);
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    var buffer = line.Split(new[] { ':' }, 2);

                    if (buffer.Length == 2)
                    {
                        Header[buffer[0]] = buffer[1];
                    }
                    else
                    {
                        return false;
                    }
                }

                Console.WriteLine(line);
            }

            return IsValid;
        }

        private bool IsValid
        {
            get
            {
                return !string.IsNullOrEmpty(Method);
            }
        }

    }
}

 

Revise this Paste

Children: 55726
Your Name: Code Language: