1 /*
2  * Hunt - A high-level D Programming Language Web framework that encourages rapid development and clean, pragmatic design.
3  *
4  * Copyright (C) 2015-2019, HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11  
12 module hunt.framework.util.String;
13 
14 import std.datetime;
15 import std.conv;
16 import std.string;
17 import std.array;
18 import std.path;
19 
20 pragma(inline)
21 string printDate(DateTime date)
22 {
23     return format("%.3s, %02d %.3s %d %02d:%02d:%02d GMT", // could be UTC too
24         to!string(date.dayOfWeek).capitalize, date.day, to!string(date.month)
25         .capitalize, date.year, date.hour, date.minute, date.second);
26 }
27 
28 /// convert time to RFC822 format string
29 string toRFC822DateTimeString(SysTime systime)
30 {
31     Appender!string ret;
32 
33     DateTime dt = cast(DateTime)systime;
34     Date date = dt.date;
35 
36     ret.put(to!string(date.dayOfWeek).capitalize);
37     ret.put(", ");
38     ret.put(rightJustify(to!string(date.day), 2, '0'));
39     ret.put(" ");
40     ret.put(to!string(date.month).capitalize);
41     ret.put(" ");
42     ret.put(to!string(date.year));
43     ret.put(" ");
44 
45     TimeOfDay time = cast(TimeOfDay)systime;
46     int tz_offset = cast(int)systime.utcOffset.total!"minutes";
47 
48     ret.put(rightJustify(to!string(time.hour), 2, '0'));
49     ret.put(":");
50     ret.put(rightJustify(to!string(time.minute), 2, '0'));
51     ret.put(":");
52     ret.put(rightJustify(to!string(time.second), 2, '0'));
53 
54     if (tz_offset == 0)
55     {
56         ret.put(" GMT");
57     }
58     else
59     {
60         ret.put(" " ~ (tz_offset >= 0 ? "+" : "-"));
61 
62         if (tz_offset < 0) tz_offset = -tz_offset;
63         ret.put(rightJustify(to!string(tz_offset / 60), 2, '0'));
64         ret.put(rightJustify(to!string(tz_offset % 60), 2, '0'));
65     }
66 
67     return ret.data;
68 }
69 
70 ///mime types
71 enum MimeTypes = [
72     ".ez" : "application/andrew-inset", 
73     ".hqx" : "application/mac-binhex40", 
74     ".cpt" : "application/mac-compactpro", 
75     ".doc" : "application/msword", 
76     ".bin" : "application/octet-stream", 
77     ".dms" : "application/octet-stream", 
78     ".lha" : "application/octet-stream", 
79     ".lzh" : "application/octet-stream", 
80     ".exe" : "application/octet-stream", 
81     ".class" : "application/octet-stream", 
82     ".so" : "application/octet-stream", 
83     ".dll" : "application/octet-stream", 
84     ".oda" : "application/oda", 
85     ".pdf" : "application/pdf", 
86     ".ai" : "application/postscript", 
87     ".eps" : "application/postscript", 
88     ".ps" : "application/postscript", 
89     ".smi" : "application/smil", 
90     ".smil" : "application/smil", 
91     ".wbxml" : "application/vnd.wap.wbxml", 
92     ".wmlc" : "application/vnd.wap.wmlc", 
93     ".wmlsc" : "application/vnd.wap.wmlscriptc", 
94     ".bcpio" : "application/x-bcpio", 
95     ".vcd" : "application/x-cdlink", 
96     ".pgn" : "application/x-chess-pgn", 
97     ".cpio" : "application/x-cpio", 
98     ".csh" : "application/x-csh", 
99     ".dcr" : "application/x-director", 
100     ".dir" : "application/x-director", 
101     ".dxr" : "application/x-director", 
102     ".dvi" : "application/x-dvi", 
103     ".spl" : "application/x-futuresplash", 
104     ".gtar" : "application/x-gtar", 
105     ".hdf" : "application/x-hdf", 
106     ".js" : "application/x-javascript", 
107     ".skp" : "application/x-koan", 
108     ".skd" : "application/x-koan", 
109     ".skt" : "application/x-koan", 
110     ".skm" : "application/x-koan", 
111     ".latex" : "application/x-latex", 
112     ".nc" : "application/x-netcdf", 
113     ".cdf" : "application/x-netcdf", 
114     ".sh" : "application/x-sh", 
115     ".shar" : "application/x-shar", 
116     ".swf" : "application/x-shockwave-flash", 
117     ".sit" : "application/x-stuffit", 
118     ".sv4cpio" : "application/x-sv4cpio", 
119     ".sv4crc" : "application/x-sv4crc", 
120     ".tar" : "application/x-tar", 
121     ".tcl" : "application/x-tcl", 
122     ".tex" : "application/x-tex", 
123     ".texinfo" : "application/x-texinfo", 
124     ".texi" : "application/x-texinfo", 
125     ".t" : "application/x-troff", 
126     ".tr" : "application/x-troff", 
127     ".roff" : "application/x-troff", 
128     ".man" : "application/x-troff-man", 
129     ".me" : "application/x-troff-me", 
130     ".ms" : "application/x-troff-ms", 
131     ".ustar" : "application/x-ustar", 
132     ".src" : "application/x-wais-source", 
133     ".xhtml" : "application/xhtml+xml", 
134     ".xht" : "application/xhtml+xml", 
135     ".zip" : "application/zip", 
136     ".au" : "audio/basic", 
137     ".snd" : "audio/basic", 
138     ".mid" : "audio/midi", 
139     ".midi" : "audio/midi", 
140     ".kar" : "audio/midi", 
141     ".mpga" : "audio/mpeg", 
142     ".mp2" : "audio/mpeg", 
143     ".mp3" : "audio/mpeg", 
144     ".aif" : "audio/x-aiff", 
145     ".aiff" : "audio/x-aiff", 
146     ".aifc" : "audio/x-aiff", 
147     ".m3u" : "audio/x-mpegurl", 
148     ".ram" : "audio/x-pn-realaudio", 
149     ".rm" : "audio/x-pn-realaudio", 
150     ".rpm" : "audio/x-pn-realaudio-plugin", 
151     ".ra" : "audio/x-realaudio", 
152     ".wav" : "audio/x-wav", 
153     ".pdb" : "chemical/x-pdb", 
154     ".xyz" : "chemical/x-xyz", 
155     ".bmp" : "image/bmp", 
156     ".gif" : "image/gif", 
157     ".ief" : "image/ief", 
158     ".jpeg" : "image/jpeg", 
159     ".jpg" : "image/jpeg", 
160     ".jpe" : "image/jpeg", 
161     ".png" : "image/png", 
162     ".tiff" : "image/tiff", 
163     ".tif" : "image/tif", 
164     ".djvu" : "image/vnd.djvu", 
165     ".djv" : "image/vnd.djvu", 
166     ".wbmp" : "image/vnd.wap.wbmp", 
167     ".ras" : "image/x-cmu-raster", 
168     ".pnm" : "image/x-portable-anymap", 
169     ".pbm" : "image/x-portable-bitmap", 
170     ".pgm" : "image/x-portable-graymap", 
171     ".ppm" : "image/x-portable-pixmap", 
172     ".rgb" : "image/x-rgb", 
173     ".xbm" : "image/x-xbitmap", 
174     ".xpm" : "image/x-xpixmap", 
175     ".xwd" : "image/x-windowdump", 
176     ".igs" : "model/iges", 
177     ".iges" : "model/iges", 
178     ".msh" : "model/mesh", 
179     ".mesh" : "model/mesh", 
180     ".silo" : "model/mesh", 
181     ".wrl" : "model/vrml", 
182     ".vrml" : "model/vrml", 
183     ".css" : "text/css", 
184     ".html" : "text/html", 
185     ".htm" : "text/html", 
186     ".asc" : "text/plain", 
187     ".txt" : "text/plain", 
188     ".rtx" : "text/richtext", 
189     ".rtf" : "text/rtf", 
190     ".sgml" : "text/sgml", 
191     ".sgm" : "text/sgml", 
192     ".tsv" : "text/tab-seperated-values", 
193     ".wml" : "text/vnd.wap.wml", 
194     ".wmls" : "text/vnd.wap.wmlscript", 
195     ".etx" : "text/x-setext", 
196     ".xml" : "text/xml", 
197     ".xsl" : "text/xml", 
198     ".mpeg" : "video/mpeg", 
199     ".mpg" : "video/mpeg", 
200     ".mpe" : "video/mpeg", 
201     ".qt" : "video/quicktime", 
202     ".mov" : "video/quicktime", 
203     ".mxu" : "video/vnd.mpegurl", 
204     ".avi" : "video/x-msvideo", 
205     ".movie" : "video/x-sgi-movie", 
206     ".ice" : "x-conference-xcooltalk" 
207 ];
208 
209 ///get mime content type by extension
210 string mimeContentType(string ext)
211 {
212     return MimeTypes.get(ext, "application/octet-stream");
213 }
214 
215 /// get mime content type by filename
216 string getMimeTypeByFilename(string filename)
217 {
218     string ext = extension(filename);
219 
220     return mimeContentType(ext);
221 }
222 
223 /// merge multiple strings into a long string
224 string mergeString(string[] params)
225 {
226     Appender!string ret;
227 
228     foreach(str; params)
229     {
230         ret.put(str);
231     }
232 
233     return ret.data;
234 }