Austin Schuh | 24adb6b | 2015-09-06 17:37:40 -0700 | [diff] [blame^] | 1 | // Copyright (c) 2013, Matt Godbolt |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are met: |
| 6 | // |
| 7 | // Redistributions of source code must retain the above copyright notice, this |
| 8 | // list of conditions and the following disclaimer. |
| 9 | // |
| 10 | // Redistributions in binary form must reproduce the above copyright notice, |
| 11 | // this list of conditions and the following disclaimer in the documentation |
| 12 | // and/or other materials provided with the distribution. |
| 13 | // |
| 14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 15 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 18 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 19 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 20 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 22 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 23 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 24 | // POSSIBILITY OF SUCH DAMAGE. |
| 25 | |
| 26 | #include "seasocks/ResponseBuilder.h" |
| 27 | |
| 28 | #include "internal/ConcreteResponse.h" |
| 29 | |
| 30 | namespace seasocks { |
| 31 | |
| 32 | ResponseBuilder::ResponseBuilder(ResponseCode code) : |
| 33 | _code(code), |
| 34 | _contentType("text/plain"), |
| 35 | _keepAlive(true), |
| 36 | _stream(new std::ostringstream) { |
| 37 | |
| 38 | } |
| 39 | |
| 40 | ResponseBuilder& ResponseBuilder::asHtml() { |
| 41 | return withContentType("text/html"); |
| 42 | } |
| 43 | |
| 44 | ResponseBuilder& ResponseBuilder::asText() { |
| 45 | return withContentType("text/plain"); |
| 46 | } |
| 47 | |
| 48 | ResponseBuilder& ResponseBuilder::asJson() { |
| 49 | return withContentType("application/json"); |
| 50 | } |
| 51 | |
| 52 | ResponseBuilder& ResponseBuilder::withContentType(const std::string& contentType) { |
| 53 | _contentType = contentType; |
| 54 | return *this; |
| 55 | } |
| 56 | |
| 57 | ResponseBuilder& ResponseBuilder::keepsConnectionAlive() { |
| 58 | _keepAlive = true; |
| 59 | return *this; |
| 60 | } |
| 61 | |
| 62 | ResponseBuilder& ResponseBuilder::closesConnection() { |
| 63 | _keepAlive = false; |
| 64 | return *this; |
| 65 | } |
| 66 | |
| 67 | ResponseBuilder& ResponseBuilder::withLocation(const std::string& location) { |
| 68 | return withHeader("Location", location); |
| 69 | } |
| 70 | |
| 71 | ResponseBuilder& ResponseBuilder::setsCookie(const std::string& cookie, const std::string& value) { |
| 72 | return addHeader("Set-Cookie", cookie + "=" + value); |
| 73 | } |
| 74 | |
| 75 | ResponseBuilder& ResponseBuilder::withHeader(const std::string& name, const std::string& value) { |
| 76 | _headers.erase(name); |
| 77 | _headers.insert(std::make_pair(name, value)); |
| 78 | return *this; |
| 79 | } |
| 80 | |
| 81 | ResponseBuilder& ResponseBuilder::addHeader(const std::string& name, const std::string& value) { |
| 82 | _headers.insert(std::make_pair(name, value)); |
| 83 | return *this; |
| 84 | } |
| 85 | |
| 86 | std::shared_ptr<Response> ResponseBuilder::build() { |
| 87 | return std::shared_ptr<Response>(new ConcreteResponse(_code, _stream->str(), _contentType, _headers, _keepAlive)); |
| 88 | } |
| 89 | |
| 90 | } |