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