Content-type
Inheritance: IHeader
Exemple #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Response"/> class.
 /// </summary>
 /// <param name="context">Context that the response will be sent through.</param>
 /// <param name="request">Request that the response is for.</param>
 /// <exception cref="FormatException">Version must start with 'HTTP/'</exception>
 public Response(IHttpContext context, IRequest request)
 {
     _context = context;
     HttpVersion = request.HttpVersion;
     Reason = "Made by Jonas Gauffin";
     Status = HttpStatusCode.OK;
     ContentType = new ContentTypeHeader("text/html");
     Encoding = request.Encoding;
     _headers = CreateHeaderCollection();
 }
Exemple #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Response"/> class.
        /// </summary>
        /// <param name="version">HTTP Version.</param>
        /// <param name="code">HTTP status code.</param>
        /// <param name="reason">Why the status code was selected.</param>
        /// <exception cref="FormatException">Version must start with 'HTTP/'</exception>
        public Response(string version, HttpStatusCode code, string reason)
        {
            if (!version.StartsWith("HTTP/"))
                throw new FormatException("Version must start with 'HTTP/'");

            Status = code;
            Reason = reason;
            HttpVersion = version;
            ContentType = new ContentTypeHeader("text/html");
            Encoding = Encoding.UTF8;
            _headers = CreateHeaderCollection();
        }
Exemple #3
0
        public void FormTest()
        {
            FileStream stream = new FileStream("C:\\temp\\bodymime.mime", FileMode.Open);
            MultiPartDecoder  decoder = new MultiPartDecoder();
            var header = new ContentTypeHeader("multipart/form-data");
            header.Parameters.Add("boundary", "----WebKitFormBoundaryQsuJaNmu3FVqrYwp");
            var data = decoder.Decode(stream, header, Encoding.Default);
            if (data.Files.Count > 0)
            {
                
            }

        }
Exemple #4
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream">Stream containing the content</param>
        /// <param name="contentType">Content type header</param>
        /// <param name="encoding">Stream encoding</param>
        /// <returns>Collection with all parameters.</returns>
        /// <exception cref="FormatException">Body format is invalid for the specified content type.</exception>
        /// <exception cref="InternalServerException">Failed to read all bytes from body stream.</exception>
        public DecodedData Decode(Stream stream, ContentTypeHeader contentType, Encoding encoding)
        {
            if (stream == null || stream.Length == 0)
                return null;

            if (encoding == null)
                encoding = Encoding.UTF8;

            try
            {
                var content = new byte[stream.Length];
                int bytesRead = stream.Read(content, 0, content.Length);
                if (bytesRead != content.Length)
                    throw new InternalServerException("Failed to read all bytes from body stream.");

                return new DecodedData {Parameters = UrlParser.Parse(new BufferReader(content, encoding))};
            }
            catch (ArgumentException err)
            {
                throw new FormatException(err.Message, err);
            }
        }
        /// <summary>
        /// Decode body stream
        /// </summary>
        /// <param name="stream">Stream containing the content</param>
        /// <param name="contentType">Content type header</param>
        /// <param name="encoding">Stream encoding</param>
        /// <returns>Decoded data.</returns>
        /// <exception cref="FormatException">Body format is invalid for the specified content type.</exception>
        /// <exception cref="InternalServerException">Something unexpected failed.</exception>
        /// <exception cref="ArgumentNullException"><c>stream</c> is <c>null</c>.</exception>
        public DecodedData Decode(Stream stream, ContentTypeHeader contentType, Encoding encoding)
        {
            if (stream == null)
                throw new ArgumentNullException("stream");
            if (contentType == null)
                throw new ArgumentNullException("contentType");
            if (encoding == null)
                throw new ArgumentNullException("encoding");

            //multipart/form-data, boundary=AaB03x
            string boundry = contentType.Parameters["boundary"];
            if (boundry == null)
                throw new FormatException("Missing boundary in content type.");

            var multipart = new HttpMultipart(stream, boundry, encoding);

            var form = new DecodedData();
            /*
            FileStream stream1 = new FileStream("C:\\temp\\mimebody.tmp", FileMode.Create);
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            stream1.Write(bytes, 0, bytes.Length);
            stream1.Flush();
            stream1.Close();
            */

            HttpMultipart.Element element;
            while ((element = multipart.ReadNextElement()) != null)
            {
                if (string.IsNullOrEmpty(element.Name))
                    throw new FormatException("Error parsing request. Missing value name.\nElement: " + element);

                if (!string.IsNullOrEmpty(element.Filename))
                {
                    if (string.IsNullOrEmpty(element.ContentType))
                        throw new FormatException("Error parsing request. Value '" + element.Name +
                                                  "' lacks a content type.");

                    // Read the file data
                    var buffer = new byte[element.Length];
                    stream.Seek(element.Start, SeekOrigin.Begin);
                    stream.Read(buffer, 0, (int) element.Length);

                    // Generate a filename
                    string originalFileName = element.Filename;
                    string internetCache = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);

                    // if the internet path doesn't exist, assume mono and /var/tmp
                    string path = string.IsNullOrEmpty(internetCache)
                                      ? Path.Combine("var", "tmp")
                                      : Path.Combine(internetCache.Replace("\\\\", "\\"), "tmp");

                    element.Filename = Path.Combine(path, Math.Abs(element.Filename.GetHashCode()) + ".tmp");

                    // If the file exists generate a new filename
                    while (File.Exists(element.Filename))
                        element.Filename = Path.Combine(path, Math.Abs(element.Filename.GetHashCode() + 1) + ".tmp");

                    if (!Directory.Exists(path))
                        Directory.CreateDirectory(path);

                    File.WriteAllBytes(element.Filename, buffer);

                    var file = new HttpFile
                                   {
                                       Name = element.Name,
                                       OriginalFileName = originalFileName,
                                       ContentType = element.ContentType,
                                       TempFileName = element.Filename
                                   };
                    form.Files.Add(file);
                }
                else
                {
                    var buffer = new byte[element.Length];
                    stream.Seek(element.Start, SeekOrigin.Begin);
                    stream.Read(buffer, 0, (int) element.Length);

                    form.Parameters.Add(HttpUtility.UrlDecode(element.Name), encoding.GetString(buffer));
                }
            }

            return form;
        }