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.contenttype; 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 mimeType = "text/plain"; 32 string[string] parameters; 33 } 34 35 //---------------------------------------------------------------------------- 36 // extracts the content of a Content-Type MIME header. 37 38 MimeContentType extractMimeContentType(string fieldBody) 39 { 40 MimeContentType ct; 41 42 skipSpaceComment(fieldBody); 43 auto type = extractToken(fieldBody); 44 skipSpaceComment(fieldBody); 45 if (fieldBody.cfront != '/') throw new Exception("malformed MIME header"); 46 fieldBody.popFront(); 47 skipSpaceComment(fieldBody); 48 auto subType = extractToken(fieldBody); // TODO, the delimiter in this case is whitespace. 49 ct.mimeType = type~"/"~subType; 50 extractMimeParams(fieldBody,ct.parameters); 51 return ct; 52 } 53 54 //---------------------------------------------------------------------------- 55 56 MimeHeader toMimeHeader(ref MimeContentType contentType, bool asImf = false) 57 { 58 auto a = appender!string; 59 a.put(contentType.mimeType); 60 foreach (i,v; contentType.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 //----------------------------------------------------------------------------