public bool Authenticate(NameValueCollection requestHeaders, AddHeaderDelegate d, out HttpStatusCode statusCode)
        {
            if (requestHeaders["X-SecondLife-Shard"] != null)
            {
                statusCode = HttpStatusCode.Forbidden;
                return false;
            }

            statusCode = HttpStatusCode.OK;
            return true;
        }
Пример #2
0
        public bool Authenticate(NameValueCollection requestHeaders, AddHeaderDelegate d, out HttpStatusCode statusCode)
        {
            foreach (IServiceAuth auth in m_authentications)
            {
                if (!auth.Authenticate(requestHeaders, d, out statusCode))
                    return false;
            }

            statusCode = HttpStatusCode.OK;
            return true;
        }
Пример #3
0
        public bool Authenticate(NameValueCollection requestHeaders, AddHeaderDelegate d, out HttpStatusCode statusCode)
        {
//            Console.WriteLine("DisallowLlHttpRequest");

            if (requestHeaders["X-SecondLife-Shard"] != null)
            {
                statusCode = HttpStatusCode.Forbidden;
                return false;
            }

            statusCode = HttpStatusCode.OK;
            return true;
        }
Пример #4
0
		public static void ParseParameters (byte[] data, AddHeaderDelegate func, object userData)
		{
			int dataLength = data.Length;
			int offset = 0;
			int nlen, vlen;
			string name, value;
			//TODO: can encoding change?
			Encoding enc = Encoding.Default;

			while (offset < dataLength) {
				nlen = data [offset++];

				if (nlen >= 0x80) {
					nlen = ((0x7F & nlen) * 0x1000000)
						+ ((int)data [offset++]) * 0x10000
						+ ((int)data [offset++]) * 0x100
						+ ((int)data [offset++]);
				}

				vlen = data [offset++];

				if (vlen >= 0x80) {
					vlen = ((0x7F & vlen) * 0x1000000)
						+ ((int)data [offset++]) * 0x10000
						+ ((int)data [offset++]) * 0x100
						+ ((int)data [offset++]);
				}

				// Do a sanity check on the size of the data.
				if (offset + nlen + vlen > dataLength)
					throw new ArgumentOutOfRangeException ("offset");

				name = enc.GetString (data, offset, nlen);
				offset += nlen;
				value = enc.GetString (data, offset, vlen);
				offset += vlen;

				string header = ReformatHttpHeader (name);
				bool isHeader = !String.IsNullOrEmpty (header);

				//return value 'false' means stop further processing
				if (!func (isHeader? header : name, value, isHeader, userData))
					return;
			}
		}
 public bool Authenticate(NameValueCollection requestHeaders, AddHeaderDelegate d)
 {
     //m_log.DebugFormat("[HTTP BASIC AUTH]: Authenticate in {0}", remove_me);
     if (requestHeaders != null)
     {
         string value = requestHeaders.Get("Authorization");
         if (value != null)
         {
             value = value.Trim();
             if (value.StartsWith("Basic "))
             {
                 value = value.Replace("Basic ", string.Empty);
                 if (Authenticate(value))
                     return true;
             }
         }
     }
     d("WWW-Authenticate", "Basic realm = \"Asset Server\"");
     return false;
 }
Пример #6
0
 public bool Authenticate(NameValueCollection requestHeaders, AddHeaderDelegate d)
 {
     //m_log.DebugFormat("[HTTP BASIC AUTH]: Authenticate in {0}", remove_me);
     if (requestHeaders != null)
     {
         string value = requestHeaders.Get("Authorization");
         if (value != null)
         {
             value = value.Trim();
             if (value.StartsWith("Basic "))
             {
                 value = value.Replace("Basic ", string.Empty);
                 if (Authenticate(value))
                 {
                     return(true);
                 }
             }
         }
     }
     d("WWW-Authenticate", "Basic realm = \"Asset Server\"");
     return(false);
 }
        public bool Authenticate(NameValueCollection requestHeaders, AddHeaderDelegate d, out HttpStatusCode statusCode)
        {
//            m_log.DebugFormat("[HTTP BASIC AUTH]: Authenticate in {0}", "BasicHttpAuthentication");

            string value = requestHeaders.Get("Authorization");
            if (value != null)
            {
                value = value.Trim();
                if (value.StartsWith("Basic "))
                {
                    value = value.Replace("Basic ", string.Empty);
                    if (Authenticate(value))
                    {
                        statusCode = HttpStatusCode.OK;
                        return true;
                    }
                }
            }

            d("WWW-Authenticate", "Basic realm = \"OpenSim Server\"");

            statusCode = HttpStatusCode.Unauthorized;
            return false;
        }
Пример #8
0
        /// <summary>
        /// Decode the header block into header fields.
        /// </summary>
        /// <param name="input">Input.</param>
        /// <param name="headerListener">Header listener.</param>
        public void Decode(BinaryReader input, AddHeaderDelegate addHeaderDelegate)
        {
            while(input.BaseStream.Length - input.BaseStream.Position > 0) {
                switch(this.state) {
                case State.READ_HEADER_REPRESENTATION:
                    sbyte b = input.ReadSByte();
                    if (maxDynamicTableSizeChangeRequired && (b & 0xE0) != 0x20) {
                        // Encoder MUST signal maximum dynamic table size change
                        throw new IOException("max dynamic table size change required");
                    }
                    if (b < 0) {
                        // Indexed Header Field
                        index = b & 0x7F;
                        if (index == 0) {
                            throw new IOException("illegal index value (" + index + ")");
                        } else if (index == 0x7F) {
                            state = State.READ_INDEXED_HEADER;
                        } else {
                            this.IndexHeader(index, addHeaderDelegate);
                        }
                    } else if ((b & 0x40) == 0x40) {
                        // Literal Header Field with Incremental Indexing
                        indexType = HPackUtil.IndexType.INCREMENTAL;
                        index = b & 0x3F;
                        if (index == 0) {
                            state = State.READ_LITERAL_HEADER_NAME_LENGTH_PREFIX;
                        } else if (index == 0x3F) {
                            state = State.READ_INDEXED_HEADER_NAME;
                        } else {
                            // Index was stored as the prefix
                            this.ReadName(index);
                            state = State.READ_LITERAL_HEADER_VALUE_LENGTH_PREFIX;
                        }
                    } else if ((b & 0x20) == 0x20) {
                        // Dynamic Table Size Update
                        index = b & 0x1F;
                        if (index == 0x1F) {
                            state = State.READ_MAX_DYNAMIC_TABLE_SIZE;
                        } else {
                            this.SetDynamicTableSize(index);
                            state = State.READ_HEADER_REPRESENTATION;
                        }
                    } else {
                        // Literal Header Field without Indexing / never Indexed
                        indexType = ((b & 0x10) == 0x10) ? HPackUtil.IndexType.NEVER : HPackUtil.IndexType.NONE;
                        index = b & 0x0F;
                        if (index == 0) {
                            state = State.READ_LITERAL_HEADER_NAME_LENGTH_PREFIX;
                        } else if (index == 0x0F) {
                            state = State.READ_INDEXED_HEADER_NAME;
                        } else {
                            // Index was stored as the prefix
                            this.ReadName(index);
                            state = State.READ_LITERAL_HEADER_VALUE_LENGTH_PREFIX;
                        }
                    }
                    break;

                case State.READ_MAX_DYNAMIC_TABLE_SIZE:
                    int maxSize = Decoder.DecodeULE128(input);
                    if (maxSize == -1) {
                        return;
                    }

                    // Check for numerical overflow
                    if (maxSize > int.MaxValue - index) {
                        throw DECOMPRESSION_EXCEPTION;
                    }

                    this.SetDynamicTableSize(index + maxSize);
                    state = State.READ_HEADER_REPRESENTATION;
                    break;

                case State.READ_INDEXED_HEADER:
                    int headerIndex = Decoder.DecodeULE128(input);
                    if (headerIndex == -1) {
                        return;
                    }

                    // Check for numerical overflow
                    if (headerIndex > int.MaxValue - index) {
                        throw DECOMPRESSION_EXCEPTION;
                    }

                    this.IndexHeader(index + headerIndex, addHeaderDelegate);
                    state = State.READ_HEADER_REPRESENTATION;
                    break;

                case State.READ_INDEXED_HEADER_NAME:
                    // Header Name matches an entry in the Header Table
                    int nameIndex = Decoder.DecodeULE128(input);
                    if (nameIndex == -1) {
                        return;
                    }

                    // Check for numerical overflow
                    if (nameIndex > int.MaxValue - index) {
                        throw DECOMPRESSION_EXCEPTION;
                    }

                    this.ReadName(index + nameIndex);
                    state = State.READ_LITERAL_HEADER_VALUE_LENGTH_PREFIX;
                    break;

                case State.READ_LITERAL_HEADER_NAME_LENGTH_PREFIX:
                    b = input.ReadSByte();
                    huffmanEncoded = (b & 0x80) == 0x80;
                    index = b & 0x7F;
                    if (index == 0x7f) {
                        state = State.READ_LITERAL_HEADER_NAME_LENGTH;
                    } else {
                        nameLength = index;

                        // Disallow empty names -- they cannot be represented in HTTP/1.x
                        if (nameLength == 0) {
                            throw DECOMPRESSION_EXCEPTION;
                        }

                        // Check name length against max header size
                        if (this.ExceedsMaxHeaderSize(nameLength)) {
                            if (indexType == HPackUtil.IndexType.NONE) {
                                // Name is unused so skip bytes
                                name = EMPTY;
                                this.skipLength = nameLength;
                                state = State.SKIP_LITERAL_HEADER_NAME;
                                break;
                            }

                            // Check name length against max dynamic table size
                            if (nameLength + HeaderField.HEADER_ENTRY_OVERHEAD > this.dynamicTable.Capacity) {
                                this.dynamicTable.Clear();
                                name = EMPTY;
                                this.skipLength = nameLength;
                                state = State.SKIP_LITERAL_HEADER_NAME;
                                break;
                            }
                        }
                        state = State.READ_LITERAL_HEADER_NAME;
                    }
                    break;

                case State.READ_LITERAL_HEADER_NAME_LENGTH:
                    // Header Name is a Literal String
                    nameLength = Decoder.DecodeULE128(input);
                    if (nameLength == -1) {
                        return;
                    }

                    // Check for numerical overflow
                    if (nameLength > int.MaxValue - index) {
                        throw DECOMPRESSION_EXCEPTION;
                    }
                    nameLength += index;

                    // Check name length against max header size
                    if (this.ExceedsMaxHeaderSize(nameLength)) {
                        if (indexType == HPackUtil.IndexType.NONE) {
                            // Name is unused so skip bytes
                            name = EMPTY;
                            this.skipLength = nameLength;
                            state = State.SKIP_LITERAL_HEADER_NAME;
                            break;
                        }

                        // Check name length against max dynamic table size
                        if (nameLength + HeaderField.HEADER_ENTRY_OVERHEAD > this.dynamicTable.Capacity) {
                            this.dynamicTable.Clear();
                            name = EMPTY;
                            this.skipLength = nameLength;
                            state = State.SKIP_LITERAL_HEADER_NAME;
                            break;
                        }
                    }
                    state = State.READ_LITERAL_HEADER_NAME;
                    break;

                case State.READ_LITERAL_HEADER_NAME:
                    // Wait until entire name is readable
                    if (input.BaseStream.Length - input.BaseStream.Position < nameLength) {
                        return;
                    }

                    name = this.ReadStringLiteral(input, nameLength);
                    state = State.READ_LITERAL_HEADER_VALUE_LENGTH_PREFIX;
                    break;

                case State.SKIP_LITERAL_HEADER_NAME:

                    this.skipLength -= (int)input.BaseStream.Seek(this.skipLength, SeekOrigin.Current);
                    if (this.skipLength < 0) {
                        this.skipLength = 0;
                    }
                    if (this.skipLength == 0) {
                        state = State.READ_LITERAL_HEADER_VALUE_LENGTH_PREFIX;
                    }
                    break;

                case State.READ_LITERAL_HEADER_VALUE_LENGTH_PREFIX:
                    b = input.ReadSByte();
                    huffmanEncoded = (b & 0x80) == 0x80;
                    index = b & 0x7F;
                    if (index == 0x7f) {
                        state = State.READ_LITERAL_HEADER_VALUE_LENGTH;
                    } else {
                        this.valueLength = index;

                        // Check new header size against max header size
                        long newHeaderSize1 = (long)nameLength + (long)this.valueLength;
                        if (this.ExceedsMaxHeaderSize(newHeaderSize1)) {
                            // truncation will be reported during endHeaderBlock
                            headerSize = maxHeaderSize + 1;

                            if (indexType == HPackUtil.IndexType.NONE) {
                                // Value is unused so skip bytes
                                state = State.SKIP_LITERAL_HEADER_VALUE;
                                break;
                            }

                            // Check new header size against max dynamic table size
                            if (newHeaderSize1 + HeaderField.HEADER_ENTRY_OVERHEAD > this.dynamicTable.Capacity) {
                                this.dynamicTable.Clear();
                                state = State.SKIP_LITERAL_HEADER_VALUE;
                                break;
                            }
                        }

                        if (this.valueLength == 0) {
                            this.InsertHeader(addHeaderDelegate, name, EMPTY, indexType);
                            state = State.READ_HEADER_REPRESENTATION;
                        } else {
                            state = State.READ_LITERAL_HEADER_VALUE;
                        }
                    }
                    break;

                case State.READ_LITERAL_HEADER_VALUE_LENGTH:
                    // Header Value is a Literal String
                    this.valueLength = Decoder.DecodeULE128(input);
                    if (this.valueLength == -1) {
                        return;
                    }

                    // Check for numerical overflow
                    if (this.valueLength > int.MaxValue - index) {
                        throw DECOMPRESSION_EXCEPTION;
                    }
                    this.valueLength += index;

                    // Check new header size against max header size
                    long newHeaderSize2 = (long)nameLength + (long)this.valueLength;
                    if (newHeaderSize2 + headerSize > maxHeaderSize) {
                        // truncation will be reported during endHeaderBlock
                        headerSize = maxHeaderSize + 1;

                        if (indexType == HPackUtil.IndexType.NONE) {
                            // Value is unused so skip bytes
                            state = State.SKIP_LITERAL_HEADER_VALUE;
                            break;
                        }

                        // Check new header size against max dynamic table size
                        if (newHeaderSize2 + HeaderField.HEADER_ENTRY_OVERHEAD > this.dynamicTable.Capacity) {
                            this.dynamicTable.Clear();
                            state = State.SKIP_LITERAL_HEADER_VALUE;
                            break;
                        }
                    }
                    state = State.READ_LITERAL_HEADER_VALUE;
                    break;

                case State.READ_LITERAL_HEADER_VALUE:
                    // Wait until entire value is readable
                    if (input.BaseStream.Length - input.BaseStream.Position < this.valueLength) {
                        return;
                    }

                    byte[] value = this.ReadStringLiteral(input, this.valueLength);
                    this.InsertHeader(addHeaderDelegate, name, value, indexType);
                    state = State.READ_HEADER_REPRESENTATION;
                    break;

                case State.SKIP_LITERAL_HEADER_VALUE:
                    this.valueLength -= (int)input.BaseStream.Seek(this.valueLength, SeekOrigin.Current);
                    if (this.valueLength < 0) {
                        this.valueLength = 0;
                    }
                    if (this.valueLength == 0) {
                        state = State.READ_HEADER_REPRESENTATION;
                    }
                    break;

                default:
                    throw new Exception("should not reach here");
                }
            }
        }
Пример #9
0
 private void AddHeader(AddHeaderDelegate addHeaderDelegate, byte[] name, byte[] value, bool sensitive)
 {
     if (name.Length == 0) {
         throw new ArgumentException("name is empty");
     }
     long newSize = headerSize + name.Length + value.Length;
     if (newSize <= maxHeaderSize) {
         addHeaderDelegate (name, value, sensitive);
         headerSize = (int)newSize;
     } else {
         // truncation will be reported during endHeaderBlock
         headerSize = maxHeaderSize + 1;
     }
 }
Пример #10
0
        private void InsertHeader(AddHeaderDelegate addHeaderDelegate, byte[] name, byte[] value, HPackUtil.IndexType indexType)
        {
            this.AddHeader(addHeaderDelegate, name, value, indexType == HPackUtil.IndexType.NEVER);

            switch(indexType) {
            case HPackUtil.IndexType.NONE:
            case HPackUtil.IndexType.NEVER:
                break;

            case HPackUtil.IndexType.INCREMENTAL:
                this.dynamicTable.Add(new HeaderField(name, value));
                break;

            default:
                throw new Exception("should not reach here");
            }
        }
Пример #11
0
 private void IndexHeader(int index, AddHeaderDelegate addHeaderDelegate)
 {
     if (index <= StaticTable.Length) {
         HeaderField headerField = StaticTable.GetEntry(index);
         this.AddHeader(addHeaderDelegate, headerField.Name, headerField.Value, false);
     } else if (index - StaticTable.Length <= this.dynamicTable.Length()) {
         HeaderField headerField = this.dynamicTable.GetEntry(index - StaticTable.Length);
         this.AddHeader(addHeaderDelegate, headerField.Name, headerField.Value, false);
     } else {
         throw new IOException("illegal index value (" + index + ")");
     }
 }
Пример #12
0
 static void TryAddToCollection(AddHeaderDelegate addHeader, string headerName, string value)
 {
     try
     {
         addHeader(headerName, value);
     }
     catch (ArgumentException ex)
     {
         string encodedValue = null;
         if (TryEncodeHeaderValueAsUri(headerName, value, out encodedValue))
         {
             //note: if the hosthame of a referer header contains illegal chars, we will still throw from here
             //because Uri will not fix this up for us, which is ok. The request will get rejected in the error code path.
             addHeader(headerName, encodedValue);
         }
         else
         {
             // In self-hosted scenarios, some of the headers like Content-Length cannot be added directly.
             // It will throw ArgumentException instead.
             FxTrace.Exception.AsInformation(ex);
         }
     }
 }
Пример #13
0
        public static void CopyHeaders(NameValueCollection headers, AddHeaderDelegate addHeader)
        {
            //this nested loop logic was copied from NameValueCollection.Add(NameValueCollection)
            int count = headers.Count;
            for (int i = 0; i < count; i++)
            {
                string key = headers.GetKey(i);

                string[] values = headers.GetValues(i);
                if (values != null)
                {
                    for (int j = 0; j < values.Length; j++)
                    {
                        TryAddToCollection(addHeader, key, values[j]);
                    }
                }
                else
                {
                    addHeader(key, null);
                }
            }
        }
Пример #14
0
 static void CopyHeaders(HttpHeaders headers, AddHeaderDelegate addHeader)
 {
     foreach (KeyValuePair<string, IEnumerable<string>> header in headers)
     {
         foreach (string value in header.Value)
         {
             TryAddToCollection(addHeader, header.Key, value);                    
         }
     }
 }
Пример #15
0
 public static void CopyHeaders(HttpResponseMessage response, AddHeaderDelegate addHeader)
 {
     HttpChannelUtilities.CopyHeaders(response.Headers, addHeader);
     if (response.Content != null)
     {
         HttpChannelUtilities.CopyHeaders(response.Content.Headers, addHeader);
     }
 }