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
00037
00038
00039 module mango.http.server.HttpRequest;
00040
00041 private import mango.text.Text;
00042
00043 private import mango.convert.Atoi;
00044
00045 private import mango.io.Uri,
00046 mango.io.Token,
00047 mango.io.Buffer,
00048 mango.io.Reader,
00049 mango.io.Socket,
00050 mango.io.Exception,
00051 mango.io.Tokenizer;
00052
00053 private import mango.io.model.IBuffer,
00054 mango.io.model.IWriter;
00055
00056 private import mango.http.HttpReader;
00057
00058 private import mango.http.server.HttpCookies,
00059 mango.http.server.HttpHeaders,
00060 mango.http.server.HttpMessage,
00061 mango.http.server.HttpQueryParams;
00062
00063 private import mango.http.server.model.IProviderBridge;
00064
00065
00066
00067
00068
00069
00070
00071
00072 class HttpRequest : HttpMessage, IWritable
00073 {
00074 private int port;
00075 private char[] host;
00076 private bool mimed,
00077 uried,
00078 gulped;
00079
00080
00081 private MutableUri uri;
00082 private CompositeToken line;
00083 private HttpReader reader;
00084 private HttpQueryParams params;
00085 private HttpCookies cookies;
00086 private StartLine startLine;
00087
00088 static private InvalidStateException InvalidState;
00089
00090
00091
00092
00093
00094
00095
00096 static this()
00097 {
00098 InvalidState = new InvalidStateException("Invalid request state");
00099 }
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 this (IProviderBridge bridge)
00111 {
00112 IBuffer buffer;
00113
00114 super (bridge, null);
00115 buffer = super.getBuffer();
00116
00117
00118 uri = new MutableUri();
00119
00120
00121 startLine = new StartLine();
00122
00123
00124 params = new HttpQueryParams ();
00125
00126
00127 reader = new HttpReader (buffer);
00128
00129
00130 cookies = new HttpCookies (getHeader());
00131
00132
00133 line = new CompositeToken (Tokenizers.line, buffer);
00134 }
00135
00136
00137
00138
00139
00140
00141
00142 void reset()
00143 {
00144 port = Uri.InvalidPort;
00145 host = null;
00146 uried = false;
00147 mimed = false;
00148 gulped = false;
00149
00150 uri.reset();
00151 super.reset();
00152 params.reset();
00153 cookies.reset();
00154 }
00155
00156
00157
00158
00159
00160
00161
00162 StartLine getStartLine()
00163 {
00164 return startLine;
00165 }
00166
00167
00168
00169
00170
00171
00172
00173 Uri getRequestUri()
00174 {
00175 if (! uried)
00176 {
00177 uri.parse (startLine.getPath());
00178 if (uri.getScheme() is null)
00179 uri.setScheme (getServerScheme());
00180 uried = true;
00181 }
00182 return uri;
00183 }
00184
00185
00186
00187
00188
00189
00190
00191 Uri getExplicitUri()
00192 {
00193 getRequestUri();
00194
00195 if (uri.getHost is null)
00196 uri.setHost (getHost);
00197 return uri;
00198 }
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208 HttpReader getReader()
00209 {
00210
00211 gulped = true;
00212 return reader;
00213 }
00214
00215
00216
00217
00218
00219
00220
00221 HttpCookies getInputCookies()
00222 {
00223 if (gulped)
00224 throw InvalidState;
00225 return cookies;
00226 }
00227
00228
00229
00230
00231
00232
00233
00234 HttpHeaders getInputHeaders()
00235 {
00236 if (gulped)
00237 throw InvalidState;
00238 return getHeader();
00239 }
00240
00241
00242
00243
00244
00245
00246
00247
00248 HttpParams getInputParameters()
00249 {
00250
00251 if (! params.isParsed ())
00252 {
00253 char[] query = getRequestUri().getQuery();
00254
00255
00256 if (query.length)
00257
00258 params.parse (query);
00259 else
00260
00261 if (startLine.getMethod() == "POST" &&
00262 super.getContentType() == "application/x-www-form-urlencoded")
00263 {
00264
00265 int length = getHeader.getInt (HttpHeader.ContentLength);
00266 if (length < 0)
00267 throw new IOException ("No params supplied for http POST");
00268 else
00269 if (length > HttpHeader.MaxPostParamSize)
00270 throw new IOException ("Post parameters exceed maximum length");
00271 else
00272 {
00273 char[] c = cast(char[]) getBuffer.get (length);
00274 if (c)
00275 params.parse (uri.decode (Text.replace(c, '+', ' ')));
00276 else
00277 throw new IOException ("Post parameters exceed buffer size");
00278 }
00279 }
00280 }
00281 return params;
00282 }
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292 IBuffer getInputBuffer()
00293 {
00294
00295 gulped = true;
00296 return super.getBuffer();
00297 }
00298
00299
00300
00301
00302
00303
00304
00305
00306 void write (IWriter writer)
00307 {
00308 startLine.write (writer);
00309 super.write (writer);
00310 }
00311
00312
00313
00314
00315
00316
00317
00318 void readHeaders()
00319 {
00320 IBuffer input = super.getBuffer();
00321
00322
00323 while (line.next() && line.getLength() == 0)
00324 {}
00325
00326
00327 if (input.readable() == 0)
00328 throw new IOException ("truncated request");
00329
00330
00331 startLine.parse (line.toString());
00332
00333
00334 getHeader().parse (input);
00335
00336 version (ShowHeaders)
00337 {
00338 Stdout.cr().put(">>>> request Headers:").cr();
00339 getHeader().write (Stdout);
00340 }
00341 }
00342
00343
00344
00345
00346
00347
00348
00349 char[] getRemoteAddr()
00350 {
00351 return getBridge().getServer().getRemoteAddress(getConduit());
00352 }
00353
00354
00355
00356
00357
00358
00359
00360 char[] getRemoteHost()
00361 {
00362 return getBridge().getServer().getRemoteHost(getConduit());
00363 }
00364
00365
00366
00367
00368
00369
00370
00371 char[] getServerScheme()
00372 {
00373 return getBridge().getServer().getProtocol();
00374 }
00375
00376
00377
00378
00379
00380
00381
00382 char[] getEncoding()
00383 {
00384 getMimeType();
00385 return super.getEncoding();
00386 }
00387
00388
00389
00390
00391
00392
00393
00394 char[] getMimeType()
00395 {
00396 if (! mimed)
00397 {
00398 setMimeAndEncoding (super.getContentType());
00399 mimed = true;
00400 }
00401 return super.getMimeType();
00402 }
00403
00404
00405
00406
00407
00408
00409
00410 int getPort()
00411 {
00412 if (port == Uri.InvalidPort)
00413 {
00414 getHost();
00415 if (port == Uri.InvalidPort)
00416
00417 port = getBridge().getServer().getPort();
00418 }
00419 return port;
00420 }
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430 char[] getHost()
00431 {
00432
00433 if (host.length)
00434 return host;
00435
00436
00437 Uri uri = getRequestUri ();
00438
00439 host = uri.getHost ();
00440 port = uri.getPort ();
00441 if (host.length)
00442 return host;
00443
00444
00445 host = Text.trim (getHeader().get(HttpHeader.Host));
00446 port = Uri.InvalidPort;
00447
00448 if (host.length)
00449 {
00450 int colon = Text.indexOf (host, ':');
00451 if (colon >= 0)
00452 {
00453 if (colon < host.length)
00454 port = cast(int) Atoi.convert (host[colon+1..host.length]);
00455
00456 host = host[0..colon];
00457 }
00458 return host;
00459 }
00460
00461
00462 host = getBridge().getServer().getHost();
00463 return host;
00464 }
00465 }
00466
00467
00468
00469
00470
00471
00472
00473
00474 private class StartLine : IWritable
00475 {
00476
00477
00478 version (UseTokenizer)
00479 {
00480 Buffer buf;
00481 BoundToken path,
00482 method,
00483 protocol;
00484
00485
00486
00487
00488
00489 this()
00490 {
00491 buf = new Buffer;
00492
00493
00494 path = new BoundToken (Tokenizers.space);
00495 method = new BoundToken (Tokenizers.space);
00496 protocol = new BoundToken (Tokenizers.space);
00497 }
00498
00499
00500
00501
00502
00503 void parse (char[] line)
00504 {
00505
00506 buf.setValidContent(line);
00507
00508
00509 if ((method.next(buf) && path.next(buf)) ^ protocol.next(buf))
00510 {}
00511 else
00512 throw new IOException ("Invalid HTTP start-line: '"~line~"'");
00513 }
00514
00515
00516
00517
00518
00519 char[] getMethod()
00520 {
00521 return method.toString();
00522 }
00523
00524
00525
00526
00527
00528 char[] getPath()
00529 {
00530 return path.toString();
00531 }
00532
00533
00534
00535
00536
00537 char[] getProtocol()
00538 {
00539 return protocol.toString();
00540 }
00541
00542
00543
00544
00545
00546 char[] toString()
00547 {
00548 return getMethod()~" "~getPath()~" "~getProtocol();
00549 }
00550
00551
00552
00553
00554
00555 void write (IWriter writer)
00556 {
00557 writer.put(toString()).cr();
00558 }
00559 }
00560 else
00561 {
00562 private import mango.http.utils.TokenStack;
00563
00564 Token[] tokens;
00565
00566
00567
00568
00569
00570
00571
00572 this()
00573 {
00574 tokens = new Token[0];
00575 TokenStack.resize (tokens, 3);
00576 }
00577
00578
00579
00580
00581
00582
00583
00584 void parse (char[] line)
00585 {
00586
00587 int index;
00588 int anchor = 0;
00589 int count = 0;
00590
00591 do {
00592 index = Text.indexOf (line, ' ', anchor);
00593 if (index == -1)
00594 index = line.length;
00595 tokens[count].set(line[anchor..index]);
00596 anchor = index + 1;
00597 } while (++count < 3 && anchor < line.length);
00598
00599 if (count != 3 || anchor < line.length)
00600 throw new IOException ("Invalid HTTP start-line: '"~line~"'");
00601 }
00602
00603
00604
00605
00606
00607
00608
00609 char[] getMethod()
00610 {
00611 return tokens[0].toString();
00612 }
00613
00614
00615
00616
00617
00618
00619
00620 char[] getPath()
00621 {
00622 return tokens[1].toString();
00623 }
00624
00625
00626
00627
00628
00629
00630
00631 char[] getProtocol()
00632 {
00633 return tokens[2].toString();
00634 }
00635
00636
00637
00638
00639
00640
00641
00642 char[] toString()
00643 {
00644 return getMethod()~" "~getPath()~" "~getProtocol();
00645 }
00646
00647
00648
00649
00650
00651
00652
00653 void write (IWriter writer)
00654 {
00655 writer.put (toString).cr();
00656 }
00657 }
00658 }
00659