0001    // RequestSerializer.swift
0002    //
0003    // The MIT License (MIT)
0004    //
0005    // Copyright (c) 2015 Zewo
0006    //
0007    // Permission is hereby granted, free of charge, to any person obtaining a copy
0008    // of this software and associated documentation files (the "Software"), to deal
0009    // in the Software without restriction, including without limitation the rights
0010    // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0011    // copies of the Software, and to permit persons to whom the Software is
0012    // furnished to do so, subject to the following conditions:
0013    //
0014    // The above copyright notice and this permission notice shall be included in all
0015    // copies or substantial portions of the Software.
0016    //
0017    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0018    // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0019    // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0020    // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0021    // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0022    // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0023    // SOFTWARE.
0024    
0025    @_exported import Stream
0026    
0027    public struct RequestSerializer: RequestSerializerType {
0028        public init() {}
0029    
0030        public func serialize(request: Request, @noescape send: Data throws -> Void) throws {
0031            let newLine: Data = [13, 10]
0032    
0033            try send("\(request.method) \(request.uri) HTTP/\(request.version.major).\(request.version.minor)".data)
0034            try send(newLine)
0035    
0036            for (name, value) in request.headers {
0037                try send("\(name): \(value)".data)
0038                try send(newLine)
0039            }
0040    
0041            if request.cookies.count > 0 {
0042                try send("Cookie:" )
0043                for (index, cookie) in request.cookies.enumerate() {
0044                    try send(" \(cookie.name)=\(cookie.value)".data)
0045    
0046                    if index < request.cookies.count - 1 {
0047                        try send(";")
0048                    }
0049                }
0050                try send(newLine)
0051            }
0052    
0053            try send(newLine)
0054    
0055            switch request.body {
0056            case .Buffer(let data):
0057                try send(data)
0058            case .Stream(let bodyStream):
0059                while !bodyStream.closed {
0060                    let data = try bodyStream.receive()
0061                    try send(String(data.count, radix: 16).data)
0062                    try send(newLine)
0063                    try send(data)
0064                    try send(newLine)
0065                }
0066    
0067                try send("0".data)
0068                try send(newLine)
0069                try send(newLine)
0070            }
0071        }
0072    }
0073