1 /****************************************************************************** 2 3 HTTP response message generator with support for cookies 4 5 Takes a list of HttpCookieGenerator instances in the constructor. When 6 rendering the response by CookiesHttpResponse.render(), a Set-Cookie header 7 line will be added for each HttpCookieGenerator instance a cookie value is 8 assigned to. 9 CookiesHttpResponse.reset() calls reset() on all cookies. 10 11 Copyright: 12 Copyright (c) 2009-2016 dunnhumby Germany GmbH. 13 All rights reserved. 14 15 License: 16 Boost Software License Version 1.0. See LICENSE_BOOST.txt for details. 17 Alternatively, this file may be distributed under the terms of the Tango 18 3-Clause BSD License (see LICENSE_BSD.txt for details). 19 20 ******************************************************************************/ 21 22 module ocean.net.http.cookie.CookiesHttpResponse; 23 24 25 import ocean.net.http.HttpResponse; 26 import ocean.net.http.cookie.HttpCookieGenerator; 27 import ocean.net.http.consts.HeaderFieldNames; 28 29 /******************************************************************************/ 30 31 class CookiesHttpResponse : HttpResponse 32 { 33 /************************************************************************** 34 35 List of cookies. render() adds a Set-Cookie header line will for each 36 cookie to which a value was assigned to after the last reset(). 37 38 **************************************************************************/ 39 40 public HttpCookieGenerator[] cookies; 41 42 /************************************************************************** 43 44 Constructor 45 46 Params: 47 cookies = cookies to use 48 49 **************************************************************************/ 50 51 public this ( HttpCookieGenerator[] cookies ... ) 52 out 53 { 54 foreach (cookie; this.cookies) 55 { 56 assert (cookie !is null, "null cookie instance"); 57 } 58 } 59 do 60 { 61 this.cookies = cookies.dup; // No .dup caused segfaults, apparently the 62 // array is then sliced. 63 super.addKey(HeaderFieldNames.ResponseNames.SetCookie); 64 } 65 66 /************************************************************************** 67 68 Called by render() when the Set-Cookie header lines should be appended. 69 70 Params: 71 append = header line appender 72 73 **************************************************************************/ 74 75 protected override void addHeaders ( AppendHeaderLines append ) 76 { 77 foreach (cookie; this.cookies) if (cookie.value) 78 { 79 scope append_line = append..new IncrementalValue("Set-Cookie"); 80 81 cookie.render(&append_line.appendToValue); 82 } 83 } 84 85 /************************************************************************** 86 87 Called by reset(), resets the cookies. 88 89 **************************************************************************/ 90 91 public override void reset ( ) 92 { 93 super.reset(); 94 95 foreach (cookie; this.cookies) 96 { 97 cookie.reset(); 98 } 99 } 100 }