1 //Written in the D programming language 2 /* 3 * MIME Content-Type header. 4 * 5 * Copyright: Copyright (C) 2013 Jaypha 6 * 7 * Distributed under the Boost Software License, Version 1.0. 8 * (See http://www.boost.org/LICENSE_1_0.txt) 9 * 10 * Authors: Jason den Dulk 11 */ 12 13 /* 14 * RFCs : 2045 15 */ 16 17 module jaypha.inet.mime.content_type; 18 19 import jaypha.inet.mime.reading; 20 import jaypha.inet.imf.writing; 21 22 import std.array; 23 24 //----------------------------------------------------------------------------- 25 // Content Type info 26 27 struct MimeContentType 28 { 29 enum headerName = "Content-Type"; 30 31 string type = "text/plain"; 32 string[string] parameters; 33 } 34 35 //---------------------------------------------------------------------------- 36 // extracts the content of a Content-Type MIME header. 37 38 MimeContentType extractMimeContentType(string s) 39 { 40 MimeContentType ct; 41 42 skipSpaceComment(s); 43 auto type = extractToken(s); 44 skipSpaceComment(s); 45 if (s.cfront != '/') throw new Exception("malformed MIME header"); 46 s.popFront(); 47 skipSpaceComment(s); 48 auto subType = extractToken(s); // TODO, the delimiter in this case is whitespace. 49 ct.type = type~"/"~subType; 50 extractMimeParams(s,ct.parameters); 51 return ct; 52 } 53 54 //---------------------------------------------------------------------------- 55 56 MimeHeader toMimeHeader(ref MimeContentType ct, bool asImf = false) 57 { 58 auto a = appender!string; 59 a.put(ct.type); 60 foreach (i,v;ct.parameters) 61 a.put("; "~i~"=\""~v~"\""); 62 63 if (asImf) 64 return unstructuredHeader(MimeContentType.headerName,a.data); 65 else 66 return MimeHeader(MimeContentType.headerName,a.data); 67 } 68 69 //----------------------------------------------------------------------------