dotcloud/docker — pull request #1382 by monnand (2013-08-02 08:21:51+00:00)
Title: 650 http utils and user agent field

fix #650 and #156

This is a follow up of #1064.

Changes:

- Added a file ``http.go`` in package ``utils``. In side this file:
* ``VersionInfo`` and its related functions mentioned in #1064 are moved into this file.
* Defined a new interface ``HTTPRequestDecorator``. It has a method named ``ChangeRequest()``, which takes an HTTP request, changes it and return the modified request (or error).
* Defined a new structure ``HTTPUserAgentDecorator``, which implements ``HTTPRequestDecorator``. It has a list of ``VersionInfo`` and append this information to user agent field to an HTTP request.
* Defined a new structure ``HTTPRequestFactory``. It has a method ``NewRequest()`` used to create a new HTTP request. This method is compatible with the ``NewRequest()`` function in standard ``net/http`` package. ``HTTPRequestFactory`` has a member which is a list of ``HTTPRequestDecorator``. On a call to ``NewRequest()``, the ``HTTPRequestFactory`` first creates an HTTP request and then applies all the decorators on this request.
- Changed ``server.go`` and ``auth/auth.go`` accordingly, so that they will use ``HTTPRequestFactory`` to create ``*http.Request``, which will automatically add the User-Agent field.

With ``HTTPRequestFactory`` and ``HTTPRequestDecorator``, it will be easy to add new header field, change request body and do other things on the requests.

P.S. The name ``HTTPRequestDecorator`` may lead to the decorator design pattern. Although it is fairly simple to apply this pattern on the interface, I didn't use the decorator pattern here in the code. Is there any better choice than ``HTTPRequestDecorator``?