1 /*
2  * Archttp - A highly performant web framework written in D.
3  *
4  * Copyright (C) 2021-2022 Kerisy.com
5  *
6  * Website: https://www.kerisy.com
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module archttp.codec.HttpDecoder;
13 
14 import gear.codec.Decoder;
15 import gear.codec.Encoder;
16 
17 import nbuff;
18 
19 import gear.event;
20 
21 import archttp.HttpRequestParser;
22 import archttp.HttpRequest;
23 import archttp.HttpContext;
24 
25 import gear.logging;
26 
27 class HttpDecoder : Decoder!HttpRequest
28 {
29     private
30     {
31         HttpRequestParser _parser = new HttpRequestParser;
32         HttpRequest _request;
33     }
34     
35     override long Decode(ref Nbuff buffer, ref HttpRequest request)
36     {
37         long result = _parser.parse(cast(string) buffer.data().data());
38         
39         if ( ParserStatus.COMPLETED == _parser.parserStatus() )
40         {
41             request = _parser.request();
42 
43             _parser.reset();
44             buffer.pop(result);
45             
46             return result;
47         }
48 
49         if ( ParserStatus.PARTIAL == _parser.parserStatus() )
50         {
51             return 0;
52         }
53         
54         return -1;
55     }
56 }