Exemple #1
0
        private static void ReadMediaTypeParameter(string text, ref int textIndex, ref KeyValuePair <string, string>[] parameters)
        {
            int startIndex = textIndex;

            if (ReadToken(text, ref textIndex))
            {
                throw Error.HttpHeaderFailure(400, Strings.HttpProcessUtility_MediaTypeMissingValue);
            }
            string parameterName = text.Substring(startIndex, textIndex - startIndex);

            if (text[textIndex] != '=')
            {
                throw Error.HttpHeaderFailure(400, Strings.HttpProcessUtility_MediaTypeMissingValue);
            }
            textIndex++;
            string str2 = ReadQuotedParameterValue(parameterName, text, ref textIndex);

            if (parameters == null)
            {
                parameters = new KeyValuePair <string, string> [1];
            }
            else
            {
                KeyValuePair <string, string>[] destinationArray = new KeyValuePair <string, string> [parameters.Length + 1];
                Array.Copy(parameters, destinationArray, parameters.Length);
                parameters = destinationArray;
            }
            parameters[parameters.Length - 1] = new KeyValuePair <string, string>(parameterName, str2);
        }
        private static MediaType ReadMediaType(string text)
        {
            Debug.Assert(text != null, "text != null");

            string type;
            string subType;
            int    textIndex = 0;

            ReadMediaTypeAndSubtype(text, ref textIndex, out type, out subType);

            KeyValuePair <string, string>[] parameters = null;
            while (!SkipWhitespace(text, ref textIndex))
            {
                if (text[textIndex] != ';')
                {
                    throw Error.HttpHeaderFailure(400, Strings.HttpProcessUtility_MediaTypeRequiresSemicolonBeforeParameter);
                }

                textIndex++;
                if (SkipWhitespace(text, ref textIndex))
                {
                    break;
                }

                ReadMediaTypeParameter(text, ref textIndex, ref parameters);
            }

            return(new MediaType(type, subType, parameters));
        }
        private static void ReadMediaTypeAndSubtype(string text, ref int textIndex, out string type, out string subType)
        {
            Debug.Assert(text != null, "text != null");
            int textStart = textIndex;

            if (ReadToken(text, ref textIndex))
            {
                throw Error.HttpHeaderFailure(400, Strings.HttpProcessUtility_MediaTypeUnspecified);
            }

            if (text[textIndex] != '/')
            {
                throw Error.HttpHeaderFailure(400, Strings.HttpProcessUtility_MediaTypeRequiresSlash);
            }

            type = text.Substring(textStart, textIndex - textStart);
            textIndex++;

            int subTypeStart = textIndex;

            ReadToken(text, ref textIndex);

            if (textIndex == subTypeStart)
            {
                throw Error.HttpHeaderFailure(400, Strings.HttpProcessUtility_MediaTypeRequiresSubType);
            }

            subType = text.Substring(subTypeStart, textIndex - subTypeStart);
        }
        private static Encoding EncodingFromName(string name)
        {
            if (name == null)
            {
                return(MissingEncoding);
            }

            name = name.Trim();
            if (name.Length == 0)
            {
                return(MissingEncoding);
            }
            else
            {
                try
                {
#if ASTORIA_LIGHT
                    return(Encoding.UTF8);
#else
                    return(Encoding.GetEncoding(name));
#endif
                }
                catch (ArgumentException)
                {
                    throw Error.HttpHeaderFailure(400, Strings.HttpProcessUtility_EncodingNotSupported(name));
                }
            }
        }
        private static string ReadQuotedParameterValue(string parameterName, string headerText, ref int textIndex)
        {
            StringBuilder parameterValue = new StringBuilder();

            bool valueIsQuoted = false;

            if (textIndex < headerText.Length)
            {
                if (headerText[textIndex] == '\"')
                {
                    textIndex++;
                    valueIsQuoted = true;
                }
            }

            while (textIndex < headerText.Length)
            {
                char currentChar = headerText[textIndex];

                if (currentChar == '\\' || currentChar == '\"')
                {
                    if (!valueIsQuoted)
                    {
                        throw Error.HttpHeaderFailure(400, Strings.HttpProcessUtility_EscapeCharWithoutQuotes(parameterName));
                    }

                    textIndex++;

                    if (currentChar == '\"')
                    {
                        valueIsQuoted = false;
                        break;
                    }

                    if (textIndex >= headerText.Length)
                    {
                        throw Error.HttpHeaderFailure(400, Strings.HttpProcessUtility_EscapeCharAtEnd(parameterName));
                    }

                    currentChar = headerText[textIndex];
                }
                else
                if (!IsHttpToken(currentChar))
                {
                    break;
                }

                parameterValue.Append(currentChar);
                textIndex++;
            }

            if (valueIsQuoted)
            {
                throw Error.HttpHeaderFailure(400, Strings.HttpProcessUtility_ClosingQuoteNotFound(parameterName));
            }

            return(parameterValue.ToString());
        }
Exemple #6
0
        internal static KeyValuePair <string, string>[] ReadContentType(string contentType, out string mime, out Encoding encoding)
        {
            if (string.IsNullOrEmpty(contentType))
            {
                throw Error.HttpHeaderFailure(400, Strings.HttpProcessUtility_ContentTypeMissing);
            }
            MediaType type = ReadMediaType(contentType);

            mime     = type.MimeType;
            encoding = type.SelectEncoding();
            return(type.Parameters);
        }
Exemple #7
0
        private static string ReadQuotedParameterValue(string parameterName, string headerText, ref int textIndex)
        {
            StringBuilder builder = new StringBuilder();
            bool          flag    = false;

            if ((textIndex < headerText.Length) && (headerText[textIndex] == '"'))
            {
                textIndex++;
                flag = true;
            }
            while (textIndex < headerText.Length)
            {
                char c = headerText[textIndex];
                if ((c == '\\') || (c == '"'))
                {
                    if (!flag)
                    {
                        throw Error.HttpHeaderFailure(400, Strings.HttpProcessUtility_EscapeCharWithoutQuotes(parameterName));
                    }
                    textIndex++;
                    if (c == '"')
                    {
                        flag = false;
                        break;
                    }
                    if (textIndex >= headerText.Length)
                    {
                        throw Error.HttpHeaderFailure(400, Strings.HttpProcessUtility_EscapeCharAtEnd(parameterName));
                    }
                    c = headerText[textIndex];
                }
                else if (!IsHttpToken(c))
                {
                    break;
                }
                builder.Append(c);
                textIndex++;
            }
            if (flag)
            {
                throw Error.HttpHeaderFailure(400, Strings.HttpProcessUtility_ClosingQuoteNotFound(parameterName));
            }
            return(builder.ToString());
        }
Exemple #8
0
        private static void ReadMediaTypeAndSubtype(string text, ref int textIndex, out string type, out string subType)
        {
            int startIndex = textIndex;

            if (ReadToken(text, ref textIndex))
            {
                throw Error.HttpHeaderFailure(400, Strings.HttpProcessUtility_MediaTypeUnspecified);
            }
            if (text[textIndex] != '/')
            {
                throw Error.HttpHeaderFailure(400, Strings.HttpProcessUtility_MediaTypeRequiresSlash);
            }
            type = text.Substring(startIndex, textIndex - startIndex);
            textIndex++;
            int num2 = textIndex;

            ReadToken(text, ref textIndex);
            if (textIndex == num2)
            {
                throw Error.HttpHeaderFailure(400, Strings.HttpProcessUtility_MediaTypeRequiresSubType);
            }
            subType = text.Substring(num2, textIndex - num2);
        }
Exemple #9
0
        private static Encoding EncodingFromName(string name)
        {
            Encoding encoding;

            if (name == null)
            {
                return(MissingEncoding);
            }
            name = name.Trim();
            if (name.Length == 0)
            {
                return(MissingEncoding);
            }
            try
            {
                encoding = Encoding.GetEncoding(name);
            }
            catch (ArgumentException)
            {
                throw Error.HttpHeaderFailure(400, Strings.HttpProcessUtility_EncodingNotSupported(name));
            }
            return(encoding);
        }