1 //
2 //  ========================================================================
3 //  Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
4 //  ------------------------------------------------------------------------
5 //  All rights reserved. This program and the accompanying materials
6 //  are made available under the terms of the Eclipse Public License v1.0
7 //  and Apache License v2.0 which accompanies this distribution.
8 //
9 //      The Eclipse Public License is available at
10 //      http://www.eclipse.org/legal/epl-v10.html
11 //
12 //      The Apache License v2.0 is available at
13 //      http://www.opensource.org/licenses/apache2.0.php
14 //
15 //  You may elect to redistribute this code under either of these licenses.
16 //  ========================================================================
17 //
18 
19 module hunt.http.codec.http.model.HttpHeaderValue;
20 
21 import hunt.http.codec.http.model.HttpHeader;
22 import hunt.util.traits;
23 
24 import std.algorithm;
25 import std.string;
26 // import hunt.container.ByteBuffer;
27 // import java.nio.charset.StandardCharsets;
28 // import java.util.EnumSet;
29 
30 // import hunt.http.utils.collection.ArrayTrie;
31 // import hunt.http.utils.collection.Trie;
32 
33 /**
34  * 
35  */
36 struct HttpHeaderValue {
37 
38 	enum HttpHeaderValue CLOSE = HttpHeaderValue("close");
39     enum HttpHeaderValue CHUNKED = HttpHeaderValue("chunked");
40     enum HttpHeaderValue GZIP = HttpHeaderValue("gzip");
41     enum HttpHeaderValue IDENTITY = HttpHeaderValue("identity");
42     enum HttpHeaderValue KEEP_ALIVE = HttpHeaderValue("keep-alive");
43     enum HttpHeaderValue CONTINUE = HttpHeaderValue("100-continue");
44     enum HttpHeaderValue PROCESSING = HttpHeaderValue("102-processing");
45     enum HttpHeaderValue TE = HttpHeaderValue("TE");
46     enum HttpHeaderValue BYTES = HttpHeaderValue("bytes");
47     enum HttpHeaderValue NO_CACHE = HttpHeaderValue("no-cache");
48     enum HttpHeaderValue UPGRADE = HttpHeaderValue("Upgrade");
49     enum HttpHeaderValue UNKNOWN = HttpHeaderValue("::UNKNOWN::");	
50 
51 
52 	private static HttpHeader[] __known = [
53 			HttpHeader.CONNECTION, 
54 			HttpHeader.TRANSFER_ENCODING,
55 			HttpHeader.CONTENT_ENCODING
56 			];
57 	
58 	__gshared HttpHeaderValue[string] CACHE; 
59 
60 	shared static this() {
61 		foreach (ref HttpHeaderValue value ; HttpHeaderValue.values())
62         {
63             if (value != UNKNOWN)
64                 CACHE[value.toString()] = value;
65 		}
66 	}
67 
68 	mixin GetConstantValues!(HttpHeaderValue);
69 
70 	private string _string;
71 	// private ByteBuffer buffer;
72 	
73 	private this(string s) {
74 		_string = s;
75 		// buffer = ByteBuffer.wrap(s.getBytes(StandardCharsets.UTF_8));
76 	}
77 
78 	// ByteBuffer toBuffer() {
79 	// 	return buffer.asReadOnlyBuffer();
80 	// }
81 
82     bool isSame(string s) {
83         return s.length != 0 && std..string.icmp(_string, s) == 0;
84     }
85 
86 	string asString() {
87 		return _string;
88 	}
89 
90 	string toString() {
91 		return _string;
92 	}
93 
94 	static bool hasKnownValues(HttpHeader header) {
95 		if (header == HttpHeader.Null)
96 			return false;
97 		return __known.canFind(header);
98 	}
99 }