#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2009 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Collections.Generic;
namespace Nuclex.Networking.Http {
/// Collects data to build an HTTP request container
internal class RequestBuilder {
/// Wraps a HTTP header field value
private class FieldValue {
/// Combined contents of the HTTP header field value
public string Contents;
}
/// Initializes a new HTTP request builder
public RequestBuilder() {
this.headers = new Dictionary();
Reset();
}
/// Resets the request builder to its initial state
public void Reset() {
this.headers.Clear();
this.Method = null;
this.Uri = null;
this.Version = null;
}
/// Adds a header field for inclusion in the built HTTP request
///
/// Name of the header field to include in the built HTTP request
///
public void AddHeader(string fieldName) {
AddHeader(fieldName, null);
}
///
/// Adds a header field and its value for inclusion in the built HTTP request
///
///
/// Name of the header field to include in the built HTTP request
///
///
/// Field value to set or append to the existing field value for that field
///
public void AddHeader(string fieldName, string append) {
FieldValue value;
// Try to obtain the header field. If it isn't on record yet, we set up
// a new header field transparently.
if(!this.headers.TryGetValue(fieldName, out value)) {
value = new FieldValue();
this.headers.Add(fieldName, value);
}
// Header field is known. If this is a header field without a value (either new
// or had no provided value), it will be provided with a value, otherwise, the
// value will be appended to the existing field value.
if(value.Contents == null) {
value.Contents = append;
} else if(append != null) {
value.Contents += append;
}
}
/// Builds an HTTP request from the current data available
/// The newly built HTTP request
public Request BuildRequest() {
// Condense the headers into simple strings (our own storage format is optimized
// to be extendable without having to readd the values into the dictionary,
// however, once the request is complete, this is no longer useful).
Dictionary condensedHeaders = new Dictionary();
foreach(KeyValuePair header in this.headers) {
condensedHeaders.Add(header.Key, header.Value.Contents);
}
// Everything is ready, now build the HTTP request container
return new Request(this.Method, this.Uri, this.Version, condensedHeaders);
}
/// HTTP request method
public string Method;
/// URI being requested
public string Uri;
/// Version of the HTTP protocol being used
public string Version;
///
/// Records the header fields that will be assigned to the built HTTP request
///
private Dictionary headers;
}
} // namespace Nuclex.Networking.Http