<commit_before>﻿/*
     Copyright 2012 <NAME> <<EMAIL>>

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace SimpleHttpServer
{
    public static class RequestExtensions
    {
        public static string Content(this HttpListenerRequest req)
        {
            string result = string.Empty;

            if (req.ContentLength64 > 0)
            {
                var bytez = new byte[req.ContentLength64];
                req.InputStream.Read(bytez, 0, (int)req.ContentLength64);
                result = req.GetEncoding().GetString(bytez);
            }
            return result;
        }

        public static Encoding GetEncoding(this HttpListenerRequest req)
        {
            //todo check Content-Encoding an return the correct encoding
            return Encoding.UTF8;
        }

        public static string DumpHeader(this HttpListenerRequest req)
        {
            return string.Format("->->->->-> REQUEST <-<-<-<-<-" + Environment.NewLine +
                                   "->->->->-> Url: {0}" + Environment.NewLine +
                                   "->->->->-> From: {1}" + Environment.NewLine +
                                   "->->->->-> Method: {2}" + Environment.NewLine +<commit_msg>support for deflate/gzip encoding<commit_after>﻿/*
     Copyright 2012 <NAME> <<EMAIL>>

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace SimpleHttpServer
{
    public static class RequestExtensions
    {
        public static string Content(this HttpListenerRequest req)
        {
            string result = string.Empty;

            if (req.ContentLength64 > 0)
            {
                var bytez = new byte[req.ContentLength64];
                req.InputStream.Read(bytez, 0, (int)req.ContentLength64);
                result = req.GetEncoding().GetString(bytez);
            }
            return result;
        }

        public static Encoding GetEncoding(this HttpListenerRequest req)
        {
            //we only support UTF encoded bodies
            return Encoding.UTF8;
        }

        public static Encoding GetAcceptCharset(this HttpListenerRequest req)
        {
            var value = req.Headers.Get("Accept-Charset");
            return GetEncodingFromHeader(value);
        }

        public static EncodingType GetAcceptEncoding(this HttpListenerRequest req)
        {
            EncodingType ret;
            var value = req.Headers.Get("Accept-Encoding");

            if (value.Contains("deflate"))
                ret = EncodingType.Deflate;
            else if(value.Contains("gzip"))
                ret = EncodingType.Gzip;
            else
                ret = EncodingType.Plain;

            return ret;
        }

        private static Encoding GetEncodingFromHeader(string value)
        {
            Encoding ret = null;
            if(value.Contains("UTF-8"))
            {
                ret = Encoding.UTF8;
            } else 
            {
                var splits = value.Split(',');

                foreach (var item in splits)
	            {
                        try 
	                    {	        
		                    ret = Encoding.GetEncoding(splits[0]);
                            continue;
	                    }
	                    catch (Exception)
	                    {	
		                
	                    }
	            }
             
                if(ret == null)
                    ret = Encoding.UTF8;            
            }

            return ret;
        }

        public static string DumpHeader(this HttpListenerRequest req)
        {
            return string.Format("->->->->-> REQUEST <-<-<-<-<-" + Environment.NewLine +
                                   "->->->->-> Url: {0}" + Environment.NewLine +
                                   "->->->->-> From: {1}" + Environment.NewLine +
                                   "->->->->-> Method: {2}" + Environment.NewLine +
{'fraction_non_alphanumeric': 0.09131619937694704, 'fraction_numerical': 0.008566978193146417, 'mean_word_length': 1.5918264379414733, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3857', 'n_tokens_mistral': 1309, 'n_tokens_neox': 1222, 'n_words': 382}