1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* soup-session.c
*
* Copyright (C) 2000-2003, Ximian, Inc.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include "soup-address.h"
#include "soup-auth.h"
#include "soup-auth-basic.h"
#include "soup-auth-digest.h"
#include "soup-auth-manager-ntlm.h"
#include "soup-connection.h"
#include "soup-marshal.h"
#include "soup-message-private.h"
#include "soup-message-queue.h"
#include "soup-misc.h"
#include "soup-proxy-resolver-static.h"
#include "soup-proxy-uri-resolver.h"
#include "soup-session.h"
#include "soup-session-feature.h"
#include "soup-session-private.h"
#include "soup-socket.h"
#include "soup-ssl.h"
#include "soup-uri.h"
/**
* SECTION:soup-session
* @short_description: Soup session state object
*
* #SoupSession is the object that controls client-side HTTP. A
* #SoupSession encapsulates all of the state that libsoup is keeping
* on behalf of your program; cached HTTP connections, authentication
* information, etc.
*
* Most applications will only need a single #SoupSession; the primary
* reason you might need multiple sessions is if you need to have
* multiple independent authentication contexts. (Eg, you are
* connecting to a server and authenticating as two different users at
* different times; the easiest way to ensure that each #SoupMessage
* is sent with the authentication information you intended is to use
* one session for the first user, and a second session for the other
* user.)
*
* #SoupSession itself is an abstract class, with two subclasses. If
* you are using the glib main loop, you will generally want to use
* #SoupSessionAsync, which uses non-blocking I/O and callbacks. On
* the other hand, if your application is threaded and you want to do
* synchronous I/O in a separate thread from the UI, use
* #SoupSessionSync.
**/
typedef struct {
SoupURI *uri;
SoupAddress *addr;
GSList *connections; /* CONTAINS: SoupConnection */
guint num_conns;
guint num_messages;
} SoupSessionHost;
typedef struct {
char *ssl_ca_file;
SoupSSLCredentials *ssl_creds;
gboolean ssl_strict;
SoupMessageQueue *queue;
char *user_agent;
char *accept_language;
gboolean accept_language_auto;
GSList *features;
GHashTable *features_cache;
GHashTable *hosts; /* char* -> SoupSessionHost */
GHashTable *conns; /* SoupConnection -> SoupSessionHost */
guint num_conns;
guint max_conns, max_conns_per_host;
guint io_timeout, idle_timeout;
/* Must hold the host_lock before potentially creating a
* new SoupSessionHost, or adding/removing a connection.
* Must not emit signals or destroy objects while holding it.
*/
GMutex *host_lock;
GMainContext *async_context;
GResolver *resolver;
} SoupSessionPrivate;
#define SOUP_SESSION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SOUP_TYPE_SESSION, SoupSessionPrivate))
static void free_host (SoupSessionHost *host);
static void queue_message (SoupSession *session, SoupMessage *msg,
SoupSessionCallback callback, gpointer user_data);
static void requeue_message (SoupSession *session, SoupMessage *msg);
static void cancel_message (SoupSession *session, SoupMessage *msg,
guint status_code);
static void auth_required (SoupSession *session, SoupMessage *msg,
SoupAuth *auth, gboolean retrying);
static void auth_manager_authenticate (SoupAuthManager *manager,
SoupMessage *msg, SoupAuth *auth,
gboolean retrying, gpointer user_data);
#define SOUP_SESSION_MAX_CONNS_DEFAULT 10
#define SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT 2
#define SOUP_SESSION_USER_AGENT_BASE "libsoup/" PACKAGE_VERSION
G_DEFINE_TYPE (SoupSession, soup_session, G_TYPE_OBJECT)
enum {
REQUEST_QUEUED,
REQUEST_STARTED,
REQUEST_UNQUEUED,
AUTHENTICATE,
CONNECTION_CREATED,
TUNNELING,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
enum {
PROP_0,
PROP_PROXY_URI,
PROP_MAX_CONNS,
PROP_MAX_CONNS_PER_HOST,
PROP_USE_NTLM,
PROP_SSL_CA_FILE,
PROP_SSL_STRICT,
PROP_ASYNC_CONTEXT,
PROP_TIMEOUT,
PROP_USER_AGENT,
PROP_ACCEPT_LANGUAGE,
PROP_ACCEPT_LANGUAGE_AUTO,
PROP_IDLE_TIMEOUT,
PROP_ADD_FEATURE,
PROP_ADD_FEATURE_BY_TYPE,
PROP_REMOVE_FEATURE_BY_TYPE,
LAST_PROP
};
static void set_property (GObject *object, guint prop_id,
const GValue *value, GParamSpec *pspec);
static void get_property (GObject *object, guint prop_id,
GValue *value, GParamSpec *pspec);
static void
soup_session_init (SoupSession *session)
{
SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
SoupAuthManager *auth_manager;
priv->queue = soup_message_queue_new (session);
priv->host_lock = g_mutex_new ();
priv->hosts = g_hash_table_new_full (soup_uri_host_hash,
soup_uri_host_equal,
NULL, (GDestroyNotify)free_host);
priv->conns = g_hash_table_new (NULL, NULL);
priv->max_conns = SOUP_SESSION_MAX_CONNS_DEFAULT;
priv->max_conns_per_host = SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT;
priv->features_cache = g_hash_table_new (NULL, NULL);
auth_manager = g_object_new (SOUP_TYPE_AUTH_MANAGER_NTLM,
SOUP_AUTH_MANAGER_NTLM_USE_NTLM, FALSE,
NULL);
g_signal_connect (auth_manager, "authenticate",
G_CALLBACK (auth_manager_authenticate), session);
soup_auth_manager_add_type (auth_manager, SOUP_TYPE_AUTH_BASIC);
soup_auth_manager_add_type (auth_manager, SOUP_TYPE_AUTH_DIGEST);
soup_session_add_feature (session, SOUP_SESSION_FEATURE (auth_manager));
g_object_unref (auth_manager);
/* We'll be doing DNS continuously-ish while the session is active,
* so hold a ref on the default GResolver.
*/
priv->resolver = g_resolver_get_default ();
priv->ssl_strict = TRUE;
}
static void
dispose (GObject *object)
{
SoupSession *session = SOUP_SESSION (object);
SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
soup_session_abort (session);
while (priv->features)
soup_session_remove_feature (session, priv->features->data);
G_OBJECT_CLASS (soup_session_parent_class)->dispose (object);
}
static void
finalize (GObject *object)
{
SoupSession *session = SOUP_SESSION (object);
SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
soup_message_queue_destroy (priv->queue);
g_mutex_free (priv->host_lock);
g_hash_table_destroy (priv->hosts);
g_hash_table_destroy (priv->conns);
g_free (priv->user_agent);
g_free (priv->accept_language);
if (priv->ssl_ca_file)
g_free (priv->ssl_ca_file);
if (priv->ssl_creds)
soup_ssl_free_client_credentials (priv->ssl_creds);
if (priv->async_context)
g_main_context_unref (priv->async_context);
g_hash_table_destroy (priv->features_cache);
g_object_unref (priv->resolver);
G_OBJECT_CLASS (soup_session_parent_class)->finalize (object);
}
static void
soup_session_class_init (SoupSessionClass *session_class)
{
GObjectClass *object_class = G_OBJECT_CLASS (session_class);
g_type_class_add_private (session_class, sizeof (SoupSessionPrivate));
/* virtual method definition */
session_class->queue_message = queue_message;
session_class->requeue_message = requeue_message;
session_class->cancel_message = cancel_message;
session_class->auth_required = auth_required;
/* virtual method override */
object_class->dispose = dispose;
object_class->finalize = finalize;
object_class->set_property = set_property;
object_class->get_property = get_property;
/* signals */
/**
* SoupSession::request-queued:
* @session: the session
* @msg: the request that was queued
*
* Emitted when a request is queued on @session. (Note that
* "queued" doesn't just mean soup_session_queue_message();
* soup_session_send_message() implicitly queues the message
* as well.)
*
* When sending a request, first #SoupSession::request_queued
* is emitted, indicating that the session has become aware of
* the request.
*
* Once a connection is available to send the request on, the
* session emits #SoupSession::request_started. Then, various
* #SoupMessage signals are emitted as the message is
* processed. If the message is requeued, it will emit
* #SoupMessage::restarted, which will then be followed by
* another #SoupSession::request_started and another set of
* #SoupMessage signals when the message is re-sent.
*
* Eventually, the message will emit #SoupMessage::finished.
* Normally, this signals the completion of message
* processing. However, it is possible that the application
* will requeue the message from the "finished" handler (or
* equivalently, from the soup_session_queue_message()
* callback). In that case, the process will loop back to
* #SoupSession::request_started.
*
* Eventually, a message will reach "finished" and not be
* requeued. At that point, the session will emit
* #SoupSession::request_unqueued to indicate that it is done
* with the message.
*
* To sum up: #SoupSession::request_queued and
* #SoupSession::request_unqueued are guaranteed to be emitted
* exactly once, but #SoupSession::request_started and
* #SoupMessage::finished (and all of the other #SoupMessage
* signals) may be invoked multiple times for a given message.
*
* Since: 2.4.1
**/
signals[REQUEST_QUEUED] =
g_signal_new ("request-queued",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST,
0, /* FIXME? */
NULL, NULL,
soup_marshal_NONE__OBJECT,
G_TYPE_NONE, 1,
SOUP_TYPE_MESSAGE);
/**
* SoupSession::request-started:
* @session: the session
* @msg: the request being sent
* @socket: the socket the request is being sent on
*
* Emitted just before a request is sent. See
* #SoupSession::request_queued for a detailed description of
* the message lifecycle within a session.
**/
signals[REQUEST_STARTED] =
g_signal_new ("request-started",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (SoupSessionClass, request_started),
NULL, NULL,
soup_marshal_NONE__OBJECT_OBJECT,
G_TYPE_NONE, 2,
SOUP_TYPE_MESSAGE,
SOUP_TYPE_SOCKET);
/**
* SoupSession::request-unqueued:
* @session: the session
* @msg: the request that was unqueued
*
* Emitted when a request is removed from @session's queue,
* indicating that @session is done with it. See
* #SoupSession::request_queued for a detailed description of the
* message lifecycle within a session.
*
* Since: 2.4.1
**/
signals[REQUEST_UNQUEUED] =
g_signal_new ("request-unqueued",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST,
0, /* FIXME? */
NULL, NULL,
soup_marshal_NONE__OBJECT,
G_TYPE_NONE, 1,
SOUP_TYPE_MESSAGE);
/**
* SoupSession::authenticate:
* @session: the session
* @msg: the #SoupMessage being sent
* @auth: the #SoupAuth to authenticate
* @retrying: %TRUE if this is the second (or later) attempt
*
* Emitted when the session requires authentication. If
* credentials are available call soup_auth_authenticate() on
* @auth. If these credentials fail, the signal will be
* emitted again, with @retrying set to %TRUE, which will
* continue until you return without calling
* soup_auth_authenticate() on @auth.
*
* Note that this may be emitted before @msg's body has been
* fully read.
*
* If you call soup_session_pause_message() on @msg before
* returning, then you can authenticate @auth asynchronously
* (as long as you g_object_ref() it to make sure it doesn't
* get destroyed), and then unpause @msg when you are ready
* for it to continue.
**/
signals[AUTHENTICATE] =
g_signal_new ("authenticate",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (SoupSessionClass, authenticate),
NULL, NULL,
soup_marshal_NONE__OBJECT_OBJECT_BOOLEAN,
G_TYPE_NONE, 3,
SOUP_TYPE_MESSAGE,
SOUP_TYPE_AUTH,
G_TYPE_BOOLEAN);
signals[CONNECTION_CREATED] =
g_signal_new ("connection-created",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST,
0,
NULL, NULL,
soup_marshal_NONE__OBJECT,
G_TYPE_NONE, 1,
/* SoupConnection is private, so we can't use
* SOUP_TYPE_CONNECTION here.
*/
G_TYPE_OBJECT);
signals[TUNNELING] =
g_signal_new ("tunneling",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST,
0,
NULL, NULL,
soup_marshal_NONE__OBJECT,
G_TYPE_NONE, 1,
/* SoupConnection is private, so we can't use
* SOUP_TYPE_CONNECTION here.
*/
G_TYPE_OBJECT);
/* properties */
/**
* SOUP_SESSION_PROXY_URI:
*
* Alias for the #SoupSession:proxy-uri property. (The HTTP
* proxy to use for this session.)
**/
g_object_class_install_property (
object_class, PROP_PROXY_URI,
g_param_spec_boxed (SOUP_SESSION_PROXY_URI,
"Proxy URI",
"The HTTP Proxy to use for this session",
SOUP_TYPE_URI,
G_PARAM_READWRITE));
/**
* SOUP_SESSION_MAX_CONNS:
*
* Alias for the #SoupSession:max-conns property. (The maximum
* number of connections that the session can open at once.)
**/
g_object_class_install_property (
object_class, PROP_MAX_CONNS,
g_param_spec_int (SOUP_SESSION_MAX_CONNS,
"Max Connection Count",
"The maximum number of connections that the session can open at once",
1,
G_MAXINT,
SOUP_SESSION_MAX_CONNS_DEFAULT,
G_PARAM_READWRITE));
/**
* SOUP_SESSION_MAX_CONNS_PER_HOST:
*
* Alias for the #SoupSession:max-conns-per-host property.
* (The maximum number of connections that the session can
* open at once to a given host.)
**/
g_object_class_install_property (
object_class, PROP_MAX_CONNS_PER_HOST,
g_param_spec_int (SOUP_SESSION_MAX_CONNS_PER_HOST,
"Max Per-Host Connection Count",
"The maximum number of connections that the session can open at once to a given host",
1,
G_MAXINT,
SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT,
G_PARAM_READWRITE));
/**
* SoupSession:idle-timeout:
*
* Connection lifetime when idle
*
* Since: 2.4.1
**/
/**
* SOUP_SESSION_IDLE_TIMEOUT:
*
* Alias for the #SoupSession:idle-timeout property. (The idle
* connection lifetime.)
*
* Since: 2.4.1
**/
g_object_class_install_property (
object_class, PROP_IDLE_TIMEOUT,
g_param_spec_uint (SOUP_SESSION_IDLE_TIMEOUT,
"Idle Timeout",
"Connection lifetime when idle",
0, G_MAXUINT, 0,
G_PARAM_READWRITE));
/**
* SOUP_SESSION_USE_NTLM:
*
* Alias for the #SoupSession:use-ntlm property. (Whether or
* not to use NTLM authentication.)
**/
g_object_class_install_property (
object_class, PROP_USE_NTLM,
g_param_spec_boolean (SOUP_SESSION_USE_NTLM,
"Use NTLM",
"Whether or not to use NTLM authentication",
FALSE,
G_PARAM_READWRITE));
/**
* SOUP_SESSION_SSL_CA_FILE:
*
* Alias for the #SoupSession:ssl-ca-file property. (File
* containing SSL CA certificates.)
**/
g_object_class_install_property (
object_class, PROP_SSL_CA_FILE,
g_param_spec_string (SOUP_SESSION_SSL_CA_FILE,
"SSL CA file",
"File containing SSL CA certificates",
NULL,
G_PARAM_READWRITE));
/**
* SOUP_SESSION_SSL_STRICT:
*
* Alias for the #SoupSession:ignore-ssl-cert-errors
* property. By default, when validating certificates against
* a CA file, Soup will consider invalid certificates as a
* connection error. Setting this property to %TRUE makes soup
* ignore the errors, and make the connection.
*
* Since: 2.30
**/
g_object_class_install_property (
object_class, PROP_SSL_STRICT,
g_param_spec_boolean (SOUP_SESSION_SSL_STRICT,
"Strictly validate SSL certificates",
"Whether certificate errors should be considered a connection error",
TRUE,
G_PARAM_READWRITE));
/**
* SOUP_SESSION_ASYNC_CONTEXT:
*
* Alias for the #SoupSession:async-context property. (The
* session's #GMainContext.)
**/
g_object_class_install_property (
object_class, PROP_ASYNC_CONTEXT,
g_param_spec_pointer (SOUP_SESSION_ASYNC_CONTEXT,
"Async GMainContext",
"The GMainContext to dispatch async I/O in",
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
/**
* SOUP_SESSION_TIMEOUT:
*
* Alias for the #SoupSession:timeout property. (The timeout
* in seconds for blocking socket I/O operations.)
**/
g_object_class_install_property (
object_class, PROP_TIMEOUT,
g_param_spec_uint (SOUP_SESSION_TIMEOUT,
"Timeout value",
"Value in seconds to timeout a blocking I/O",
0, G_MAXUINT, 0,
G_PARAM_READWRITE));
/**
* SoupSession:user-agent:
*
* If non-%NULL, the value to use for the "User-Agent" header
* on #SoupMessage<!-- -->s sent from this session.
*
* RFC 2616 says: "The User-Agent request-header field
* contains information about the user agent originating the
* request. This is for statistical purposes, the tracing of
* protocol violations, and automated recognition of user
* agents for the sake of tailoring responses to avoid
* particular user agent limitations. User agents SHOULD
* include this field with requests."
*
* The User-Agent header contains a list of one or more
* product tokens, separated by whitespace, with the most
* significant product token coming first. The tokens must be
* brief, ASCII, and mostly alphanumeric (although "-", "_",
* and "." are also allowed), and may optionally include a "/"
* followed by a version string. You may also put comments,
* enclosed in parentheses, between or after the tokens.
*
* If you set a %user_agent property that has trailing
* whitespace, #SoupSession will append its own product token
* (eg, "<literal>libsoup/2.3.2</literal>") to the end of the
* header for you.
**/
/**
* SOUP_SESSION_USER_AGENT:
*
* Alias for the #SoupSession:user-agent property, qv.
**/
g_object_class_install_property (
object_class, PROP_USER_AGENT,
g_param_spec_string (SOUP_SESSION_USER_AGENT,
"User-Agent string",
"User-Agent string",
NULL,
G_PARAM_READWRITE));
/**
* SoupSession:accept-language:
*
* If non-%NULL, the value to use for the "Accept-Language" header
* on #SoupMessage<!-- -->s sent from this session.
*
* Setting this will disable
* #SoupSession:accept-language-auto.
*
* Since: 2.30
**/
/**
* SOUP_SESSION_ACCEPT_LANGUAGE:
*
* Alias for the #SoupSession:accept-language property, qv.
**/
g_object_class_install_property (
object_class, PROP_ACCEPT_LANGUAGE,
g_param_spec_string (SOUP_SESSION_ACCEPT_LANGUAGE,
"Accept-Language string",
"Accept-Language string",
NULL,
G_PARAM_READWRITE));
/**
* SoupSession:accept-language-auto:
*
* If %TRUE, #SoupSession will automatically set the string
* for the "Accept-Language" header on every #SoupMessage
* sent, based on the return value of g_get_language_names().
*
* Setting this will override any previous value of
* #SoupSession:accept-language.
*
* Since: 2.30
**/
/**
* SOUP_SESSION_ACCEPT_LANGUAGE_AUTO:
*
* Alias for the #SoupSession:accept-language-auto property, qv.
**/
g_object_class_install_property (
object_class, PROP_ACCEPT_LANGUAGE_AUTO,
g_param_spec_boolean (SOUP_SESSION_ACCEPT_LANGUAGE_AUTO,
"Accept-Language automatic mode",
"Accept-Language automatic mode",
FALSE,
G_PARAM_READWRITE));
/**
* SoupSession:add-feature:
*
* Add a feature object to the session. (Shortcut for calling
* soup_session_add_feature().)
*
* Since: 2.24
**/
/**
* SOUP_SESSION_ADD_FEATURE:
*
* Alias for the #SoupSession:add-feature property. (Shortcut
* for calling soup_session_add_feature().
*
* Since: 2.24
**/
g_object_class_install_property (
object_class, PROP_ADD_FEATURE,
g_param_spec_object (SOUP_SESSION_ADD_FEATURE,
"Add Feature",
"Add a feature object to the session",
SOUP_TYPE_SESSION_FEATURE,
G_PARAM_READWRITE));
/**
* SoupSession:add-feature-by-type:
*
* Add a feature object of the given type to the session.
* (Shortcut for calling soup_session_add_feature_by_type().)
*
* Since: 2.24
**/
/**
* SOUP_SESSION_ADD_FEATURE_BY_TYPE:
*
* Alias for the #SoupSession:add-feature-by-type property.
* (Shortcut for calling soup_session_add_feature_by_type().
*
* Since: 2.24
**/
g_object_class_install_property (
object_class, PROP_ADD_FEATURE_BY_TYPE,
g_param_spec_gtype (SOUP_SESSION_ADD_FEATURE_BY_TYPE,
"Add Feature By Type",
"Add a feature object of the given type to the session",
SOUP_TYPE_SESSION_FEATURE,
G_PARAM_READWRITE));
/**
* SoupSession:remove-feature-by-type:
*
* Remove feature objects from the session. (Shortcut for
* calling soup_session_remove_feature_by_type().)
*
* Since: 2.24
**/
/**
* SOUP_SESSION_REMOVE_FEATURE_BY_TYPE:
*
* Alias for the #SoupSession:remove-feature-by-type
* property. (Shortcut for calling
* soup_session_remove_feature_by_type().
*
* Since: 2.24
**/
g_object_class_install_property (
object_class, PROP_REMOVE_FEATURE_BY_TYPE,
g_param_spec_gtype (SOUP_SESSION_REMOVE_FEATURE_BY_TYPE,
"Remove Feature By Type",
"Remove features of the given type from the session",
SOUP_TYPE_SESSION_FEATURE,
G_PARAM_READWRITE));
}
static gboolean
safe_str_equal (const char *a, const char *b)
{
if (!a && !b)
return TRUE;
if ((a && !b) || (b && !a))
return FALSE;
return strcmp (a, b) == 0;
}
/* Converts a language in POSIX format and to be RFC2616 compliant */
/* Based on code from epiphany-webkit (ephy_langs_append_languages()) */
static gchar *
posix_lang_to_rfc2616 (const gchar *language)
{
/* Don't include charset variants, etc */
if (strchr (language, '.') || strchr (language, '@'))
return NULL;
/* Ignore "C" locale, which g_get_language_names() always
* includes as a fallback.
*/
if (!strcmp (language, "C"))
return NULL;
return g_strdelimit (g_ascii_strdown (language, -1), "_", '-');
}
/* Converts @quality from 0-100 to 0.0-1.0 and appends to @str */
static gchar *
add_quality_value (const gchar *str, int quality)
{
g_return_val_if_fail (str != NULL, NULL);
if (quality > 0 && quality < 100) {
double qvalue = quality / 100.0;
return g_strdup_printf ("%s;q=%.2g", str, qvalue);
} else {
/* Just dup the string in this case */
return g_strdup (str);
}
}
/* Returns a RFC2616 compliant languages list from system locales */
static gchar *
accept_languages_from_system (void)
{
const char * const * lang_names;
GPtrArray *langs = NULL;
char *lang, **langs_array, *langs_str;
int delta;
int i;
lang_names = g_get_language_names ();
g_return_val_if_fail (lang_names != NULL, NULL);
/* Build the array of languages */
langs = g_ptr_array_new ();
for (i = 0; lang_names[i] != NULL; i++) {
lang = posix_lang_to_rfc2616 (lang_names[i]);
if (lang)
g_ptr_array_add (langs, lang);
}
/* Add quality values */
if (langs->len < 10)
delta = 10;
else if (langs->len < 20)
delta = 5;
else
delta = 1;
for (i = 0; i < langs->len; i++) {
lang = langs->pdata[i];
langs->pdata[i] = add_quality_value (lang, 100 - i * delta);
g_free (lang);
}
/* Fallback: add "en" if list is empty */
if (langs->len == 0)
g_ptr_array_add (langs, g_strdup ("en"));
g_ptr_array_add (langs, NULL);
langs_array = (char **)langs->pdata;
langs_str = g_strjoinv (", ", langs_array);
g_strfreev (langs_array);
g_ptr_array_free (langs, FALSE);
return langs_str;
}
static void
set_property (GObject *object, guint prop_id,
const GValue *value, GParamSpec *pspec)
{
SoupSession *session = SOUP_SESSION (object);
SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
SoupURI *uri;
gboolean ca_file_changed = FALSE;
const char *new_ca_file, *user_agent;
SoupSessionFeature *feature;
switch (prop_id) {
case PROP_PROXY_URI:
uri = g_value_get_boxed (value);
if (uri) {
soup_session_remove_feature_by_type (session, SOUP_TYPE_PROXY_RESOLVER);
feature = SOUP_SESSION_FEATURE (soup_proxy_resolver_static_new (uri));
soup_session_add_feature (session, feature);
g_object_unref (feature);
} else
soup_session_remove_feature_by_type (session, SOUP_TYPE_PROXY_RESOLVER_STATIC);
soup_session_abort (session);
break;
case PROP_MAX_CONNS:
priv->max_conns = g_value_get_int (value);
break;
case PROP_MAX_CONNS_PER_HOST:
priv->max_conns_per_host = g_value_get_int (value);
break;
case PROP_USE_NTLM:
feature = soup_session_get_feature (session, SOUP_TYPE_AUTH_MANAGER_NTLM);
if (feature) {
g_object_set_property (G_OBJECT (feature),
SOUP_AUTH_MANAGER_NTLM_USE_NTLM,
value);
} else
g_warning ("Trying to set use-ntlm on session with no auth-manager");
break;
case PROP_SSL_CA_FILE:
new_ca_file = g_value_get_string (value);
if (!safe_str_equal (priv->ssl_ca_file, new_ca_file))
ca_file_changed = TRUE;
g_free (priv->ssl_ca_file);
priv->ssl_ca_file = g_strdup (new_ca_file);
if (ca_file_changed && priv->ssl_creds) {
soup_ssl_free_client_credentials (priv->ssl_creds);
priv->ssl_creds = NULL;
}
break;
case PROP_SSL_STRICT:
priv->ssl_strict = g_value_get_boolean (value);
break;
case PROP_ASYNC_CONTEXT:
priv->async_context = g_value_get_pointer (value);
if (priv->async_context)
g_main_context_ref (priv->async_context);
break;
case PROP_TIMEOUT:
priv->io_timeout = g_value_get_uint (value);
break;
case PROP_USER_AGENT:
g_free (priv->user_agent);
user_agent = g_value_get_string (value);
if (!user_agent)
priv->user_agent = NULL;
else if (!*user_agent) {
priv->user_agent =
g_strdup (SOUP_SESSION_USER_AGENT_BASE);
} else if (g_str_has_suffix (user_agent, " ")) {
priv->user_agent =
g_strdup_printf ("%s%s", user_agent,
SOUP_SESSION_USER_AGENT_BASE);
} else
priv->user_agent = g_strdup (user_agent);
break;
case PROP_ACCEPT_LANGUAGE:
g_free (priv->accept_language);
priv->accept_language = g_strdup (g_value_get_string (value));
priv->accept_language_auto = FALSE;
break;
case PROP_ACCEPT_LANGUAGE_AUTO:
priv->accept_language_auto = g_value_get_boolean (value);
if (priv->accept_language) {
g_free (priv->accept_language);
priv->accept_language = NULL;
}
/* Get languages from system if needed */
if (priv->accept_language_auto)
priv->accept_language = accept_languages_from_system ();
break;
case PROP_IDLE_TIMEOUT:
priv->idle_timeout = g_value_get_uint (value);
break;
case PROP_ADD_FEATURE:
soup_session_add_feature (session, g_value_get_object (value));
break;
case PROP_ADD_FEATURE_BY_TYPE:
soup_session_add_feature_by_type (session, g_value_get_gtype (value));
break;
case PROP_REMOVE_FEATURE_BY_TYPE:
soup_session_remove_feature_by_type (session, g_value_get_gtype (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
get_property (GObject *object, guint prop_id,
GValue *value, GParamSpec *pspec)
{
SoupSession *session = SOUP_SESSION (object);
SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
SoupSessionFeature *feature;
switch (prop_id) {
case PROP_PROXY_URI:
feature = soup_session_get_feature (session, SOUP_TYPE_PROXY_RESOLVER_STATIC);
if (feature) {
g_object_get_property (G_OBJECT (feature),
SOUP_PROXY_RESOLVER_STATIC_PROXY_URI,
value);
} else
g_value_set_boxed (value, NULL);
break;
case PROP_MAX_CONNS:
g_value_set_int (value, priv->max_conns);
break;
case PROP_MAX_CONNS_PER_HOST:
g_value_set_int (value, priv->max_conns_per_host);
break;
case PROP_USE_NTLM:
feature = soup_session_get_feature (session, SOUP_TYPE_AUTH_MANAGER_NTLM);
if (feature) {
g_object_get_property (G_OBJECT (feature),
SOUP_AUTH_MANAGER_NTLM_USE_NTLM,
value);
} else
g_value_set_boolean (value, FALSE);
break;
case PROP_SSL_CA_FILE:
g_value_set_string (value, priv->ssl_ca_file);
break;
case PROP_SSL_STRICT:
g_value_set_boolean (value, priv->ssl_strict);
break;
case PROP_ASYNC_CONTEXT:
g_value_set_pointer (value, priv->async_context ? g_main_context_ref (priv->async_context) : NULL);
break;
case PROP_TIMEOUT:
g_value_set_uint (value, priv->io_timeout);
break;
case PROP_USER_AGENT:
g_value_set_string (value, priv->user_agent);
break;
case PROP_ACCEPT_LANGUAGE:
g_value_set_string (value, priv->accept_language);
break;
case PROP_ACCEPT_LANGUAGE_AUTO:
g_value_set_boolean (value, priv->accept_language_auto);
break;
case PROP_IDLE_TIMEOUT:
g_value_set_uint (value, priv->idle_timeout);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
/**
* soup_session_get_async_context:
* @session: a #SoupSession
*
* Gets @session's async_context. This does not add a ref to the
* context, so you will need to ref it yourself if you want it to
* outlive its session.
*
* Return value: @session's #GMainContext, which may be %NULL
**/
GMainContext *
soup_session_get_async_context (SoupSession *session)
{
SoupSessionPrivate *priv;
g_return_val_if_fail (SOUP_IS_SESSION (session), NULL);
priv = SOUP_SESSION_GET_PRIVATE (session);
return priv->async_context;
}
/* Hosts */
static SoupSessionHost *
soup_session_host_new (SoupSession *session, SoupURI *uri)
{
SoupSessionHost *host;
host = g_slice_new0 (SoupSessionHost);
host->uri = soup_uri_copy_host (uri);
host->addr = soup_address_new (host->uri->host, host->uri->port);
return host;
}
/* Requires host_lock to be locked */
static SoupSessionHost *
get_host_for_uri (SoupSession *session, SoupURI *uri)
{
SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
SoupSessionHost *host;
host = g_hash_table_lookup (priv->hosts, uri);
if (host)
return host;
host = soup_session_host_new (session, uri);
g_hash_table_insert (priv->hosts, host->uri, host);
return host;
}
/* Note: get_host_for_message doesn't lock the host_lock. The caller
* must do it itself if there's a chance the host doesn't already
* exist.
*/
static SoupSessionHost *
get_host_for_message (SoupSession *session, SoupMessage *msg)
{
return get_host_for_uri (session, soup_message_get_uri (msg));
}
static void
free_host (SoupSessionHost *host)
{
while (host->connections) {
SoupConnection *conn = host->connections->data;
host->connections = g_slist_remove (host->connections, conn);
soup_connection_disconnect (conn);
}
soup_uri_free (host->uri);
g_object_unref (host->addr);
g_slice_free (SoupSessionHost, host);
}
static void
auth_required (SoupSession *session, SoupMessage *msg,
SoupAuth *auth, gboolean retrying)
{
g_signal_emit (session, signals[AUTHENTICATE], 0, msg, auth, retrying);
}
static void
auth_manager_authenticate (SoupAuthManager *manager, SoupMessage *msg,
SoupAuth *auth, gboolean retrying,
gpointer session)
{
SOUP_SESSION_GET_CLASS (session)->auth_required (
session, msg, auth, retrying);
}
#define SOUP_METHOD_IS_SAFE(method) (method == SOUP_METHOD_GET || \
method == SOUP_METHOD_HEAD || \
method == SOUP_METHOD_OPTIONS || \
method == SOUP_METHOD_PROPFIND)
static void
redirect_handler (SoupMessage *msg, gpointer user_data)
{
SoupSession *session = user_data;
const char *new_loc;
SoupURI *new_uri;
new_loc = soup_message_headers_get_one (msg->response_headers,
"Location");
g_return_if_fail (new_loc != NULL);
if (msg->status_code == SOUP_STATUS_SEE_OTHER ||
(msg->status_code == SOUP_STATUS_FOUND &&
!SOUP_METHOD_IS_SAFE (msg->method)) ||
(msg->status_code == SOUP_STATUS_MOVED_PERMANENTLY &&
msg->method == SOUP_METHOD_POST)) {
if (msg->method != SOUP_METHOD_HEAD) {
/* Redirect using a GET */
g_object_set (msg,
SOUP_MESSAGE_METHOD, SOUP_METHOD_GET,
NULL);
}
soup_message_set_request (msg, NULL,
SOUP_MEMORY_STATIC, NULL, 0);
soup_message_headers_set_encoding (msg->request_headers,
SOUP_ENCODING_NONE);
} else if (msg->status_code == SOUP_STATUS_MOVED_PERMANENTLY ||
msg->status_code == SOUP_STATUS_TEMPORARY_REDIRECT ||
msg->status_code == SOUP_STATUS_FOUND) {
/* Don't redirect non-safe methods */
if (!SOUP_METHOD_IS_SAFE (msg->method))
return;
} else {
/* Three possibilities:
*
* 1) This was a non-3xx response that happened to
* have a "Location" header
* 2) It's a non-redirecty 3xx response (300, 304,
* 305, 306)
* 3) It's some newly-defined 3xx response (308+)
*
* We ignore all of these cases. In the first two,
* redirecting would be explicitly wrong, and in the
* last case, we have no clue if the 3xx response is
* supposed to be redirecty or non-redirecty. Plus,
* 2616 says unrecognized status codes should be
* treated as the equivalent to the x00 code, and we
* don't redirect on 300, so therefore we shouldn't
* redirect on 308+ either.
*/
return;
}
/* Location is supposed to be an absolute URI, but some sites
* are lame, so we use soup_uri_new_with_base().
*/
new_uri = soup_uri_new_with_base (soup_message_get_uri (msg), new_loc);
if (!new_uri || !new_uri->host) {
if (new_uri)
soup_uri_free (new_uri);
soup_message_set_status_full (msg,
SOUP_STATUS_MALFORMED,
"Invalid Redirect URL");
return;
}
soup_message_set_uri (msg, new_uri);
soup_uri_free (new_uri);
soup_session_requeue_message (session, msg);
}
void
soup_session_send_queue_item (SoupSession *session,
SoupMessageQueueItem *item,
SoupConnection *conn)
{
SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
if (item->conn) {
if (item->conn != conn) {
g_object_unref (item->conn);
item->conn = g_object_ref (conn);
}
} else
item->conn = g_object_ref (conn);
if (priv->user_agent) {
soup_message_headers_replace (item->msg->request_headers,
"User-Agent", priv->user_agent);
}
if (priv->accept_language &&
!soup_message_headers_get_list (item->msg->request_headers,
"Accept-Language")) {
soup_message_headers_append (item->msg->request_headers,
"Accept-Language",
priv->accept_language);
}
g_signal_emit (session, signals[REQUEST_STARTED], 0,
item->msg, soup_connection_get_socket (conn));
soup_connection_send_request (conn, item->msg);
}
gboolean
soup_session_cleanup_connections (SoupSession *session,
gboolean prune_idle)
{
SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
GSList *conns = NULL, *c;
GHashTableIter iter;
gpointer conn, host;
SoupConnectionState state;
g_mutex_lock (priv->host_lock);
g_hash_table_iter_init (&iter, priv->conns);
while (g_hash_table_iter_next (&iter, &conn, &host)) {
state = soup_connection_get_state (conn);
if (state == SOUP_CONNECTION_REMOTE_DISCONNECTED ||
(prune_idle && state == SOUP_CONNECTION_IDLE))
conns = g_slist_prepend (conns, g_object_ref (conn));
}
g_mutex_unlock (priv->host_lock);
if (!conns)
return FALSE;
for (c = conns; c; c = c->next) {
conn = c->data;
soup_connection_disconnect (conn);
g_object_unref (conn);
}
g_slist_free (conns);
return TRUE;
}
static void
connection_disconnected (SoupConnection *conn, gpointer user_data)
{
SoupSession *session = user_data;
SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
SoupSessionHost *host;
g_mutex_lock (priv->host_lock);
host = g_hash_table_lookup (priv->conns, conn);
if (host) {
g_hash_table_remove (priv->conns, conn);
host->connections = g_slist_remove (host->connections, conn);
host->num_conns--;
}
g_signal_handlers_disconnect_by_func (conn, connection_disconnected, session);
priv->num_conns--;
g_mutex_unlock (priv->host_lock);
g_object_unref (conn);
}
void
soup_session_connection_failed (SoupSession *session,
SoupConnection *conn, guint status)
{
SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
SoupSessionHost *host;
SoupMessageQueueItem *item;
SoupMessage *msg;
g_mutex_lock (priv->host_lock);
host = g_hash_table_lookup (priv->conns, conn);
g_mutex_unlock (priv->host_lock);
if (host)
connection_disconnected (conn, session);
else {
host = g_object_get_data (G_OBJECT (conn), "SoupSessionHost");
if (!host)
return;
}
if (status == SOUP_STATUS_TRY_AGAIN)
return;
/* Cancel any other messages waiting for a connection to it,
* since they're out of luck.
*/
g_object_ref (session);
for (item = soup_message_queue_first (priv->queue); item; item = soup_message_queue_next (priv->queue, item)) {
msg = item->msg;
if (SOUP_MESSAGE_IS_STARTING (msg) &&
get_host_for_message (session, msg) == host)
soup_session_cancel_message (session, msg, status);
}
g_object_unref (session);
}
static void
tunnel_connected (SoupMessage *msg, gpointer user_data)
{
SoupSession *session = user_data;
if (SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) {
SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
SoupMessageQueueItem *item =
soup_message_queue_lookup (priv->queue, msg);
/* Clear the connection's proxy_uri, since it is now
* (effectively) directly connected.
*/
g_object_set (item->conn,
SOUP_CONNECTION_PROXY_URI, NULL,
NULL);
soup_message_queue_item_unref (item);
}
}
static void
tunnel_connect_restarted (SoupMessage *msg, gpointer session)
{
SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
SoupMessageQueueItem *item;
if (msg->status_code != SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED)
return;
item = soup_message_queue_lookup (priv->queue, msg);
if (!item)
return;
if (soup_connection_get_state (item->conn) == SOUP_CONNECTION_DISCONNECTED) {
/* We got a 407, and the session provided auth and
* restarted the message, but the proxy closed the
* connection, so we need to create a new one. The
* easiest way to do this is to just give up on the
* current msg and conn, and re-run the queue.
*/
soup_session_cancel_message (session, msg,
SOUP_STATUS_TRY_AGAIN);
}
soup_message_queue_item_unref (item);
}
SoupMessageQueueItem *
soup_session_make_connect_message (SoupSession *session,
SoupAddress *server_addr)
{
SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
SoupURI *uri;
SoupMessage *msg;
SoupMessageQueueItem *item;
uri = soup_uri_new (NULL);
soup_uri_set_scheme (uri, SOUP_URI_SCHEME_HTTPS);
soup_uri_set_host (uri, soup_address_get_name (server_addr));
soup_uri_set_port (uri, soup_address_get_port (server_addr));
soup_uri_set_path (uri, "");
msg = soup_message_new_from_uri (SOUP_METHOD_CONNECT, uri);
soup_message_set_flags (msg, SOUP_MESSAGE_NO_REDIRECT);
soup_uri_free (uri);
/* Call the base implementation of soup_session_queue_message
* directly, to add msg to the SoupMessageQueue and cause all
* the right signals to be emitted. We can't use
* queue_message's callback arg in this case because that's
* actually implemented by the subclasses.
*/
g_signal_connect (msg, "finished",
G_CALLBACK (tunnel_connected), session);
g_signal_connect (msg, "restarted",
G_CALLBACK (tunnel_connect_restarted), session);
queue_message (session, msg, NULL, NULL);
item = soup_message_queue_lookup (priv->queue, msg);
g_object_unref (msg);
return item;
}
/**
* soup_session_get_connection:
* @session: a #SoupSession
* @item: a #SoupMessageQueueItem
* @try_pruning: on return, whether or not to try pruning a connection
*
* Tries to find or create a connection for @item; this is an internal
* method for #SoupSession subclasses.
*
* If there is an idle connection to the relevant host available, then
* that connection will be returned. The connection will be set to
* %SOUP_CONNECTION_IN_USE, so the caller must call
* soup_connection_set_state() to set it to %SOUP_CONNECTION_IDLE if
* it ends up not using the connection right away.
*
* If there is no idle connection available, but it is possible to
* create a new connection, then one will be created and returned
* (with state %SOUP_CONNECTION_NEW). The caller MUST then call
* soup_connection_connect_sync() or soup_connection_connect_async()
* to connect it. If the connection attempt fails, the caller must
* call soup_session_connection_failed() to tell the session to free
* the connection.
*
* If no connection is available and a new connection cannot be made,
* soup_session_get_connection() will return %NULL. If @session has
* the maximum number of open connections open, but does not have the
* maximum number of per-host connections open to the relevant host,
* then *@try_pruning will be set to %TRUE. In this case, the caller
* can call soup_session_try_prune_connection() to close an idle
* connection, and then try soup_session_get_connection() again. (If
* calling soup_session_try_prune_connection() wouldn't help, then
* *@try_pruning is left untouched; it is NOT set to %FALSE.)
*
* Return value: a #SoupConnection, or %NULL
**/
SoupConnection *
soup_session_get_connection (SoupSession *session,
SoupMessageQueueItem *item,
gboolean *try_pruning)
{
SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
SoupConnection *conn;
SoupSessionHost *host;
SoupAddress *remote_addr, *tunnel_addr;
SoupSSLCredentials *ssl_creds;
GSList *conns;
int num_pending = 0;
SoupURI *uri;
g_mutex_lock (priv->host_lock);
host = get_host_for_message (session, item->msg);
for (conns = host->connections; conns; conns = conns->next) {
if (soup_connection_get_state (conns->data) == SOUP_CONNECTION_IDLE) {
soup_connection_set_state (conns->data, SOUP_CONNECTION_IN_USE);
g_mutex_unlock (priv->host_lock);
return conns->data;
} else if (soup_connection_get_state (conns->data) == SOUP_CONNECTION_CONNECTING)
num_pending++;
}
/* Limit the number of pending connections; num_messages / 2
* is somewhat arbitrary...
*/
if (num_pending > host->num_messages / 2) {
g_mutex_unlock (priv->host_lock);
return NULL;
}
if (host->num_conns >= priv->max_conns_per_host) {
g_mutex_unlock (priv->host_lock);
return NULL;
}
if (priv->num_conns >= priv->max_conns) {
*try_pruning = TRUE;
g_mutex_unlock (priv->host_lock);
return NULL;
}
if (item->proxy_addr) {
remote_addr = item->proxy_addr;
tunnel_addr = NULL;
} else {
remote_addr = host->addr;
tunnel_addr = NULL;
}
uri = soup_message_get_uri (item->msg);
if (uri->scheme == SOUP_URI_SCHEME_HTTPS) {
if (!priv->ssl_creds)
priv->ssl_creds = soup_ssl_get_client_credentials (priv->ssl_ca_file);
ssl_creds = priv->ssl_creds;
if (item->proxy_addr)
tunnel_addr = host->addr;
} else
ssl_creds = NULL;
conn = soup_connection_new (
SOUP_CONNECTION_REMOTE_ADDRESS, remote_addr,
SOUP_CONNECTION_TUNNEL_ADDRESS, tunnel_addr,
SOUP_CONNECTION_PROXY_URI, item->proxy_uri,
SOUP_CONNECTION_SSL_CREDENTIALS, ssl_creds,
SOUP_CONNECTION_SSL_STRICT, priv->ssl_strict,
SOUP_CONNECTION_ASYNC_CONTEXT, priv->async_context,
SOUP_CONNECTION_TIMEOUT, priv->io_timeout,
SOUP_CONNECTION_IDLE_TIMEOUT, priv->idle_timeout,
NULL);
g_object_set_data (G_OBJECT (conn), "SoupSessionHost", host);
g_signal_connect (conn, "disconnected",
G_CALLBACK (connection_disconnected),
session);
g_signal_emit (session, signals[CONNECTION_CREATED], 0, conn);
g_hash_table_insert (priv->conns, conn, host);
priv->num_conns++;
host->num_conns++;
host->connections = g_slist_prepend (host->connections, conn);
g_mutex_unlock (priv->host_lock);
return conn;
}
SoupMessageQueue *
soup_session_get_queue (SoupSession *session)
{
SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
return priv->queue;
}
static void
message_finished (SoupMessage *msg, gpointer user_data)
{
SoupMessageQueueItem *item = user_data;
SoupSession *session = item->session;
SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
SoupSessionHost *host;
if (item->conn) {
g_object_unref (item->conn);
item->conn = NULL;
}
if (!SOUP_MESSAGE_IS_STARTING (msg)) {
soup_message_queue_remove (priv->queue, item);
g_mutex_lock (priv->host_lock);
host = get_host_for_message (session, item->msg);
host->num_messages--;
g_mutex_unlock (priv->host_lock);
g_signal_handlers_disconnect_by_func (msg, message_finished, item);
/* g_signal_handlers_disconnect_by_func doesn't work if you
* have a metamarshal, meaning it doesn't work with
* soup_message_add_header_handler()
*/
g_signal_handlers_disconnect_matched (msg, G_SIGNAL_MATCH_DATA,
0, 0, NULL, NULL, session);
g_signal_emit (session, signals[REQUEST_UNQUEUED], 0, msg);
soup_message_queue_item_unref (item);
}
}
static void
queue_message (SoupSession *session, SoupMessage *msg,
SoupSessionCallback callback, gpointer user_data)
{
SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
SoupMessageQueueItem *item;
SoupSessionHost *host;
item = soup_message_queue_append (priv->queue, msg, callback, user_data);
g_mutex_lock (priv->host_lock);
host = get_host_for_message (session, item->msg);
host->num_messages++;
g_mutex_unlock (priv->host_lock);
soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
g_signal_connect_after (msg, "finished",
G_CALLBACK (message_finished), item);
if (!(soup_message_get_flags (msg) & SOUP_MESSAGE_NO_REDIRECT)) {
soup_message_add_header_handler (
msg, "got_body", "Location",
G_CALLBACK (redirect_handler), session);
}
g_signal_emit (session, signals[REQUEST_QUEUED], 0, msg);
}
/**
* SoupSessionCallback:
* @session: the session
* @msg: the message that has finished
* @user_data: the data passed to soup_session_queue_message
*
* Prototype for the callback passed to soup_session_queue_message(),
* qv.
**/
/**
* soup_session_queue_message:
* @session: a #SoupSession
* @msg: (transfer full): the message to queue
* @callback: (allow-none) (scope async): a #SoupSessionCallback which will
* be called after the message completes or when an unrecoverable error occurs.
* @user_data: (allow-none): a pointer passed to @callback.
*
* Queues the message @msg for sending. All messages are processed
* while the glib main loop runs. If @msg has been processed before,
* any resources related to the time it was last sent are freed.
*
* Upon message completion, the callback specified in @callback will
* be invoked (in the thread associated with @session's async
* context). If after returning from this callback the message has not
* been requeued, @msg will be unreffed.
*/
void
soup_session_queue_message (SoupSession *session, SoupMessage *msg,
SoupSessionCallback callback, gpointer user_data)
{
g_return_if_fail (SOUP_IS_SESSION (session));
g_return_if_fail (SOUP_IS_MESSAGE (msg));
SOUP_SESSION_GET_CLASS (session)->queue_message (session, msg,
callback, user_data);
}
static void
requeue_message (SoupSession *session, SoupMessage *msg)
{
soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
}
/**
* soup_session_requeue_message:
* @session: a #SoupSession
* @msg: the message to requeue
*
* This causes @msg to be placed back on the queue to be attempted
* again.
**/
void
soup_session_requeue_message (SoupSession *session, SoupMessage *msg)
{
g_return_if_fail (SOUP_IS_SESSION (session));
g_return_if_fail (SOUP_IS_MESSAGE (msg));
SOUP_SESSION_GET_CLASS (session)->requeue_message (session, msg);
}
/**
* soup_session_send_message:
* @session: a #SoupSession
* @msg: the message to send
*
* Synchronously send @msg. This call will not return until the
* transfer is finished successfully or there is an unrecoverable
* error.
*
* @msg is not freed upon return.
*
* Return value: the HTTP status code of the response
*/
guint
soup_session_send_message (SoupSession *session, SoupMessage *msg)
{
g_return_val_if_fail (SOUP_IS_SESSION (session), SOUP_STATUS_MALFORMED);
g_return_val_if_fail (SOUP_IS_MESSAGE (msg), SOUP_STATUS_MALFORMED);
return SOUP_SESSION_GET_CLASS (session)->send_message (session, msg);
}
/**
* soup_session_pause_message:
* @session: a #SoupSession
* @msg: a #SoupMessage currently running on @session
*
* Pauses HTTP I/O on @msg. Call soup_session_unpause_message() to
* resume I/O.
**/
void
soup_session_pause_message (SoupSession *session,
SoupMessage *msg)
{
g_return_if_fail (SOUP_IS_SESSION (session));
g_return_if_fail (SOUP_IS_MESSAGE (msg));
soup_message_io_pause (msg);
}
/**
* soup_session_unpause_message:
* @session: a #SoupSession
* @msg: a #SoupMessage currently running on @session
*
* Resumes HTTP I/O on @msg. Use this to resume after calling
* soup_session_pause_message().
*
* If @msg is being sent via blocking I/O, this will resume reading or
* writing immediately. If @msg is using non-blocking I/O, then
* reading or writing won't resume until you return to the main loop.
**/
void
soup_session_unpause_message (SoupSession *session,
SoupMessage *msg)
{
g_return_if_fail (SOUP_IS_SESSION (session));
g_return_if_fail (SOUP_IS_MESSAGE (msg));
soup_message_io_unpause (msg);
}
static void
cancel_message (SoupSession *session, SoupMessage *msg, guint status_code)
{
SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
SoupMessageQueueItem *item;
item = soup_message_queue_lookup (priv->queue, msg);
if (item) {
if (item->cancellable)
g_cancellable_cancel (item->cancellable);
soup_message_queue_item_unref (item);
}
soup_message_io_stop (msg);
soup_message_set_status (msg, status_code);
soup_message_finished (msg);
}
/**
* soup_session_cancel_message:
* @session: a #SoupSession
* @msg: the message to cancel
* @status_code: status code to set on @msg (generally
* %SOUP_STATUS_CANCELLED)
*
* Causes @session to immediately finish processing @msg (regardless
* of its current state) with a final status_code of @status_code. You
* may call this at any time after handing @msg off to @session; if
* @session has started sending the request but has not yet received
* the complete response, then it will close the request's connection.
* Note that with non-idempotent requests (eg, %POST, %PUT, %DELETE)
* it is possible that you might cancel the request after the server
* acts on it, but before it returns a response, leaving the remote
* resource in an unknown state.
*
* If the message is cancelled while its response body is being read,
* then the response body in @msg will be left partially-filled-in.
* The response headers, on the other hand, will always be either
* empty or complete.
*
* For messages queued with soup_session_queue_message() (and
* cancelled from the same thread), the callback will be invoked
* before soup_session_cancel_message() returns.
**/
void
soup_session_cancel_message (SoupSession *session, SoupMessage *msg,
guint status_code)
{
g_return_if_fail (SOUP_IS_SESSION (session));
g_return_if_fail (SOUP_IS_MESSAGE (msg));
/* If the message is already ending, don't do anything */
if (soup_message_get_io_status (msg) == SOUP_MESSAGE_IO_STATUS_FINISHED)
return;
SOUP_SESSION_GET_CLASS (session)->cancel_message (session, msg, status_code);
}
static void
gather_conns (gpointer key, gpointer host, gpointer data)
{
SoupConnection *conn = key;
GSList **conns = data;
*conns = g_slist_prepend (*conns, g_object_ref (conn));
}
/**
* soup_session_abort:
* @session: the session
*
* Cancels all pending requests in @session.
**/
void
soup_session_abort (SoupSession *session)
{
SoupSessionPrivate *priv;
SoupMessageQueueItem *item;
GSList *conns, *c;
g_return_if_fail (SOUP_IS_SESSION (session));
priv = SOUP_SESSION_GET_PRIVATE (session);
for (item = soup_message_queue_first (priv->queue);
item;
item = soup_message_queue_next (priv->queue, item)) {
soup_session_cancel_message (session, item->msg,
SOUP_STATUS_CANCELLED);
}
/* Close all connections */
g_mutex_lock (priv->host_lock);
conns = NULL;
g_hash_table_foreach (priv->conns, gather_conns, &conns);
g_mutex_unlock (priv->host_lock);
for (c = conns; c; c = c->next) {
soup_connection_disconnect (c->data);
g_object_unref (c->data);
}
g_slist_free (conns);
}
/**
* soup_session_prepare_for_uri:
* @session: a #SoupSession
* @uri: a #SoupURI which may be required
*
* Tells @session that @uri may be requested shortly, and so the
* session can try to prepare (resolving the domain name, obtaining
* proxy address, etc.) in order to work more quickly once the URI is
* actually requested.
*
* This method acts asynchronously, in @session's %async_context.
* If you are using #SoupSessionSync and do not have a main loop running,
* then you can't use this method.
*
* Since: 2.30
**/
void
soup_session_prepare_for_uri (SoupSession *session, SoupURI *uri)
{
SoupSessionPrivate *priv;
SoupSessionHost *host;
SoupAddress *addr;
g_return_if_fail (SOUP_IS_SESSION (session));
g_return_if_fail (uri != NULL);
if (!uri->host)
return;
priv = SOUP_SESSION_GET_PRIVATE (session);
g_mutex_lock (priv->host_lock);
host = get_host_for_uri (session, uri);
addr = g_object_ref (host->addr);
g_mutex_unlock (priv->host_lock);
soup_address_resolve_async (addr, priv->async_context,
NULL, NULL, NULL);
}
/**
* soup_session_add_feature:
* @session: a #SoupSession
* @feature: an object that implements #SoupSessionFeature
*
* Adds @feature's functionality to @session. You can also add a
* feature to the session at construct time by using the
* %SOUP_SESSION_ADD_FEATURE property.
*
* Since: 2.24
**/
void
soup_session_add_feature (SoupSession *session, SoupSessionFeature *feature)
{
SoupSessionPrivate *priv;
g_return_if_fail (SOUP_IS_SESSION (session));
g_return_if_fail (SOUP_IS_SESSION_FEATURE (feature));
priv = SOUP_SESSION_GET_PRIVATE (session);
priv->features = g_slist_prepend (priv->features, g_object_ref (feature));
g_hash_table_remove_all (priv->features_cache);
soup_session_feature_attach (feature, session);
}
/**
* soup_session_add_feature_by_type:
* @session: a #SoupSession
* @feature_type: the #GType of a class that implements #SoupSessionFeature
*
* Creates a new feature of type @feature_type and adds it to
* @session. You can use this instead of soup_session_add_feature() in
* the case wher you don't need to customize the new feature in any
* way. You can also add a feature to the session at construct time by
* using the %SOUP_SESSION_ADD_FEATURE_BY_TYPE property.
*
* Since: 2.24
**/
void
soup_session_add_feature_by_type (SoupSession *session, GType feature_type)
{
SoupSessionFeature *feature;
g_return_if_fail (SOUP_IS_SESSION (session));
g_return_if_fail (g_type_is_a (feature_type, SOUP_TYPE_SESSION_FEATURE));
feature = g_object_new (feature_type, NULL);
soup_session_add_feature (session, feature);
g_object_unref (feature);
}
/**
* soup_session_remove_feature:
* @session: a #SoupSession
* @feature: a feature that has previously been added to @session
*
* Removes @feature's functionality from @session.
*
* Since: 2.24
**/
void
soup_session_remove_feature (SoupSession *session, SoupSessionFeature *feature)
{
SoupSessionPrivate *priv;
g_return_if_fail (SOUP_IS_SESSION (session));
priv = SOUP_SESSION_GET_PRIVATE (session);
if (g_slist_find (priv->features, feature)) {
priv->features = g_slist_remove (priv->features, feature);
g_hash_table_remove_all (priv->features_cache);
soup_session_feature_detach (feature, session);
g_object_unref (feature);
}
}
/**
* soup_session_remove_feature_by_type:
* @session: a #SoupSession
* @feature_type: the #GType of a class that implements #SoupSessionFeature
*
* Removes all features of type @feature_type (or any subclass of
* @feature_type) from @session. You can also remove standard features
* from the session at construct time by using the
* %SOUP_SESSION_REMOVE_FEATURE_BY_TYPE property.
*
* Since: 2.24
**/
void
soup_session_remove_feature_by_type (SoupSession *session, GType feature_type)
{
SoupSessionPrivate *priv;
GSList *f;
g_return_if_fail (SOUP_IS_SESSION (session));
priv = SOUP_SESSION_GET_PRIVATE (session);
restart:
for (f = priv->features; f; f = f->next) {
if (G_TYPE_CHECK_INSTANCE_TYPE (f->data, feature_type)) {
soup_session_remove_feature (session, f->data);
goto restart;
}
}
}
/**
* soup_session_get_features:
* @session: a #SoupSession
* @feature_type: the #GType of the class of features to get
*
* Generates a list of @session's features of type @feature_type. (If
* you want to see all features, you can pass %G_TYPE_SESSION_FEATURE
* for @feature_type.)
*
* Return value: a list of features. You must free the list, but not
* its contents
*
* Since: 2.26
**/
GSList *
soup_session_get_features (SoupSession *session, GType feature_type)
{
SoupSessionPrivate *priv;
GSList *f, *ret;
g_return_val_if_fail (SOUP_IS_SESSION (session), NULL);
priv = SOUP_SESSION_GET_PRIVATE (session);
for (f = priv->features, ret = NULL; f; f = f->next) {
if (G_TYPE_CHECK_INSTANCE_TYPE (f->data, feature_type))
ret = g_slist_prepend (ret, f->data);
}
return g_slist_reverse (ret);
}
/**
* soup_session_get_feature:
* @session: a #SoupSession
* @feature_type: the #GType of the feature to get
*
* Gets the first feature in @session of type @feature_type. For
* features where there may be more than one feature of a given type,
* use soup_session_get_features().
*
* Return value: a #SoupSessionFeature, or %NULL. The feature is owned
* by @session.
*
* Since: 2.26
**/
SoupSessionFeature *
soup_session_get_feature (SoupSession *session, GType feature_type)
{
SoupSessionPrivate *priv;
SoupSessionFeature *feature;
GSList *f;
g_return_val_if_fail (SOUP_IS_SESSION (session), NULL);
priv = SOUP_SESSION_GET_PRIVATE (session);
feature = g_hash_table_lookup (priv->features_cache,
GSIZE_TO_POINTER (feature_type));
if (feature)
return feature;
for (f = priv->features; f; f = f->next) {
feature = f->data;
if (G_TYPE_CHECK_INSTANCE_TYPE (feature, feature_type)) {
g_hash_table_insert (priv->features_cache,
GSIZE_TO_POINTER (feature_type),
feature);
return feature;
}
}
return NULL;
}
/**
* soup_session_get_feature_for_message:
* @session: a #SoupSession
* @feature_type: the #GType of the feature to get
* @msg: a #SoupMessage
*
* Gets the first feature in @session of type @feature_type, provided
* that it is not disabled for @msg. As with
* soup_session_get_feature(), this should only be used for features
* where @feature_type is only expected to match a single feature. In
* particular, if there are two matching features, and the first is
* disabled on @msg, and the second is not, then this will return
* %NULL, not the second feature.
*
* Return value: a #SoupSessionFeature, or %NULL. The feature is owned
* by @session.
*
* Since: 2.28
**/
SoupSessionFeature *
soup_session_get_feature_for_message (SoupSession *session, GType feature_type,
SoupMessage *msg)
{
SoupSessionFeature *feature;
feature = soup_session_get_feature (session, feature_type);
if (feature && soup_message_disables_feature (msg, feature))
return NULL;
return feature;
}
|