1 module soup.ClientContext; 2 3 private import gio.IOStream; 4 private import gio.Socket: GIOSocket = Socket; 5 private import gio.SocketAddress; 6 private import glib.Str; 7 private import gobject.ObjectG; 8 private import soup.Address; 9 private import soup.AuthDomain; 10 private import soup.Socket; 11 private import soup.c.functions; 12 public import soup.c.types; 13 14 15 /** 16 * A #SoupClientContext provides additional information about the 17 * client making a particular request. In particular, you can use 18 * soup_client_context_get_auth_domain() and 19 * soup_client_context_get_auth_user() to determine if HTTP 20 * authentication was used successfully. 21 * 22 * soup_client_context_get_remote_address() and/or 23 * soup_client_context_get_host() can be used to get information for 24 * logging or debugging purposes. soup_client_context_get_gsocket() may 25 * also be of use in some situations (eg, tracking when multiple 26 * requests are made on the same connection). 27 */ 28 public class ClientContext 29 { 30 /** the main Gtk struct */ 31 protected SoupClientContext* soupClientContext; 32 protected bool ownedRef; 33 34 /** Get the main Gtk struct */ 35 public SoupClientContext* getClientContextStruct(bool transferOwnership = false) 36 { 37 if (transferOwnership) 38 ownedRef = false; 39 return soupClientContext; 40 } 41 42 /** the main Gtk struct as a void* */ 43 protected void* getStruct() 44 { 45 return cast(void*)soupClientContext; 46 } 47 48 /** 49 * Sets our main struct and passes it to the parent class. 50 */ 51 public this (SoupClientContext* soupClientContext, bool ownedRef = false) 52 { 53 this.soupClientContext = soupClientContext; 54 this.ownedRef = ownedRef; 55 } 56 57 58 /** */ 59 public static GType getType() 60 { 61 return soup_client_context_get_type(); 62 } 63 64 /** 65 * Retrieves the #SoupAddress associated with the remote end 66 * of a connection. 67 * 68 * Deprecated: Use soup_client_context_get_remote_address(), which returns 69 * a #GSocketAddress. 70 * 71 * Returns: the #SoupAddress 72 * associated with the remote end of a connection, it may be 73 * %NULL if you used soup_server_accept_iostream(). 74 */ 75 public Address getAddress() 76 { 77 auto __p = soup_client_context_get_address(soupClientContext); 78 79 if(__p is null) 80 { 81 return null; 82 } 83 84 return ObjectG.getDObject!(Address)(cast(SoupAddress*) __p); 85 } 86 87 /** 88 * Checks whether the request associated with @client has been 89 * authenticated, and if so returns the #SoupAuthDomain that 90 * authenticated it. 91 * 92 * Returns: a #SoupAuthDomain, or 93 * %NULL if the request was not authenticated. 94 */ 95 public AuthDomain getAuthDomain() 96 { 97 auto __p = soup_client_context_get_auth_domain(soupClientContext); 98 99 if(__p is null) 100 { 101 return null; 102 } 103 104 return ObjectG.getDObject!(AuthDomain)(cast(SoupAuthDomain*) __p); 105 } 106 107 /** 108 * Checks whether the request associated with @client has been 109 * authenticated, and if so returns the username that the client 110 * authenticated as. 111 * 112 * Returns: the authenticated-as user, or %NULL if 113 * the request was not authenticated. 114 */ 115 public string getAuthUser() 116 { 117 return Str.toString(soup_client_context_get_auth_user(soupClientContext)); 118 } 119 120 /** 121 * Retrieves the #GSocket that @client is associated with. 122 * 123 * If you are using this method to observe when multiple requests are 124 * made on the same persistent HTTP connection (eg, as the ntlm-test 125 * test program does), you will need to pay attention to socket 126 * destruction as well (eg, by using weak references), so that you do 127 * not get fooled when the allocator reuses the memory address of a 128 * previously-destroyed socket to represent a new socket. 129 * 130 * Returns: the #GSocket that @client is 131 * associated with, %NULL if you used soup_server_accept_iostream(). 132 * 133 * Since: 2.48 134 */ 135 public GIOSocket getGsocket() 136 { 137 auto __p = soup_client_context_get_gsocket(soupClientContext); 138 139 if(__p is null) 140 { 141 return cast(GIOSocket)null; 142 } 143 144 return ObjectG.getDObject!(GIOSocket)(cast(GSocket*) __p); 145 } 146 147 /** 148 * Retrieves the IP address associated with the remote end of a 149 * connection. 150 * 151 * Returns: the IP address associated with the remote 152 * end of a connection, it may be %NULL if you used 153 * soup_server_accept_iostream(). 154 */ 155 public string getHost() 156 { 157 return Str.toString(soup_client_context_get_host(soupClientContext)); 158 } 159 160 /** 161 * Retrieves the #GSocketAddress associated with the local end 162 * of a connection. 163 * 164 * Returns: the #GSocketAddress 165 * associated with the local end of a connection, it may be 166 * %NULL if you used soup_server_accept_iostream(). 167 * 168 * Since: 2.48 169 */ 170 public SocketAddress getLocalAddress() 171 { 172 auto __p = soup_client_context_get_local_address(soupClientContext); 173 174 if(__p is null) 175 { 176 return null; 177 } 178 179 return ObjectG.getDObject!(SocketAddress)(cast(GSocketAddress*) __p); 180 } 181 182 /** 183 * Retrieves the #GSocketAddress associated with the remote end 184 * of a connection. 185 * 186 * Returns: the #GSocketAddress 187 * associated with the remote end of a connection, it may be 188 * %NULL if you used soup_server_accept_iostream(). 189 * 190 * Since: 2.48 191 */ 192 public SocketAddress getRemoteAddress() 193 { 194 auto __p = soup_client_context_get_remote_address(soupClientContext); 195 196 if(__p is null) 197 { 198 return null; 199 } 200 201 return ObjectG.getDObject!(SocketAddress)(cast(GSocketAddress*) __p); 202 } 203 204 /** 205 * Retrieves the #SoupSocket that @client is associated with. 206 * 207 * If you are using this method to observe when multiple requests are 208 * made on the same persistent HTTP connection (eg, as the ntlm-test 209 * test program does), you will need to pay attention to socket 210 * destruction as well (either by using weak references, or by 211 * connecting to the #SoupSocket::disconnected signal), so that you do 212 * not get fooled when the allocator reuses the memory address of a 213 * previously-destroyed socket to represent a new socket. 214 * 215 * Deprecated: use soup_client_context_get_gsocket(), which returns 216 * a #GSocket. 217 * 218 * Returns: the #SoupSocket that @client is 219 * associated with. 220 */ 221 public Socket getSocket() 222 { 223 auto __p = soup_client_context_get_socket(soupClientContext); 224 225 if(__p is null) 226 { 227 return null; 228 } 229 230 return ObjectG.getDObject!(Socket)(cast(SoupSocket*) __p); 231 } 232 233 /** 234 * "Steals" the HTTP connection associated with @client from its 235 * #SoupServer. This happens immediately, regardless of the current 236 * state of the connection; if the response to the current 237 * #SoupMessage has not yet finished being sent, then it will be 238 * discarded; you can steal the connection from a 239 * #SoupMessage:wrote-informational or #SoupMessage:wrote-body signal 240 * handler if you need to wait for part or all of the response to be 241 * sent. 242 * 243 * Note that when calling this function from C, @client will most 244 * likely be freed as a side effect. 245 * 246 * Returns: the #GIOStream formerly associated 247 * with @client (or %NULL if @client was no longer associated with a 248 * connection). No guarantees are made about what kind of #GIOStream 249 * is returned. 250 * 251 * Since: 2.50 252 */ 253 public IOStream stealConnection() 254 { 255 auto __p = soup_client_context_steal_connection(soupClientContext); 256 257 if(__p is null) 258 { 259 return null; 260 } 261 262 return ObjectG.getDObject!(IOStream)(cast(GIOStream*) __p, true); 263 } 264 }