1 /******************************************************************************* 2 3 Copyright: 4 Copyright (c) 2004 Kris Bell. 5 Some parts copyright (c) 2009-2016 dunnhumby Germany GmbH. 6 All rights reserved. 7 8 License: 9 Tango Dual License: 3-Clause BSD License / Academic Free License v3.0. 10 See LICENSE_TANGO.txt for details. 11 12 Version: Initial release: April 2004 13 14 Authors: Kris 15 16 *******************************************************************************/ 17 18 module ocean.net.http.HttpParams; 19 20 import ocean.meta.types.Qualifiers; 21 22 import ocean.time.Time; 23 24 import ocean.io.model.IConduit; 25 26 import ocean.net.http.HttpTokens; 27 28 import ocean.io.stream.Delimiters; 29 30 public import ocean.net.http.model.HttpParamsView; 31 32 /****************************************************************************** 33 34 Maintains a set of query parameters, parsed from an HTTP request. 35 Use HttpParams instead for output parameters. 36 37 Note that these input params may have been encoded by the user- 38 agent. Unfortunately there has been little consensus on what that 39 encoding should be (especially regarding GET query-params). With 40 luck, that will change to a consistent usage of UTF-8 within the 41 near future. 42 43 ******************************************************************************/ 44 45 class HttpParams : HttpTokens, HttpParamsView 46 { 47 // tell compiler to expose super.parse() also 48 alias HttpTokens.parse parse; 49 50 private Delimiters amp; 51 52 /********************************************************************** 53 54 Construct parameters by telling the HttpStack that 55 name/value pairs are separated by a '=' character. 56 57 **********************************************************************/ 58 59 this () 60 { 61 super ('='); 62 63 // construct a line tokenizer for later usage 64 amp = new Delimiters("&"); 65 } 66 67 /********************************************************************** 68 69 Return the number of headers 70 71 **********************************************************************/ 72 73 uint size () 74 { 75 return super.stack.size; 76 } 77 78 /********************************************************************** 79 80 Read all query parameters. Everything is mapped rather 81 than being allocated & copied 82 83 **********************************************************************/ 84 85 override void parse (InputBuffer input) 86 { 87 setParsed (true); 88 amp.set (input); 89 90 while (amp.next || amp.get.length) 91 stack.push (amp.get); 92 } 93 94 /********************************************************************** 95 96 Add a name/value pair to the query list 97 98 **********************************************************************/ 99 100 override void add (cstring name, cstring value) 101 { 102 super.add (name, value); 103 } 104 105 /********************************************************************** 106 107 Add a name/integer pair to the query list 108 109 **********************************************************************/ 110 111 override void addInt (cstring name, int value) 112 { 113 super.addInt (name, value); 114 } 115 116 117 /********************************************************************** 118 119 Add a name/date(long) pair to the query list 120 121 **********************************************************************/ 122 123 override void addDate (cstring name, Time value) 124 { 125 super.addDate (name, value); 126 } 127 128 /********************************************************************** 129 130 Return the value of the provided header, or null if the 131 header does not exist 132 133 **********************************************************************/ 134 135 override cstring get (cstring name, cstring ret = null) 136 { 137 return super.get (name, ret); 138 } 139 140 /********************************************************************** 141 142 Return the integer value of the provided header, or the 143 provided default-value if the header does not exist 144 145 **********************************************************************/ 146 147 override int getInt (cstring name, int ret = -1) 148 { 149 return super.getInt (name, ret); 150 } 151 152 /********************************************************************** 153 154 Return the date value of the provided header, or the 155 provided default-value if the header does not exist 156 157 **********************************************************************/ 158 159 override Time getDate (cstring name, Time ret = Time.epoch) 160 { 161 return super.getDate (name, ret); 162 } 163 164 165 /********************************************************************** 166 167 Output the param list to the provided consumer 168 169 **********************************************************************/ 170 171 override void produce (scope size_t delegate(const(void)[]) consume, cstring eol=null) 172 { 173 return super.produce (consume, eol); 174 } 175 }