示例#1
0
        private int GetMessageHeaders(ref string stringMessage, ref HTTP_Message message)
        {
            int headersEnding;
            int startingLineEnding = stringMessage.IndexOf(HEADERS_BLOCK_SEPARATOR);

            if (startingLineEnding != PARSING_FAILED_CODE)
            {
                headersEnding = stringMessage.IndexOf(BODY_SEPARATOR);
                if (headersEnding != PARSING_FAILED_CODE)
                {
                    int             headersBegining = startingLineEnding + 2;
                    Regex           regex           = new Regex(RegularExpressions["messageHeaders"], RegexOptions.Multiline);
                    MatchCollection matchCollection =
                        regex.Matches(stringMessage.Substring(headersBegining, headersEnding - headersBegining));
                    if (matchCollection.Count > 0)
                    {
                        stringMessage   = stringMessage.Substring(0, headersBegining - 2);
                        message.Headers = new HTTP_Message_Headers();
                        foreach (Match match in matchCollection)
                        {
                            string name = match.Groups["name"].Value;
                            if (!message.Headers.HeadersDictionary.ContainsKey(name))
                            {
                                if (name.Trim().ToLower() == "host")
                                {
                                    string host;
                                    int    port;
                                    string headerValue = match.Groups["value"].Value.Trim(HEADERS_SEPARATOR);
                                    ParseHeaderHost(headerValue, out host, out port);
                                    message.Headers.HeadersDictionary.Add(name, new Header_Host(headerValue, host, port));
                                }
                                else if (name.Trim().ToLower() == "content-type")
                                {
                                    string headerValue = match.Groups["value"].Value.Trim(HEADERS_SEPARATOR);
                                    if (!string.IsNullOrEmpty(headerValue))
                                    {
                                        string charset;
                                        ParseHeaderContentType(headerValue, out charset);
                                        message.Headers.HeadersDictionary.Add(name, new Header_ContentType(headerValue, charset));
                                    }
                                }
                                else
                                {
                                    message.Headers.HeadersDictionary.Add(name, new Header(match.Groups["value"].Value.Trim(HEADERS_SEPARATOR)));
                                }
                            }
                        }
                    }
                    else
                    {
                        headersEnding = MESSAGE_HAS_NO_HEADERS_CODE;
                    }
                }
            }
            else
            {
                headersEnding = MESSAGE_HAS_NO_HEADERS_CODE;
            }
            return(headersEnding);
        }
 private bool LogIn(Connection connection, HTTP_Message request)
 {
     if (Server.NEED_AUTHORIZATION)
     {
         if (!request.Headers.HeadersDictionary.ContainsKey("Authorization"))
         {
             Server.AnswerToClient(CreateHTTPErrorMessage(401, "Unauthorized"), connection.ClientSocket);
             return(false);
         }
         else
         {
             string authorizationHeader = Encoding.UTF8.GetString(Convert.FromBase64String(request.Headers.HeadersDictionary["Authorization"].Value.Replace("Basic ", "")));
             string login    = authorizationHeader.Split(":".ToCharArray())[0];
             string password = authorizationHeader.Split(":".ToCharArray())[1];
             if (!Server.VarifyAuthorizationData(login, password))
             {
                 Server.AnswerToClient(CreateHTTPErrorMessage(401, "Unauthorized"), connection.ClientSocket);
                 return(false);
             }
             else
             {
                 return(true);
             }
         }
     }
     else
     {
         return(true);
     }
 }
        private void AppendResponse(HTTP_Message response, int responseKey, Connection connection)
        {
            switch (int.Parse(response.StartingLine.MiddlePart))
            {
            case 400:
            case 401:
            case 403:
            case 404:
            case 501:
                Server.AnswerToClient(CreateHTTPErrorMessage(int.Parse(response.StartingLine.MiddlePart), response.StartingLine.EndingPart), connection.ClientSocket);
                break;

            default:
                if (Server.APPEND_HTML_PAGES)
                {
                    if (response.Headers.HeadersDictionary.ContainsKey("Content-Type") && (response.Headers.HeadersDictionary["Content-Type"].Value == "text/html"))
                    {
                        string body = Server.HTTPParser.GetMessageBodyAsString(responseKey);
                        body = Regex.Replace(body, "<title>(?<title>.*?)</title>", "<title>ProxyServer - $1</title>");
                        body = Regex.Replace(body, "(<body.*?>)", "$1<div style='height:20px;width:100%;background-color:black;color:white;font-weight:bold;text-align:center;'>Proxy Server</div>");
                        Server.HTTPParser.ChangeBody(body, responseKey);
                    }
                    Server.AnswerToClient(response.SourceMessage, connection.ClientSocket);
                }
                break;
            }
        }
示例#4
0
 private void SetEncoding(HTTP_Message message, out Encoding encoding)
 {
     encoding = Encoding.UTF8;
     if (message.Headers.HeadersDictionary.ContainsKey("Content-Type"))
     {
         var contentType = message.Headers.HeadersDictionary["Content-Type"] as Header_ContentType;
         if (!String.IsNullOrEmpty(contentType?.Charset))
         {
             try
             {
                 encoding = Encoding.GetEncoding(contentType?.Charset);
             }
             catch
             { }
         }
     }
 }
示例#5
0
        private bool ParseCommonPart(byte[] sourceMessage, out string messageStartingLine, out int newMessageKey)
        {
            messageStartingLine = null;
            string       stringMessage = GetStringFromBytes(sourceMessage, Encoding.ASCII.EncodingName);
            HTTP_Message newMessage    = new HTTP_Message(sourceMessage);

            newMessageKey = ParsedMessages.AddElement(newMessage);
            int messageHeadersEnding = GetMessageHeaders(ref stringMessage, ref newMessage);

            if (messageHeadersEnding != MESSAGE_HAS_NO_HEADERS_CODE)
            {
                messageStartingLine = stringMessage;
                if (messageHeadersEnding != PARSING_FAILED_CODE)
                {
                    newMessage.Body = GetMessageBody(newMessageKey, messageHeadersEnding);
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }