00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 module mango.http.server.HttpHeaders;
00037
00038 private import mango.io.Token,
00039 mango.io.Tokenizer;
00040
00041 private import mango.io.model.IBuffer,
00042 mango.io.model.IWriter;
00043
00044 private import mango.http.utils.TokenStack;
00045
00046 private import mango.http.server.HttpTokens;
00047
00048
00049
00050
00051
00052
00053
00054
00055 struct HttpHeaderName
00056 {
00057 char[] value;
00058 }
00059
00060
00061
00062
00063
00064
00065
00066 struct HttpHeader
00067 {
00068
00069 static const int IOBufferSize = 16 * 1024;
00070
00071
00072 static const int MaxPostParamSize = 4 * 1024;
00073
00074 static const HttpHeaderName Version = {"HTTP/1.1"};
00075 static const HttpHeaderName TextHtml = {"text/html"};
00076
00077 static const HttpHeaderName Accept = {"Accept:"};
00078 static const HttpHeaderName AcceptCharset = {"Accept-Charset:"};
00079 static const HttpHeaderName AcceptEncoding = {"Accept-Encoding:"};
00080 static const HttpHeaderName AcceptLanguage = {"Accept-Language:"};
00081 static const HttpHeaderName AcceptRanges = {"Accept-Ranges:"};
00082 static const HttpHeaderName Age = {"Age:"};
00083 static const HttpHeaderName Allow = {"Allow:"};
00084 static const HttpHeaderName Authorization = {"Authorization:"};
00085 static const HttpHeaderName CacheControl = {"Cache-Control:"};
00086 static const HttpHeaderName Connection = {"Connection:"};
00087 static const HttpHeaderName ContentEncoding = {"Content-Encoding:"};
00088 static const HttpHeaderName ContentLanguage = {"Content-Language:"};
00089 static const HttpHeaderName ContentLength = {"Content-Length:"};
00090 static const HttpHeaderName ContentLocation = {"Content-Location:"};
00091 static const HttpHeaderName ContentRange = {"Content-Range:"};
00092 static const HttpHeaderName ContentType = {"Content-Type:"};
00093 static const HttpHeaderName Cookie = {"Cookie:"};
00094 static const HttpHeaderName Date = {"Date:"};
00095 static const HttpHeaderName ETag = {"ETag:"};
00096 static const HttpHeaderName Expect = {"Expect:"};
00097 static const HttpHeaderName Expires = {"Expires:"};
00098 static const HttpHeaderName From = {"From:"};
00099 static const HttpHeaderName Host = {"Host:"};
00100 static const HttpHeaderName Identity = {"identity:"};
00101 static const HttpHeaderName IfMatch = {"If-Match:"};
00102 static const HttpHeaderName IfModifiedSince = {"If-Modified-Since:"};
00103 static const HttpHeaderName IfNoneMatch = {"If-None-Match:"};
00104 static const HttpHeaderName IfRange = {"If-Range:"};
00105 static const HttpHeaderName IfUnmodifiedSince = {"If-Unmodified-Since:"};
00106 static const HttpHeaderName LastModified = {"Last-Modified:"};
00107 static const HttpHeaderName Location = {"Location:"};
00108 static const HttpHeaderName MaxForwards = {"Max-Forwards:"};
00109 static const HttpHeaderName MimeVersion = {"MIME-Version:"};
00110 static const HttpHeaderName Pragma = {"Pragma:"};
00111 static const HttpHeaderName ProxyAuthenticate = {"Proxy-Authenticate:"};
00112 static const HttpHeaderName ProxyConnection = {"Proxy-Connection:"};
00113 static const HttpHeaderName Range = {"Range:"};
00114 static const HttpHeaderName Referrer = {"Referer:"};
00115 static const HttpHeaderName RetryAfter = {"Retry-After:"};
00116 static const HttpHeaderName Server = {"Server:"};
00117 static const HttpHeaderName ServletEngine = {"Servlet-Engine:"};
00118 static const HttpHeaderName SetCookie = {"Set-Cookie:"};
00119 static const HttpHeaderName SetCookie2 = {"Set-Cookie2:"};
00120 static const HttpHeaderName TE = {"TE:"};
00121 static const HttpHeaderName Trailer = {"Trailer:"};
00122 static const HttpHeaderName TransferEncoding = {"Transfer-Encoding:"};
00123 static const HttpHeaderName Upgrade = {"Upgrade:"};
00124 static const HttpHeaderName UserAgent = {"User-Agent:"};
00125 static const HttpHeaderName Vary = {"Vary:"};
00126 static const HttpHeaderName Warning = {"Warning:"};
00127 static const HttpHeaderName WwwAuthenticate = {"WWW-Authenticate:"};
00128 }
00129
00130
00131
00132
00133
00134
00135
00136
00137 struct HeaderElement
00138 {
00139 HttpHeaderName name;
00140 char[] value;
00141 }
00142
00143
00144
00145
00146
00147
00148
00149
00150 class HttpHeaders : HttpTokens
00151 {
00152 private static BoundToken line;
00153
00154
00155 alias HttpTokens.parse parse;
00156
00157
00158
00159
00160
00161
00162
00163 static this ()
00164 {
00165
00166
00167
00168 line = new BoundToken (new LineTokenizer());
00169 }
00170
00171
00172
00173
00174
00175
00176
00177
00178 this ()
00179 {
00180
00181
00182 super (':', true);
00183 }
00184
00185
00186
00187
00188
00189
00190
00191 this (HttpHeaders source)
00192 {
00193 super (source);
00194 }
00195
00196
00197
00198
00199
00200
00201
00202 HttpHeaders clone ()
00203 {
00204 return new HttpHeaders (this);
00205 }
00206
00207
00208
00209
00210
00211
00212
00213
00214 void parse (IBuffer input)
00215 {
00216 setParsed (true);
00217 while (line.next(input) && line.getLength())
00218 stack.push(line);
00219 }
00220
00221
00222
00223
00224
00225
00226
00227
00228 char[] get (HttpHeaderName name)
00229 {
00230 return super.get (name.value);
00231 }
00232
00233
00234
00235
00236
00237
00238
00239
00240 int getInt (HttpHeaderName name)
00241 {
00242 return super.getInt (name.value);
00243 }
00244
00245
00246
00247
00248
00249
00250
00251
00252 long getDate (HttpHeaderName name)
00253 {
00254 return super.getDate (name.value);
00255 }
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265 int opApply (int delegate(inout HeaderElement) dg)
00266 {
00267 HeaderElement element;
00268 int result = 0;
00269
00270 foreach (HttpToken token; super)
00271 {
00272 element.name.value = token.name;
00273 element.value = token.value;
00274 result = dg (element);
00275 if (result)
00276 break;
00277 }
00278 return result;
00279 }
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289 FilteredHeaders createFilter (HttpHeaderName header)
00290 {
00291 return new FilteredHeaders (this, header);
00292 }
00293
00294
00295
00296
00297
00298
00299
00300 private class FilteredHeaders : FilteredTokens
00301 {
00302
00303
00304
00305
00306
00307
00308
00309 this (HttpHeaders headers, HttpHeaderName header)
00310 {
00311 super (headers, header.value);
00312 }
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322 int opApply (int delegate(inout HeaderElement) dg)
00323 {
00324 HeaderElement element;
00325 int result = 0;
00326
00327 foreach (HttpToken token; super)
00328 {
00329 element.name.value = token.name;
00330 element.value = token.value;
00331 result = dg (element);
00332 if (result)
00333 break;
00334 }
00335 return result;
00336 }
00337
00338 }
00339 }
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351 class HttpMutableHeaders : HttpHeaders
00352 {
00353
00354
00355
00356
00357
00358
00359
00360 this (IBuffer output)
00361 {
00362 super ();
00363 super.setOutputBuffer (output);
00364 }
00365
00366
00367
00368
00369
00370
00371
00372 this (HttpMutableHeaders source)
00373 {
00374 super (source);
00375 }
00376
00377
00378
00379
00380
00381
00382
00383 HttpMutableHeaders clone ()
00384 {
00385 return new HttpMutableHeaders (this);
00386 }
00387
00388
00389
00390
00391
00392
00393
00394
00395 void add (HttpHeaderName name, void delegate (IBuffer) dg)
00396 {
00397 super.add (name.value, dg);
00398 }
00399
00400
00401
00402
00403
00404
00405
00406 void add (HttpHeaderName name, char[] value)
00407 {
00408 super.add (name.value, value);
00409 }
00410
00411
00412
00413
00414
00415
00416
00417 void addInt (HttpHeaderName name, int value)
00418 {
00419 super.addInt (name.value, value);
00420 }
00421
00422
00423
00424
00425
00426
00427
00428 void addDate (HttpHeaderName name, long value)
00429 {
00430 super.addDate (name.value, value);
00431 }
00432
00433
00434
00435
00436
00437
00438
00439 IBuffer getOutputBuffer ()
00440 {
00441 return super.getOutputBuffer ();
00442 }
00443 }