1 module soup.ContentSniffer; 2 3 private import glib.ConstructionException; 4 private import glib.HashTable; 5 private import glib.Str; 6 private import gobject.ObjectG; 7 private import soup.Buffer; 8 private import soup.Message; 9 private import soup.SessionFeatureIF; 10 private import soup.SessionFeatureT; 11 private import soup.c.functions; 12 public import soup.c.types; 13 14 15 /** 16 * A #SoupContentSniffer tries to detect the actual content type of 17 * the files that are being downloaded by looking at some of the data 18 * before the #SoupMessage emits its #SoupMessage::got-headers signal. 19 * #SoupContentSniffer implements #SoupSessionFeature, so you can add 20 * content sniffing to a session with soup_session_add_feature() or 21 * soup_session_add_feature_by_type(). 22 * 23 * Since: 2.28 24 */ 25 public class ContentSniffer : ObjectG, SessionFeatureIF 26 { 27 /** the main Gtk struct */ 28 protected SoupContentSniffer* soupContentSniffer; 29 30 /** Get the main Gtk struct */ 31 public SoupContentSniffer* getContentSnifferStruct(bool transferOwnership = false) 32 { 33 if (transferOwnership) 34 ownedRef = false; 35 return soupContentSniffer; 36 } 37 38 /** the main Gtk struct as a void* */ 39 protected override void* getStruct() 40 { 41 return cast(void*)soupContentSniffer; 42 } 43 44 /** 45 * Sets our main struct and passes it to the parent class. 46 */ 47 public this (SoupContentSniffer* soupContentSniffer, bool ownedRef = false) 48 { 49 this.soupContentSniffer = soupContentSniffer; 50 super(cast(GObject*)soupContentSniffer, ownedRef); 51 } 52 53 // add the SessionFeature capabilities 54 mixin SessionFeatureT!(SoupContentSniffer); 55 56 57 /** */ 58 public static GType getType() 59 { 60 return soup_content_sniffer_get_type(); 61 } 62 63 /** 64 * Creates a new #SoupContentSniffer. 65 * 66 * Returns: a new #SoupContentSniffer 67 * 68 * Since: 2.28 69 * 70 * Throws: ConstructionException GTK+ fails to create the object. 71 */ 72 public this() 73 { 74 auto __p = soup_content_sniffer_new(); 75 76 if(__p is null) 77 { 78 throw new ConstructionException("null returned by new"); 79 } 80 81 this(cast(SoupContentSniffer*) __p, true); 82 } 83 84 /** 85 * Gets the number of bytes @sniffer needs in order to properly sniff 86 * a buffer. 87 * 88 * Returns: the number of bytes to sniff 89 * 90 * Since: 2.28 91 */ 92 public size_t getBufferSize() 93 { 94 return soup_content_sniffer_get_buffer_size(soupContentSniffer); 95 } 96 97 /** 98 * Sniffs @buffer to determine its Content-Type. The result may also 99 * be influenced by the Content-Type declared in @msg's response 100 * headers. 101 * 102 * Params: 103 * msg = the message to sniff 104 * buffer = a buffer containing the start of @msg's response body 105 * params = return 106 * location for Content-Type parameters (eg, "charset"), or %NULL 107 * 108 * Returns: the sniffed Content-Type of @buffer; this will never be %NULL, 109 * but may be "application/octet-stream". 110 * 111 * Since: 2.28 112 */ 113 public string sniff(Message msg, Buffer buffer, out HashTable params) 114 { 115 GHashTable* outparams = null; 116 117 auto retStr = soup_content_sniffer_sniff(soupContentSniffer, (msg is null) ? null : msg.getMessageStruct(), (buffer is null) ? null : buffer.getBufferStruct(), &outparams); 118 119 params = new HashTable(outparams); 120 121 scope(exit) Str.freeString(retStr); 122 return Str.toString(retStr); 123 } 124 }