url.c 119 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifdef HAVE_NETINET_IN_H
  26. #include <netinet/in.h>
  27. #endif
  28. #ifdef HAVE_NETDB_H
  29. #include <netdb.h>
  30. #endif
  31. #ifdef HAVE_ARPA_INET_H
  32. #include <arpa/inet.h>
  33. #endif
  34. #ifdef HAVE_NET_IF_H
  35. #include <net/if.h>
  36. #endif
  37. #ifdef HAVE_IPHLPAPI_H
  38. #include <Iphlpapi.h>
  39. #endif
  40. #ifdef HAVE_SYS_IOCTL_H
  41. #include <sys/ioctl.h>
  42. #endif
  43. #ifdef HAVE_SYS_PARAM_H
  44. #include <sys/param.h>
  45. #endif
  46. #ifdef __VMS
  47. #include <in.h>
  48. #include <inet.h>
  49. #endif
  50. #ifdef HAVE_SYS_UN_H
  51. #include <sys/un.h>
  52. #endif
  53. #ifndef HAVE_SOCKET
  54. #error "We cannot compile without socket() support!"
  55. #endif
  56. #if defined(HAVE_IF_NAMETOINDEX) && defined(_WIN32)
  57. #if defined(__MINGW32__) && (__MINGW64_VERSION_MAJOR <= 5)
  58. #include <wincrypt.h> /* workaround for old mingw-w64 missing to include it */
  59. #endif
  60. #include <iphlpapi.h>
  61. #endif
  62. #include <limits.h>
  63. #include "doh.h"
  64. #include "urldata.h"
  65. #include "netrc.h"
  66. #include "formdata.h"
  67. #include "mime.h"
  68. #include "vtls/vtls.h"
  69. #include "hostip.h"
  70. #include "transfer.h"
  71. #include "sendf.h"
  72. #include "progress.h"
  73. #include "cookie.h"
  74. #include "strcase.h"
  75. #include "escape.h"
  76. #include "share.h"
  77. #include "content_encoding.h"
  78. #include "http_digest.h"
  79. #include "http_negotiate.h"
  80. #include "select.h"
  81. #include "multiif.h"
  82. #include "easyif.h"
  83. #include "speedcheck.h"
  84. #include "curlx/warnless.h"
  85. #include "getinfo.h"
  86. #include "pop3.h"
  87. #include "urlapi-int.h"
  88. #include "system_win32.h"
  89. #include "hsts.h"
  90. #include "noproxy.h"
  91. #include "cfilters.h"
  92. #include "idn.h"
  93. /* And now for the protocols */
  94. #include "ftp.h"
  95. #include "dict.h"
  96. #include "telnet.h"
  97. #include "tftp.h"
  98. #include "http.h"
  99. #include "http2.h"
  100. #include "file.h"
  101. #include "curl_ldap.h"
  102. #include "vssh/ssh.h"
  103. #include "imap.h"
  104. #include "url.h"
  105. #include "connect.h"
  106. #include "http_ntlm.h"
  107. #include "curl_rtmp.h"
  108. #include "gopher.h"
  109. #include "mqtt.h"
  110. #include "http_proxy.h"
  111. #include "conncache.h"
  112. #include "multihandle.h"
  113. #include "strdup.h"
  114. #include "setopt.h"
  115. #include "altsvc.h"
  116. #include "curlx/dynbuf.h"
  117. #include "headers.h"
  118. #include "curlx/strerr.h"
  119. #include "curlx/strparse.h"
  120. /* The last 2 #include files should be in this order */
  121. #include "curl_memory.h"
  122. #include "memdebug.h"
  123. #ifdef USE_NGHTTP2
  124. static void data_priority_cleanup(struct Curl_easy *data);
  125. #else
  126. #define data_priority_cleanup(x)
  127. #endif
  128. /* Some parts of the code (e.g. chunked encoding) assume this buffer has at
  129. * more than just a few bytes to play with. Do not let it become too small or
  130. * bad things will happen.
  131. */
  132. #if READBUFFER_SIZE < READBUFFER_MIN
  133. # error READBUFFER_SIZE is too small
  134. #endif
  135. #ifdef USE_UNIX_SOCKETS
  136. #define UNIX_SOCKET_PREFIX "localhost"
  137. #endif
  138. /* Reject URLs exceeding this length */
  139. #define MAX_URL_LEN 0xffff
  140. /*
  141. * get_protocol_family()
  142. *
  143. * This is used to return the protocol family for a given protocol.
  144. *
  145. * Parameters:
  146. *
  147. * 'h' [in] - struct Curl_handler pointer.
  148. *
  149. * Returns the family as a single bit protocol identifier.
  150. */
  151. static curl_prot_t get_protocol_family(const struct Curl_handler *h)
  152. {
  153. DEBUGASSERT(h);
  154. DEBUGASSERT(h->family);
  155. return h->family;
  156. }
  157. void Curl_freeset(struct Curl_easy *data)
  158. {
  159. /* Free all dynamic strings stored in the data->set substructure. */
  160. enum dupstring i;
  161. enum dupblob j;
  162. for(i = (enum dupstring)0; i < STRING_LAST; i++) {
  163. Curl_safefree(data->set.str[i]);
  164. }
  165. for(j = (enum dupblob)0; j < BLOB_LAST; j++) {
  166. Curl_safefree(data->set.blobs[j]);
  167. }
  168. if(data->state.referer_alloc) {
  169. Curl_safefree(data->state.referer);
  170. data->state.referer_alloc = FALSE;
  171. }
  172. data->state.referer = NULL;
  173. if(data->state.url_alloc) {
  174. Curl_safefree(data->state.url);
  175. data->state.url_alloc = FALSE;
  176. }
  177. data->state.url = NULL;
  178. Curl_mime_cleanpart(&data->set.mimepost);
  179. #ifndef CURL_DISABLE_COOKIES
  180. curl_slist_free_all(data->state.cookielist);
  181. data->state.cookielist = NULL;
  182. #endif
  183. }
  184. /* free the URL pieces */
  185. static void up_free(struct Curl_easy *data)
  186. {
  187. struct urlpieces *up = &data->state.up;
  188. Curl_safefree(up->scheme);
  189. Curl_safefree(up->hostname);
  190. Curl_safefree(up->port);
  191. Curl_safefree(up->user);
  192. Curl_safefree(up->password);
  193. Curl_safefree(up->options);
  194. Curl_safefree(up->path);
  195. Curl_safefree(up->query);
  196. curl_url_cleanup(data->state.uh);
  197. data->state.uh = NULL;
  198. }
  199. /*
  200. * This is the internal function curl_easy_cleanup() calls. This should
  201. * cleanup and free all resources associated with this sessionhandle.
  202. *
  203. * We ignore SIGPIPE when this is called from curl_easy_cleanup.
  204. */
  205. CURLcode Curl_close(struct Curl_easy **datap)
  206. {
  207. struct Curl_easy *data;
  208. if(!datap || !*datap)
  209. return CURLE_OK;
  210. data = *datap;
  211. *datap = NULL;
  212. if(!data->state.internal && data->multi) {
  213. /* This handle is still part of a multi handle, take care of this first
  214. and detach this handle from there.
  215. This detaches the connection. */
  216. curl_multi_remove_handle(data->multi, data);
  217. }
  218. else {
  219. /* Detach connection if any is left. This should not be normal, but can be
  220. the case for example with CONNECT_ONLY + recv/send (test 556) */
  221. Curl_detach_connection(data);
  222. if(!data->state.internal && data->multi_easy) {
  223. /* when curl_easy_perform() is used, it creates its own multi handle to
  224. use and this is the one */
  225. curl_multi_cleanup(data->multi_easy);
  226. data->multi_easy = NULL;
  227. }
  228. }
  229. DEBUGASSERT(!data->conn || data->state.internal);
  230. Curl_expire_clear(data); /* shut off any timers left */
  231. if(data->state.rangestringalloc)
  232. free(data->state.range);
  233. /* release any resolve information this transfer kept */
  234. Curl_async_destroy(data);
  235. Curl_resolv_unlink(data, &data->state.dns[0]); /* done with this */
  236. Curl_resolv_unlink(data, &data->state.dns[1]);
  237. data->set.verbose = FALSE; /* no more calls to DEBUGFUNCTION */
  238. data->magic = 0; /* force a clear AFTER the possibly enforced removal from
  239. * the multi handle and async dns shutdown. The multi
  240. * handle might check the magic and so might any
  241. * DEBUGFUNCTION invoked for tracing */
  242. /* freed here just in case DONE was not called */
  243. Curl_req_free(&data->req, data);
  244. /* Close down all open SSL info and sessions */
  245. Curl_ssl_close_all(data);
  246. Curl_safefree(data->state.first_host);
  247. Curl_ssl_free_certinfo(data);
  248. if(data->state.referer_alloc) {
  249. Curl_safefree(data->state.referer);
  250. data->state.referer_alloc = FALSE;
  251. }
  252. data->state.referer = NULL;
  253. up_free(data);
  254. curlx_dyn_free(&data->state.headerb);
  255. Curl_flush_cookies(data, TRUE);
  256. #ifndef CURL_DISABLE_ALTSVC
  257. Curl_altsvc_save(data, data->asi, data->set.str[STRING_ALTSVC]);
  258. Curl_altsvc_cleanup(&data->asi);
  259. #endif
  260. #ifndef CURL_DISABLE_HSTS
  261. Curl_hsts_save(data, data->hsts, data->set.str[STRING_HSTS]);
  262. if(!data->share || !data->share->hsts)
  263. Curl_hsts_cleanup(&data->hsts);
  264. curl_slist_free_all(data->state.hstslist); /* clean up list */
  265. #endif
  266. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_DIGEST_AUTH)
  267. Curl_http_auth_cleanup_digest(data);
  268. #endif
  269. Curl_safefree(data->state.most_recent_ftp_entrypath);
  270. Curl_safefree(data->info.contenttype);
  271. Curl_safefree(data->info.wouldredirect);
  272. data_priority_cleanup(data);
  273. /* No longer a dirty share, if it exists */
  274. if(data->share) {
  275. Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
  276. data->share->dirty--;
  277. Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
  278. }
  279. Curl_hash_destroy(&data->meta_hash);
  280. #ifndef CURL_DISABLE_PROXY
  281. Curl_safefree(data->state.aptr.proxyuserpwd);
  282. #endif
  283. Curl_safefree(data->state.aptr.uagent);
  284. Curl_safefree(data->state.aptr.userpwd);
  285. Curl_safefree(data->state.aptr.accept_encoding);
  286. Curl_safefree(data->state.aptr.rangeline);
  287. Curl_safefree(data->state.aptr.ref);
  288. Curl_safefree(data->state.aptr.host);
  289. #ifndef CURL_DISABLE_COOKIES
  290. Curl_safefree(data->state.aptr.cookiehost);
  291. #endif
  292. #ifndef CURL_DISABLE_RTSP
  293. Curl_safefree(data->state.aptr.rtsp_transport);
  294. #endif
  295. Curl_safefree(data->state.aptr.user);
  296. Curl_safefree(data->state.aptr.passwd);
  297. #ifndef CURL_DISABLE_PROXY
  298. Curl_safefree(data->state.aptr.proxyuser);
  299. Curl_safefree(data->state.aptr.proxypasswd);
  300. #endif
  301. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_FORM_API)
  302. Curl_mime_cleanpart(data->state.formp);
  303. Curl_safefree(data->state.formp);
  304. #endif
  305. /* destruct wildcard structures if it is needed */
  306. Curl_wildcard_dtor(&data->wildcard);
  307. Curl_freeset(data);
  308. Curl_headers_cleanup(data);
  309. Curl_netrc_cleanup(&data->state.netrc);
  310. free(data);
  311. return CURLE_OK;
  312. }
  313. /*
  314. * Initialize the UserDefined fields within a Curl_easy.
  315. * This may be safely called on a new or existing Curl_easy.
  316. */
  317. void Curl_init_userdefined(struct Curl_easy *data)
  318. {
  319. struct UserDefined *set = &data->set;
  320. set->out = stdout; /* default output to stdout */
  321. set->in_set = stdin; /* default input from stdin */
  322. set->err = stderr; /* default stderr to stderr */
  323. #if defined(__clang__) && __clang_major__ >= 16
  324. #pragma clang diagnostic push
  325. #pragma clang diagnostic ignored "-Wcast-function-type-strict"
  326. #endif
  327. /* use fwrite as default function to store output */
  328. set->fwrite_func = (curl_write_callback)fwrite;
  329. /* use fread as default function to read input */
  330. set->fread_func_set = (curl_read_callback)fread;
  331. #if defined(__clang__) && __clang_major__ >= 16
  332. #pragma clang diagnostic pop
  333. #endif
  334. set->is_fread_set = 0;
  335. set->seek_client = ZERO_NULL;
  336. set->filesize = -1; /* we do not know the size */
  337. set->postfieldsize = -1; /* unknown size */
  338. set->maxredirs = 30; /* sensible default */
  339. set->method = HTTPREQ_GET; /* Default HTTP request */
  340. #ifndef CURL_DISABLE_RTSP
  341. set->rtspreq = RTSPREQ_OPTIONS; /* Default RTSP request */
  342. #endif
  343. #ifndef CURL_DISABLE_FTP
  344. set->ftp_use_epsv = TRUE; /* FTP defaults to EPSV operations */
  345. set->ftp_use_eprt = TRUE; /* FTP defaults to EPRT operations */
  346. set->ftp_use_pret = FALSE; /* mainly useful for drftpd servers */
  347. set->ftp_filemethod = FTPFILE_MULTICWD;
  348. set->ftp_skip_ip = TRUE; /* skip PASV IP by default */
  349. #endif
  350. set->dns_cache_timeout_ms = 60000; /* Timeout every 60 seconds by default */
  351. /* Timeout every 24 hours by default */
  352. set->general_ssl.ca_cache_timeout = 24 * 60 * 60;
  353. set->httpauth = CURLAUTH_BASIC; /* defaults to basic */
  354. #ifndef CURL_DISABLE_PROXY
  355. set->proxyport = 0;
  356. set->proxytype = CURLPROXY_HTTP; /* defaults to HTTP proxy */
  357. set->proxyauth = CURLAUTH_BASIC; /* defaults to basic */
  358. /* SOCKS5 proxy auth defaults to username/password + GSS-API */
  359. set->socks5auth = CURLAUTH_BASIC | CURLAUTH_GSSAPI;
  360. #endif
  361. Curl_mime_initpart(&set->mimepost);
  362. Curl_ssl_easy_config_init(data);
  363. #ifndef CURL_DISABLE_DOH
  364. set->doh_verifyhost = TRUE;
  365. set->doh_verifypeer = TRUE;
  366. #endif
  367. #ifdef USE_SSH
  368. /* defaults to any auth type */
  369. set->ssh_auth_types = CURLSSH_AUTH_DEFAULT;
  370. set->new_directory_perms = 0755; /* Default permissions */
  371. #endif
  372. set->new_file_perms = 0644; /* Default permissions */
  373. set->allowed_protocols = (curl_prot_t) CURLPROTO_ALL;
  374. set->redir_protocols = CURLPROTO_REDIR;
  375. #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
  376. /*
  377. * disallow unprotected protection negotiation NEC reference implementation
  378. * seem not to follow rfc1961 section 4.3/4.4
  379. */
  380. set->socks5_gssapi_nec = FALSE;
  381. #endif
  382. /* set default minimum TLS version */
  383. #ifdef USE_SSL
  384. Curl_setopt_SSLVERSION(data, CURLOPT_SSLVERSION, CURL_SSLVERSION_DEFAULT);
  385. #ifndef CURL_DISABLE_PROXY
  386. Curl_setopt_SSLVERSION(data, CURLOPT_PROXY_SSLVERSION,
  387. CURL_SSLVERSION_DEFAULT);
  388. #endif
  389. #endif
  390. #ifndef CURL_DISABLE_FTP
  391. set->wildcard_enabled = FALSE;
  392. set->chunk_bgn = ZERO_NULL;
  393. set->chunk_end = ZERO_NULL;
  394. set->fnmatch = ZERO_NULL;
  395. #endif
  396. set->tcp_keepalive = FALSE;
  397. set->tcp_keepintvl = 60;
  398. set->tcp_keepidle = 60;
  399. set->tcp_keepcnt = 9;
  400. set->tcp_fastopen = FALSE;
  401. set->tcp_nodelay = TRUE;
  402. set->ssl_enable_alpn = TRUE;
  403. set->expect_100_timeout = 1000L; /* Wait for a second by default. */
  404. set->sep_headers = TRUE; /* separated header lists by default */
  405. set->buffer_size = READBUFFER_SIZE;
  406. set->upload_buffer_size = UPLOADBUFFER_DEFAULT;
  407. set->happy_eyeballs_timeout = CURL_HET_DEFAULT;
  408. set->upkeep_interval_ms = CURL_UPKEEP_INTERVAL_DEFAULT;
  409. set->maxconnects = DEFAULT_CONNCACHE_SIZE; /* for easy handles */
  410. set->conn_max_idle_ms = 118 * 1000;
  411. set->conn_max_age_ms = 24 * 3600 * 1000;
  412. set->http09_allowed = FALSE;
  413. set->httpwant = CURL_HTTP_VERSION_NONE
  414. ;
  415. #if defined(USE_HTTP2) || defined(USE_HTTP3)
  416. memset(&set->priority, 0, sizeof(set->priority));
  417. #endif
  418. set->quick_exit = 0L;
  419. #ifndef CURL_DISABLE_WEBSOCKETS
  420. set->ws_raw_mode = FALSE;
  421. set->ws_no_auto_pong = FALSE;
  422. #endif
  423. }
  424. /* easy->meta_hash destructor. Should never be called as elements
  425. * MUST be added with their own destructor */
  426. static void easy_meta_freeentry(void *p)
  427. {
  428. (void)p;
  429. /* Will always be FALSE. Cannot use a 0 assert here since compilers
  430. * are not in agreement if they then want a NORETURN attribute or
  431. * not. *sigh* */
  432. DEBUGASSERT(p == NULL);
  433. }
  434. /**
  435. * Curl_open()
  436. *
  437. * @param curl is a pointer to a sessionhandle pointer that gets set by this
  438. * function.
  439. * @return CURLcode
  440. */
  441. CURLcode Curl_open(struct Curl_easy **curl)
  442. {
  443. struct Curl_easy *data;
  444. /* simple start-up: alloc the struct, init it with zeroes and return */
  445. data = calloc(1, sizeof(struct Curl_easy));
  446. if(!data) {
  447. /* this is a serious error */
  448. DEBUGF(curl_mfprintf(stderr, "Error: calloc of Curl_easy failed\n"));
  449. return CURLE_OUT_OF_MEMORY;
  450. }
  451. data->magic = CURLEASY_MAGIC_NUMBER;
  452. /* most recent connection is not yet defined */
  453. data->state.lastconnect_id = -1;
  454. data->state.recent_conn_id = -1;
  455. /* and not assigned an id yet */
  456. data->id = -1;
  457. data->mid = UINT_MAX;
  458. data->master_mid = UINT_MAX;
  459. data->progress.hide = TRUE;
  460. data->state.current_speed = -1; /* init to negative == impossible */
  461. Curl_hash_init(&data->meta_hash, 23,
  462. Curl_hash_str, curlx_str_key_compare, easy_meta_freeentry);
  463. curlx_dyn_init(&data->state.headerb, CURL_MAX_HTTP_HEADER);
  464. Curl_req_init(&data->req);
  465. Curl_initinfo(data);
  466. #ifndef CURL_DISABLE_HTTP
  467. Curl_llist_init(&data->state.httphdrs, NULL);
  468. #endif
  469. Curl_netrc_init(&data->state.netrc);
  470. Curl_init_userdefined(data);
  471. *curl = data;
  472. return CURLE_OK;
  473. }
  474. void Curl_conn_free(struct Curl_easy *data, struct connectdata *conn)
  475. {
  476. size_t i;
  477. DEBUGASSERT(conn);
  478. if(conn->handler && conn->handler->disconnect &&
  479. !conn->bits.shutdown_handler)
  480. conn->handler->disconnect(data, conn, TRUE);
  481. for(i = 0; i < CURL_ARRAYSIZE(conn->cfilter); ++i) {
  482. Curl_conn_cf_discard_all(data, conn, (int)i);
  483. }
  484. Curl_free_idnconverted_hostname(&conn->host);
  485. Curl_free_idnconverted_hostname(&conn->conn_to_host);
  486. #ifndef CURL_DISABLE_PROXY
  487. Curl_free_idnconverted_hostname(&conn->http_proxy.host);
  488. Curl_free_idnconverted_hostname(&conn->socks_proxy.host);
  489. Curl_safefree(conn->http_proxy.user);
  490. Curl_safefree(conn->socks_proxy.user);
  491. Curl_safefree(conn->http_proxy.passwd);
  492. Curl_safefree(conn->socks_proxy.passwd);
  493. Curl_safefree(conn->http_proxy.host.rawalloc); /* http proxy name buffer */
  494. Curl_safefree(conn->socks_proxy.host.rawalloc); /* socks proxy name buffer */
  495. #endif
  496. Curl_safefree(conn->user);
  497. Curl_safefree(conn->passwd);
  498. Curl_safefree(conn->sasl_authzid);
  499. Curl_safefree(conn->options);
  500. Curl_safefree(conn->oauth_bearer);
  501. Curl_safefree(conn->host.rawalloc); /* hostname buffer */
  502. Curl_safefree(conn->conn_to_host.rawalloc); /* hostname buffer */
  503. Curl_safefree(conn->hostname_resolve);
  504. Curl_safefree(conn->secondaryhostname);
  505. Curl_safefree(conn->localdev);
  506. Curl_ssl_conn_config_cleanup(conn);
  507. #ifdef USE_UNIX_SOCKETS
  508. Curl_safefree(conn->unix_domain_socket);
  509. #endif
  510. Curl_safefree(conn->destination);
  511. Curl_uint_spbset_destroy(&conn->xfers_attached);
  512. Curl_hash_destroy(&conn->meta_hash);
  513. free(conn); /* free all the connection oriented data */
  514. }
  515. /*
  516. * xfer_may_multiplex()
  517. *
  518. * Return a TRUE, iff the transfer can be done over an (appropriate)
  519. * multiplexed connection.
  520. */
  521. static bool xfer_may_multiplex(const struct Curl_easy *data,
  522. const struct connectdata *conn)
  523. {
  524. #ifndef CURL_DISABLE_HTTP
  525. /* If an HTTP protocol and multiplexing is enabled */
  526. if((conn->handler->protocol & PROTO_FAMILY_HTTP) &&
  527. (!conn->bits.protoconnstart || !conn->bits.close)) {
  528. if(Curl_multiplex_wanted(data->multi) &&
  529. (data->state.http_neg.allowed & (CURL_HTTP_V2x|CURL_HTTP_V3x)))
  530. /* allows HTTP/2 or newer */
  531. return TRUE;
  532. }
  533. #else
  534. (void)data;
  535. (void)conn;
  536. #endif
  537. return FALSE;
  538. }
  539. #ifndef CURL_DISABLE_PROXY
  540. static bool
  541. proxy_info_matches(const struct proxy_info *data,
  542. const struct proxy_info *needle)
  543. {
  544. if((data->proxytype == needle->proxytype) &&
  545. (data->port == needle->port) &&
  546. curl_strequal(data->host.name, needle->host.name))
  547. return TRUE;
  548. return FALSE;
  549. }
  550. static bool
  551. socks_proxy_info_matches(const struct proxy_info *data,
  552. const struct proxy_info *needle)
  553. {
  554. if(!proxy_info_matches(data, needle))
  555. return FALSE;
  556. /* the user information is case-sensitive
  557. or at least it is not defined as case-insensitive
  558. see https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1 */
  559. /* curl_strequal does a case insensitive comparison,
  560. so do not use it here! */
  561. if(Curl_timestrcmp(data->user, needle->user) ||
  562. Curl_timestrcmp(data->passwd, needle->passwd))
  563. return FALSE;
  564. return TRUE;
  565. }
  566. #else
  567. /* disabled, will not get called */
  568. #define proxy_info_matches(x,y) FALSE
  569. #define socks_proxy_info_matches(x,y) FALSE
  570. #endif
  571. /* A connection has to have been idle for less than 'conn_max_idle_ms'
  572. (the success rate is just too low after this), or created less than
  573. 'conn_max_age_ms' ago, to be subject for reuse. */
  574. static bool conn_maxage(struct Curl_easy *data,
  575. struct connectdata *conn,
  576. struct curltime now)
  577. {
  578. timediff_t age_ms;
  579. if(data->set.conn_max_idle_ms) {
  580. age_ms = curlx_timediff(now, conn->lastused);
  581. if(age_ms > data->set.conn_max_idle_ms) {
  582. infof(data, "Too old connection (%" FMT_TIMEDIFF_T
  583. " ms idle, max idle is %" FMT_TIMEDIFF_T " ms), disconnect it",
  584. age_ms, data->set.conn_max_idle_ms);
  585. return TRUE;
  586. }
  587. }
  588. if(data->set.conn_max_age_ms) {
  589. age_ms = curlx_timediff(now, conn->created);
  590. if(age_ms > data->set.conn_max_age_ms) {
  591. infof(data,
  592. "Too old connection (created %" FMT_TIMEDIFF_T
  593. " ms ago, max lifetime is %" FMT_TIMEDIFF_T " ms), disconnect it",
  594. age_ms, data->set.conn_max_age_ms);
  595. return TRUE;
  596. }
  597. }
  598. return FALSE;
  599. }
  600. /*
  601. * Return TRUE iff the given connection is considered dead.
  602. */
  603. bool Curl_conn_seems_dead(struct connectdata *conn,
  604. struct Curl_easy *data,
  605. struct curltime *pnow)
  606. {
  607. DEBUGASSERT(!data->conn);
  608. if(!CONN_INUSE(conn)) {
  609. /* The check for a dead socket makes sense only if the connection is not in
  610. use */
  611. bool dead;
  612. struct curltime now;
  613. if(!pnow) {
  614. now = curlx_now();
  615. pnow = &now;
  616. }
  617. if(conn_maxage(data, conn, *pnow)) {
  618. /* avoid check if already too old */
  619. dead = TRUE;
  620. }
  621. else if(conn->handler->connection_check) {
  622. /* The protocol has a special method for checking the state of the
  623. connection. Use it to check if the connection is dead. */
  624. unsigned int state;
  625. /* briefly attach the connection to this transfer for the purpose of
  626. checking it */
  627. Curl_attach_connection(data, conn);
  628. state = conn->handler->connection_check(data, conn, CONNCHECK_ISDEAD);
  629. dead = (state & CONNRESULT_DEAD);
  630. /* detach the connection again */
  631. Curl_detach_connection(data);
  632. }
  633. else {
  634. bool input_pending = FALSE;
  635. Curl_attach_connection(data, conn);
  636. dead = !Curl_conn_is_alive(data, conn, &input_pending);
  637. if(input_pending) {
  638. /* For reuse, we want a "clean" connection state. The includes
  639. * that we expect - in general - no waiting input data. Input
  640. * waiting might be a TLS Notify Close, for example. We reject
  641. * that.
  642. * For protocols where data from other end may arrive at
  643. * any time (HTTP/2 PING for example), the protocol handler needs
  644. * to install its own `connection_check` callback.
  645. */
  646. DEBUGF(infof(data, "connection has input pending, not reusable"));
  647. dead = TRUE;
  648. }
  649. Curl_detach_connection(data);
  650. }
  651. if(dead) {
  652. /* remove connection from cpool */
  653. infof(data, "Connection %" FMT_OFF_T " seems to be dead",
  654. conn->connection_id);
  655. return TRUE;
  656. }
  657. }
  658. return FALSE;
  659. }
  660. CURLcode Curl_conn_upkeep(struct Curl_easy *data,
  661. struct connectdata *conn,
  662. struct curltime *now)
  663. {
  664. CURLcode result = CURLE_OK;
  665. if(curlx_timediff(*now, conn->keepalive) <= data->set.upkeep_interval_ms)
  666. return result;
  667. /* briefly attach for action */
  668. Curl_attach_connection(data, conn);
  669. if(conn->handler->connection_check) {
  670. /* Do a protocol-specific keepalive check on the connection. */
  671. unsigned int rc;
  672. rc = conn->handler->connection_check(data, conn, CONNCHECK_KEEPALIVE);
  673. if(rc & CONNRESULT_DEAD)
  674. result = CURLE_RECV_ERROR;
  675. }
  676. else {
  677. /* Do the generic action on the FIRSTSOCKET filter chain */
  678. result = Curl_conn_keep_alive(data, conn, FIRSTSOCKET);
  679. }
  680. Curl_detach_connection(data);
  681. conn->keepalive = *now;
  682. return result;
  683. }
  684. #ifdef USE_SSH
  685. static bool ssh_config_matches(struct connectdata *one,
  686. struct connectdata *two)
  687. {
  688. struct ssh_conn *sshc1, *sshc2;
  689. sshc1 = Curl_conn_meta_get(one, CURL_META_SSH_CONN);
  690. sshc2 = Curl_conn_meta_get(two, CURL_META_SSH_CONN);
  691. return (sshc1 && sshc2 && Curl_safecmp(sshc1->rsa, sshc2->rsa) &&
  692. Curl_safecmp(sshc1->rsa_pub, sshc2->rsa_pub));
  693. }
  694. #endif
  695. struct url_conn_match {
  696. struct connectdata *found;
  697. struct Curl_easy *data;
  698. struct connectdata *needle;
  699. BIT(may_multiplex);
  700. BIT(want_ntlm_http);
  701. BIT(want_proxy_ntlm_http);
  702. BIT(wait_pipe);
  703. BIT(force_reuse);
  704. BIT(seen_pending_conn);
  705. BIT(seen_single_use_conn);
  706. BIT(seen_multiplex_conn);
  707. };
  708. static bool url_match_connect_config(struct connectdata *conn,
  709. struct url_conn_match *m)
  710. {
  711. /* connect-only or to-be-closed connections will not be reused */
  712. if(conn->connect_only || conn->bits.close || conn->bits.no_reuse)
  713. return FALSE;
  714. /* ip_version must match */
  715. if(m->data->set.ipver != CURL_IPRESOLVE_WHATEVER
  716. && m->data->set.ipver != conn->ip_version)
  717. return FALSE;
  718. if(m->needle->localdev || m->needle->localport) {
  719. /* If we are bound to a specific local end (IP+port), we must not reuse a
  720. random other one, although if we did not ask for a particular one we
  721. can reuse one that was bound.
  722. This comparison is a bit rough and too strict. Since the input
  723. parameters can be specified in numerous ways and still end up the same
  724. it would take a lot of processing to make it really accurate. Instead,
  725. this matching will assume that reuses of bound connections will most
  726. likely also reuse the exact same binding parameters and missing out a
  727. few edge cases should not hurt anyone much.
  728. */
  729. if((conn->localport != m->needle->localport) ||
  730. (conn->localportrange != m->needle->localportrange) ||
  731. (m->needle->localdev &&
  732. (!conn->localdev || strcmp(conn->localdev, m->needle->localdev))))
  733. return FALSE;
  734. }
  735. if(m->needle->bits.conn_to_host != conn->bits.conn_to_host)
  736. /* do not mix connections that use the "connect to host" feature and
  737. * connections that do not use this feature */
  738. return FALSE;
  739. if(m->needle->bits.conn_to_port != conn->bits.conn_to_port)
  740. /* do not mix connections that use the "connect to port" feature and
  741. * connections that do not use this feature */
  742. return FALSE;
  743. /* Does `conn` use the correct protocol? */
  744. #ifdef USE_UNIX_SOCKETS
  745. if(m->needle->unix_domain_socket) {
  746. if(!conn->unix_domain_socket)
  747. return FALSE;
  748. if(strcmp(m->needle->unix_domain_socket, conn->unix_domain_socket))
  749. return FALSE;
  750. if(m->needle->bits.abstract_unix_socket != conn->bits.abstract_unix_socket)
  751. return FALSE;
  752. }
  753. else if(conn->unix_domain_socket)
  754. return FALSE;
  755. #endif
  756. return TRUE;
  757. }
  758. static bool url_match_fully_connected(struct connectdata *conn,
  759. struct url_conn_match *m)
  760. {
  761. if(!Curl_conn_is_connected(conn, FIRSTSOCKET) ||
  762. conn->bits.upgrade_in_progress) {
  763. /* Not yet connected, or a protocol upgrade is in progress. The later
  764. * happens for HTTP/2 Upgrade: requests that need a response. */
  765. if(m->may_multiplex) {
  766. m->seen_pending_conn = TRUE;
  767. /* Do not pick a connection that has not connected yet */
  768. infof(m->data, "Connection #%" FMT_OFF_T
  769. " is not open enough, cannot reuse", conn->connection_id);
  770. }
  771. /* Do not pick a connection that has not connected yet */
  772. return FALSE;
  773. }
  774. return TRUE;
  775. }
  776. static bool url_match_multi(struct connectdata *conn,
  777. struct url_conn_match *m)
  778. {
  779. if(CONN_INUSE(conn)) {
  780. DEBUGASSERT(conn->attached_multi);
  781. if(conn->attached_multi != m->data->multi)
  782. return FALSE;
  783. }
  784. return TRUE;
  785. }
  786. static bool url_match_multiplex_needs(struct connectdata *conn,
  787. struct url_conn_match *m)
  788. {
  789. if(CONN_INUSE(conn)) {
  790. if(!conn->bits.multiplex) {
  791. /* conn busy and conn cannot take more transfers */
  792. m->seen_single_use_conn = TRUE;
  793. return FALSE;
  794. }
  795. m->seen_multiplex_conn = TRUE;
  796. if(!m->may_multiplex || !url_match_multi(conn, m))
  797. /* conn busy and transfer cannot be multiplexed */
  798. return FALSE;
  799. }
  800. return TRUE;
  801. }
  802. static bool url_match_multiplex_limits(struct connectdata *conn,
  803. struct url_conn_match *m)
  804. {
  805. if(CONN_INUSE(conn) && m->may_multiplex) {
  806. DEBUGASSERT(conn->bits.multiplex);
  807. /* If multiplexed, make sure we do not go over concurrency limit */
  808. if(CONN_ATTACHED(conn) >=
  809. Curl_multi_max_concurrent_streams(m->data->multi)) {
  810. infof(m->data, "client side MAX_CONCURRENT_STREAMS reached"
  811. ", skip (%u)", CONN_ATTACHED(conn));
  812. return FALSE;
  813. }
  814. if(CONN_ATTACHED(conn) >=
  815. Curl_conn_get_max_concurrent(m->data, conn, FIRSTSOCKET)) {
  816. infof(m->data, "MAX_CONCURRENT_STREAMS reached, skip (%u)",
  817. CONN_ATTACHED(conn));
  818. return FALSE;
  819. }
  820. /* When not multiplexed, we have a match here! */
  821. infof(m->data, "Multiplexed connection found");
  822. }
  823. return TRUE;
  824. }
  825. static bool url_match_ssl_use(struct connectdata *conn,
  826. struct url_conn_match *m)
  827. {
  828. if(m->needle->handler->flags & PROTOPT_SSL) {
  829. /* We are looking for SSL, if `conn` does not do it, not a match. */
  830. if(!Curl_conn_is_ssl(conn, FIRSTSOCKET))
  831. return FALSE;
  832. }
  833. else if(Curl_conn_is_ssl(conn, FIRSTSOCKET)) {
  834. /* If the protocol does not allow reuse of SSL connections OR
  835. is of another protocol family, not a match. */
  836. if(!(m->needle->handler->flags & PROTOPT_SSL_REUSE) ||
  837. (get_protocol_family(conn->handler) != m->needle->handler->protocol))
  838. return FALSE;
  839. }
  840. return TRUE;
  841. }
  842. #ifndef CURL_DISABLE_PROXY
  843. static bool url_match_proxy_use(struct connectdata *conn,
  844. struct url_conn_match *m)
  845. {
  846. if(m->needle->bits.httpproxy != conn->bits.httpproxy ||
  847. m->needle->bits.socksproxy != conn->bits.socksproxy)
  848. return FALSE;
  849. if(m->needle->bits.socksproxy &&
  850. !socks_proxy_info_matches(&m->needle->socks_proxy,
  851. &conn->socks_proxy))
  852. return FALSE;
  853. if(m->needle->bits.httpproxy) {
  854. if(m->needle->bits.tunnel_proxy != conn->bits.tunnel_proxy)
  855. return FALSE;
  856. if(!proxy_info_matches(&m->needle->http_proxy, &conn->http_proxy))
  857. return FALSE;
  858. if(IS_HTTPS_PROXY(m->needle->http_proxy.proxytype)) {
  859. /* https proxies come in different types, http/1.1, h2, ... */
  860. if(m->needle->http_proxy.proxytype != conn->http_proxy.proxytype)
  861. return FALSE;
  862. /* match SSL config to proxy */
  863. if(!Curl_ssl_conn_config_match(m->data, conn, TRUE)) {
  864. DEBUGF(infof(m->data,
  865. "Connection #%" FMT_OFF_T
  866. " has different SSL proxy parameters, cannot reuse",
  867. conn->connection_id));
  868. return FALSE;
  869. }
  870. /* the SSL config to the server, which may apply here is checked
  871. * further below */
  872. }
  873. }
  874. return TRUE;
  875. }
  876. #else
  877. #define url_match_proxy_use(c,m) ((void)c, (void)m, TRUE)
  878. #endif
  879. #ifndef CURL_DISABLE_HTTP
  880. static bool url_match_http_multiplex(struct connectdata *conn,
  881. struct url_conn_match *m)
  882. {
  883. if(m->may_multiplex &&
  884. (m->data->state.http_neg.allowed & (CURL_HTTP_V2x|CURL_HTTP_V3x)) &&
  885. (m->needle->handler->protocol & CURLPROTO_HTTP) &&
  886. !conn->httpversion_seen) {
  887. if(m->data->set.pipewait) {
  888. infof(m->data, "Server upgrade does not support multiplex yet, wait");
  889. m->found = NULL;
  890. m->wait_pipe = TRUE;
  891. return TRUE; /* stop searching, we want to wait */
  892. }
  893. infof(m->data, "Server upgrade cannot be used");
  894. return FALSE;
  895. }
  896. return TRUE;
  897. }
  898. static bool url_match_http_version(struct connectdata *conn,
  899. struct url_conn_match *m)
  900. {
  901. /* If looking for HTTP and the HTTP versions allowed do not include
  902. * the HTTP version of conn, continue looking. */
  903. if((m->needle->handler->protocol & PROTO_FAMILY_HTTP)) {
  904. switch(Curl_conn_http_version(m->data, conn)) {
  905. case 30:
  906. if(!(m->data->state.http_neg.allowed & CURL_HTTP_V3x)) {
  907. DEBUGF(infof(m->data, "not reusing conn #%" CURL_FORMAT_CURL_OFF_T
  908. ", we do not want h3", conn->connection_id));
  909. return FALSE;
  910. }
  911. break;
  912. case 20:
  913. if(!(m->data->state.http_neg.allowed & CURL_HTTP_V2x)) {
  914. DEBUGF(infof(m->data, "not reusing conn #%" CURL_FORMAT_CURL_OFF_T
  915. ", we do not want h2", conn->connection_id));
  916. return FALSE;
  917. }
  918. break;
  919. default:
  920. if(!(m->data->state.http_neg.allowed & CURL_HTTP_V1x)) {
  921. DEBUGF(infof(m->data, "not reusing conn #%" CURL_FORMAT_CURL_OFF_T
  922. ", we do not want h1", conn->connection_id));
  923. return FALSE;
  924. }
  925. break;
  926. }
  927. }
  928. return TRUE;
  929. }
  930. #else
  931. #define url_match_http_multiplex(c,m) ((void)c, (void)m, TRUE)
  932. #define url_match_http_version(c,m) ((void)c, (void)m, TRUE)
  933. #endif
  934. static bool url_match_proto_config(struct connectdata *conn,
  935. struct url_conn_match *m)
  936. {
  937. if(!url_match_http_version(conn, m))
  938. return FALSE;
  939. #ifdef USE_SSH
  940. if(get_protocol_family(m->needle->handler) & PROTO_FAMILY_SSH) {
  941. if(!ssh_config_matches(m->needle, conn))
  942. return FALSE;
  943. }
  944. #endif
  945. #ifndef CURL_DISABLE_FTP
  946. else if(get_protocol_family(m->needle->handler) & PROTO_FAMILY_FTP) {
  947. if(!ftp_conns_match(m->needle, conn))
  948. return FALSE;
  949. }
  950. #endif
  951. return TRUE;
  952. }
  953. static bool url_match_auth(struct connectdata *conn,
  954. struct url_conn_match *m)
  955. {
  956. if(!(m->needle->handler->flags & PROTOPT_CREDSPERREQUEST)) {
  957. /* This protocol requires credentials per connection,
  958. so verify that we are using the same name and password as well */
  959. if(Curl_timestrcmp(m->needle->user, conn->user) ||
  960. Curl_timestrcmp(m->needle->passwd, conn->passwd) ||
  961. Curl_timestrcmp(m->needle->sasl_authzid, conn->sasl_authzid) ||
  962. Curl_timestrcmp(m->needle->oauth_bearer, conn->oauth_bearer)) {
  963. /* one of them was different */
  964. return FALSE;
  965. }
  966. }
  967. #ifdef HAVE_GSSAPI
  968. /* GSS delegation differences do not actually affect every connection
  969. and auth method, but this check takes precaution before efficiency */
  970. if(m->needle->gssapi_delegation != conn->gssapi_delegation)
  971. return FALSE;
  972. #endif
  973. return TRUE;
  974. }
  975. static bool url_match_destination(struct connectdata *conn,
  976. struct url_conn_match *m)
  977. {
  978. /* Additional match requirements if talking TLS OR
  979. * not talking to an HTTP proxy OR using a tunnel through a proxy */
  980. if((m->needle->handler->flags&PROTOPT_SSL)
  981. #ifndef CURL_DISABLE_PROXY
  982. || !m->needle->bits.httpproxy || m->needle->bits.tunnel_proxy
  983. #endif
  984. ) {
  985. if(!curl_strequal(m->needle->handler->scheme, conn->handler->scheme)) {
  986. /* `needle` and `conn` do not have the same scheme... */
  987. if(get_protocol_family(conn->handler) != m->needle->handler->protocol) {
  988. /* and `conn`s protocol family is not the protocol `needle` wants.
  989. * IMAPS would work for IMAP, but no vice versa. */
  990. return FALSE;
  991. }
  992. /* We are in an IMAPS vs IMAP like case. We expect `conn` to have SSL */
  993. if(!Curl_conn_is_ssl(conn, FIRSTSOCKET)) {
  994. DEBUGF(infof(m->data,
  995. "Connection #%" FMT_OFF_T " has compatible protocol family, "
  996. "but no SSL, no match", conn->connection_id));
  997. return FALSE;
  998. }
  999. }
  1000. /* If needle has "conn_to_*" set, conn must match this */
  1001. if((m->needle->bits.conn_to_host && !curl_strequal(
  1002. m->needle->conn_to_host.name, conn->conn_to_host.name)) ||
  1003. (m->needle->bits.conn_to_port &&
  1004. m->needle->conn_to_port != conn->conn_to_port))
  1005. return FALSE;
  1006. /* hostname and port must match */
  1007. if(!curl_strequal(m->needle->host.name, conn->host.name) ||
  1008. m->needle->remote_port != conn->remote_port)
  1009. return FALSE;
  1010. }
  1011. return TRUE;
  1012. }
  1013. static bool url_match_ssl_config(struct connectdata *conn,
  1014. struct url_conn_match *m)
  1015. {
  1016. /* If talking TLS, conn needs to use the same SSL options. */
  1017. if((m->needle->handler->flags & PROTOPT_SSL) &&
  1018. !Curl_ssl_conn_config_match(m->data, conn, FALSE)) {
  1019. DEBUGF(infof(m->data,
  1020. "Connection #%" FMT_OFF_T
  1021. " has different SSL parameters, cannot reuse",
  1022. conn->connection_id));
  1023. return FALSE;
  1024. }
  1025. return TRUE;
  1026. }
  1027. #ifdef USE_NTLM
  1028. static bool url_match_auth_ntlm(struct connectdata *conn,
  1029. struct url_conn_match *m)
  1030. {
  1031. /* If we are looking for an HTTP+NTLM connection, check if this is
  1032. already authenticating with the right credentials. If not, keep
  1033. looking so that we can reuse NTLM connections if
  1034. possible. (Especially we must not reuse the same connection if
  1035. partway through a handshake!) */
  1036. if(m->want_ntlm_http) {
  1037. if(Curl_timestrcmp(m->needle->user, conn->user) ||
  1038. Curl_timestrcmp(m->needle->passwd, conn->passwd)) {
  1039. /* we prefer a credential match, but this is at least a connection
  1040. that can be reused and "upgraded" to NTLM */
  1041. if(conn->http_ntlm_state == NTLMSTATE_NONE)
  1042. m->found = conn;
  1043. return FALSE;
  1044. }
  1045. }
  1046. else if(conn->http_ntlm_state != NTLMSTATE_NONE) {
  1047. /* Connection is using NTLM auth but we do not want NTLM */
  1048. return FALSE;
  1049. }
  1050. #ifndef CURL_DISABLE_PROXY
  1051. /* Same for Proxy NTLM authentication */
  1052. if(m->want_proxy_ntlm_http) {
  1053. /* Both conn->http_proxy.user and conn->http_proxy.passwd can be
  1054. * NULL */
  1055. if(!conn->http_proxy.user || !conn->http_proxy.passwd)
  1056. return FALSE;
  1057. if(Curl_timestrcmp(m->needle->http_proxy.user,
  1058. conn->http_proxy.user) ||
  1059. Curl_timestrcmp(m->needle->http_proxy.passwd,
  1060. conn->http_proxy.passwd))
  1061. return FALSE;
  1062. }
  1063. else if(conn->proxy_ntlm_state != NTLMSTATE_NONE) {
  1064. /* Proxy connection is using NTLM auth but we do not want NTLM */
  1065. return FALSE;
  1066. }
  1067. #endif
  1068. if(m->want_ntlm_http || m->want_proxy_ntlm_http) {
  1069. /* Credentials are already checked, we may use this connection.
  1070. * With NTLM being weird as it is, we MUST use a
  1071. * connection where it has already been fully negotiated.
  1072. * If it has not, we keep on looking for a better one. */
  1073. m->found = conn;
  1074. if((m->want_ntlm_http &&
  1075. (conn->http_ntlm_state != NTLMSTATE_NONE)) ||
  1076. (m->want_proxy_ntlm_http &&
  1077. (conn->proxy_ntlm_state != NTLMSTATE_NONE))) {
  1078. /* We must use this connection, no other */
  1079. m->force_reuse = TRUE;
  1080. return TRUE;
  1081. }
  1082. /* Continue look up for a better connection */
  1083. return FALSE;
  1084. }
  1085. return TRUE;
  1086. }
  1087. #else
  1088. #define url_match_auth_ntlm(c,m) ((void)c, (void)m, TRUE)
  1089. #endif
  1090. static bool url_match_conn(struct connectdata *conn, void *userdata)
  1091. {
  1092. struct url_conn_match *m = userdata;
  1093. /* Check if `conn` can be used for transfer `m->data` */
  1094. /* general connect config setting match? */
  1095. if(!url_match_connect_config(conn, m))
  1096. return FALSE;
  1097. /* match for destination and protocol? */
  1098. if(!url_match_destination(conn, m))
  1099. return FALSE;
  1100. if(!url_match_fully_connected(conn, m))
  1101. return FALSE;
  1102. if(!url_match_multiplex_needs(conn, m))
  1103. return FALSE;
  1104. if(!url_match_ssl_use(conn, m))
  1105. return FALSE;
  1106. if(!url_match_proxy_use(conn, m))
  1107. return FALSE;
  1108. if(!url_match_ssl_config(conn, m))
  1109. return FALSE;
  1110. if(!url_match_http_multiplex(conn, m))
  1111. return FALSE;
  1112. else if(m->wait_pipe)
  1113. /* we decided to wait on PIPELINING */
  1114. return TRUE;
  1115. if(!url_match_auth(conn, m))
  1116. return FALSE;
  1117. if(!url_match_proto_config(conn, m))
  1118. return FALSE;
  1119. if(!url_match_auth_ntlm(conn, m))
  1120. return FALSE;
  1121. else if(m->force_reuse)
  1122. return TRUE;
  1123. if(!url_match_multiplex_limits(conn, m))
  1124. return FALSE;
  1125. if(!CONN_INUSE(conn) && Curl_conn_seems_dead(conn, m->data, NULL)) {
  1126. /* remove and disconnect. */
  1127. Curl_conn_terminate(m->data, conn, FALSE);
  1128. return FALSE;
  1129. }
  1130. /* conn matches our needs. */
  1131. m->found = conn;
  1132. return TRUE;
  1133. }
  1134. static bool url_match_result(bool result, void *userdata)
  1135. {
  1136. struct url_conn_match *match = userdata;
  1137. (void)result;
  1138. if(match->found) {
  1139. /* Attach it now while still under lock, so the connection does
  1140. * no longer appear idle and can be reaped. */
  1141. Curl_attach_connection(match->data, match->found);
  1142. return TRUE;
  1143. }
  1144. else if(match->seen_single_use_conn && !match->seen_multiplex_conn) {
  1145. /* We've seen a single-use, existing connection to the destination and
  1146. * no multiplexed one. It seems safe to assume that the server does
  1147. * not support multiplexing. */
  1148. match->wait_pipe = FALSE;
  1149. }
  1150. else if(match->seen_pending_conn && match->data->set.pipewait) {
  1151. infof(match->data,
  1152. "Found pending candidate for reuse and CURLOPT_PIPEWAIT is set");
  1153. match->wait_pipe = TRUE;
  1154. }
  1155. match->force_reuse = FALSE;
  1156. return FALSE;
  1157. }
  1158. /*
  1159. * Given one filled in connection struct (named needle), this function should
  1160. * detect if there already is one that has all the significant details
  1161. * exactly the same and thus should be used instead.
  1162. *
  1163. * If there is a match, this function returns TRUE - and has marked the
  1164. * connection as 'in-use'. It must later be called with ConnectionDone() to
  1165. * return back to 'idle' (unused) state.
  1166. *
  1167. * The force_reuse flag is set if the connection must be used.
  1168. */
  1169. static bool
  1170. ConnectionExists(struct Curl_easy *data,
  1171. struct connectdata *needle,
  1172. struct connectdata **usethis,
  1173. bool *force_reuse,
  1174. bool *waitpipe)
  1175. {
  1176. struct url_conn_match match;
  1177. bool result;
  1178. memset(&match, 0, sizeof(match));
  1179. match.data = data;
  1180. match.needle = needle;
  1181. match.may_multiplex = xfer_may_multiplex(data, needle);
  1182. #ifdef USE_NTLM
  1183. match.want_ntlm_http = ((data->state.authhost.want & CURLAUTH_NTLM) &&
  1184. (needle->handler->protocol & PROTO_FAMILY_HTTP));
  1185. #ifndef CURL_DISABLE_PROXY
  1186. match.want_proxy_ntlm_http =
  1187. (needle->bits.proxy_user_passwd &&
  1188. (data->state.authproxy.want & CURLAUTH_NTLM) &&
  1189. (needle->handler->protocol & PROTO_FAMILY_HTTP));
  1190. #endif
  1191. #endif
  1192. /* Find a connection in the pool that matches what "data + needle"
  1193. * requires. If a suitable candidate is found, it is attached to "data". */
  1194. result = Curl_cpool_find(data, needle->destination,
  1195. url_match_conn, url_match_result, &match);
  1196. /* wait_pipe is TRUE if we encounter a bundle that is undecided. There
  1197. * is no matching connection then, yet. */
  1198. *usethis = match.found;
  1199. *force_reuse = match.force_reuse;
  1200. *waitpipe = match.wait_pipe;
  1201. return result;
  1202. }
  1203. /*
  1204. * Allocate and initialize a new connectdata object.
  1205. */
  1206. static struct connectdata *allocate_conn(struct Curl_easy *data)
  1207. {
  1208. struct connectdata *conn = calloc(1, sizeof(struct connectdata));
  1209. if(!conn)
  1210. return NULL;
  1211. /* and we setup a few fields in case we end up actually using this struct */
  1212. conn->sock[FIRSTSOCKET] = CURL_SOCKET_BAD; /* no file descriptor */
  1213. conn->sock[SECONDARYSOCKET] = CURL_SOCKET_BAD; /* no file descriptor */
  1214. conn->recv_idx = 0; /* default for receiving transfer data */
  1215. conn->send_idx = 0; /* default for sending transfer data */
  1216. conn->connection_id = -1; /* no ID */
  1217. conn->remote_port = -1; /* unknown at this point */
  1218. /* Default protocol-independent behavior does not support persistent
  1219. connections, so we set this to force-close. Protocols that support
  1220. this need to set this to FALSE in their "curl_do" functions. */
  1221. connclose(conn, "Default to force-close");
  1222. /* Store creation time to help future close decision making */
  1223. conn->created = curlx_now();
  1224. /* Store current time to give a baseline to keepalive connection times. */
  1225. conn->keepalive = conn->created;
  1226. #ifndef CURL_DISABLE_PROXY
  1227. conn->http_proxy.proxytype = data->set.proxytype;
  1228. conn->socks_proxy.proxytype = CURLPROXY_SOCKS4;
  1229. /* note that these two proxy bits are now just on what looks to be
  1230. requested, they may be altered down the road */
  1231. conn->bits.proxy = (data->set.str[STRING_PROXY] &&
  1232. *data->set.str[STRING_PROXY]);
  1233. conn->bits.httpproxy = (conn->bits.proxy &&
  1234. (conn->http_proxy.proxytype == CURLPROXY_HTTP ||
  1235. conn->http_proxy.proxytype == CURLPROXY_HTTP_1_0 ||
  1236. IS_HTTPS_PROXY(conn->http_proxy.proxytype)));
  1237. conn->bits.socksproxy = (conn->bits.proxy && !conn->bits.httpproxy);
  1238. if(data->set.str[STRING_PRE_PROXY] && *data->set.str[STRING_PRE_PROXY]) {
  1239. conn->bits.proxy = TRUE;
  1240. conn->bits.socksproxy = TRUE;
  1241. }
  1242. conn->bits.proxy_user_passwd = !!data->state.aptr.proxyuser;
  1243. conn->bits.tunnel_proxy = data->set.tunnel_thru_httpproxy;
  1244. #endif /* CURL_DISABLE_PROXY */
  1245. #ifndef CURL_DISABLE_FTP
  1246. conn->bits.ftp_use_epsv = data->set.ftp_use_epsv;
  1247. conn->bits.ftp_use_eprt = data->set.ftp_use_eprt;
  1248. #endif
  1249. conn->ip_version = data->set.ipver;
  1250. conn->connect_only = data->set.connect_only;
  1251. conn->transport_wanted = TRNSPRT_TCP; /* most of them are TCP streams */
  1252. /* Initialize the attached xfers bitset */
  1253. Curl_uint_spbset_init(&conn->xfers_attached);
  1254. /* Store the local bind parameters that will be used for this connection */
  1255. if(data->set.str[STRING_DEVICE]) {
  1256. conn->localdev = strdup(data->set.str[STRING_DEVICE]);
  1257. if(!conn->localdev)
  1258. goto error;
  1259. }
  1260. #ifndef CURL_DISABLE_BINDLOCAL
  1261. conn->localportrange = data->set.localportrange;
  1262. conn->localport = data->set.localport;
  1263. #endif
  1264. /* the close socket stuff needs to be copied to the connection struct as
  1265. it may live on without (this specific) Curl_easy */
  1266. conn->fclosesocket = data->set.fclosesocket;
  1267. conn->closesocket_client = data->set.closesocket_client;
  1268. conn->lastused = conn->created;
  1269. #ifdef HAVE_GSSAPI
  1270. conn->gssapi_delegation = data->set.gssapi_delegation;
  1271. #endif
  1272. return conn;
  1273. error:
  1274. free(conn->localdev);
  1275. free(conn);
  1276. return NULL;
  1277. }
  1278. const struct Curl_handler *Curl_get_scheme_handler(const char *scheme)
  1279. {
  1280. return Curl_getn_scheme_handler(scheme, strlen(scheme));
  1281. }
  1282. /* returns the handler if the given scheme is built-in */
  1283. const struct Curl_handler *Curl_getn_scheme_handler(const char *scheme,
  1284. size_t len)
  1285. {
  1286. /* table generated by schemetable.c:
  1287. 1. gcc schemetable.c && ./a.out
  1288. 2. check how small the table gets
  1289. 3. tweak the hash algorithm, then rerun from 1
  1290. 4. when the table is good enough
  1291. 5. copy the table into this source code
  1292. 6. make sure this function uses the same hash function that worked for
  1293. schemetable.c
  1294. 7. if needed, adjust the #ifdefs in schemetable.c and rerun
  1295. */
  1296. static const struct Curl_handler * const protocols[67] = {
  1297. #ifndef CURL_DISABLE_FILE
  1298. &Curl_handler_file,
  1299. #else
  1300. NULL,
  1301. #endif
  1302. NULL, NULL,
  1303. #if defined(USE_SSL) && !defined(CURL_DISABLE_GOPHER)
  1304. &Curl_handler_gophers,
  1305. #else
  1306. NULL,
  1307. #endif
  1308. NULL,
  1309. #ifdef USE_LIBRTMP
  1310. &Curl_handler_rtmpe,
  1311. #else
  1312. NULL,
  1313. #endif
  1314. #ifndef CURL_DISABLE_SMTP
  1315. &Curl_handler_smtp,
  1316. #else
  1317. NULL,
  1318. #endif
  1319. #ifdef USE_SSH
  1320. &Curl_handler_sftp,
  1321. #else
  1322. NULL,
  1323. #endif
  1324. #if !defined(CURL_DISABLE_SMB) && defined(USE_CURL_NTLM_CORE) && \
  1325. (SIZEOF_CURL_OFF_T > 4)
  1326. &Curl_handler_smb,
  1327. #else
  1328. NULL,
  1329. #endif
  1330. #if defined(USE_SSL) && !defined(CURL_DISABLE_SMTP)
  1331. &Curl_handler_smtps,
  1332. #else
  1333. NULL,
  1334. #endif
  1335. #ifndef CURL_DISABLE_TELNET
  1336. &Curl_handler_telnet,
  1337. #else
  1338. NULL,
  1339. #endif
  1340. #ifndef CURL_DISABLE_GOPHER
  1341. &Curl_handler_gopher,
  1342. #else
  1343. NULL,
  1344. #endif
  1345. #ifndef CURL_DISABLE_TFTP
  1346. &Curl_handler_tftp,
  1347. #else
  1348. NULL,
  1349. #endif
  1350. NULL, NULL, NULL,
  1351. #if defined(USE_SSL) && !defined(CURL_DISABLE_FTP)
  1352. &Curl_handler_ftps,
  1353. #else
  1354. NULL,
  1355. #endif
  1356. #ifndef CURL_DISABLE_HTTP
  1357. &Curl_handler_http,
  1358. #else
  1359. NULL,
  1360. #endif
  1361. #ifndef CURL_DISABLE_IMAP
  1362. &Curl_handler_imap,
  1363. #else
  1364. NULL,
  1365. #endif
  1366. #ifdef USE_LIBRTMP
  1367. &Curl_handler_rtmps,
  1368. #else
  1369. NULL,
  1370. #endif
  1371. #ifdef USE_LIBRTMP
  1372. &Curl_handler_rtmpt,
  1373. #else
  1374. NULL,
  1375. #endif
  1376. NULL, NULL, NULL,
  1377. #if !defined(CURL_DISABLE_LDAP) && \
  1378. !defined(CURL_DISABLE_LDAPS) && \
  1379. ((defined(USE_OPENLDAP) && defined(USE_SSL)) || \
  1380. (!defined(USE_OPENLDAP) && defined(HAVE_LDAP_SSL)))
  1381. &Curl_handler_ldaps,
  1382. #else
  1383. NULL,
  1384. #endif
  1385. #if !defined(CURL_DISABLE_WEBSOCKETS) && \
  1386. defined(USE_SSL) && !defined(CURL_DISABLE_HTTP)
  1387. &Curl_handler_wss,
  1388. #else
  1389. NULL,
  1390. #endif
  1391. #if defined(USE_SSL) && !defined(CURL_DISABLE_HTTP)
  1392. &Curl_handler_https,
  1393. #else
  1394. NULL,
  1395. #endif
  1396. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1397. #ifndef CURL_DISABLE_RTSP
  1398. &Curl_handler_rtsp,
  1399. #else
  1400. NULL,
  1401. #endif
  1402. #if defined(USE_SSL) && !defined(CURL_DISABLE_SMB) && \
  1403. defined(USE_CURL_NTLM_CORE) && (SIZEOF_CURL_OFF_T > 4)
  1404. &Curl_handler_smbs,
  1405. #else
  1406. NULL,
  1407. #endif
  1408. #if defined(USE_SSH)
  1409. &Curl_handler_scp,
  1410. #else
  1411. NULL,
  1412. #endif
  1413. NULL, NULL, NULL,
  1414. #ifndef CURL_DISABLE_POP3
  1415. &Curl_handler_pop3,
  1416. #else
  1417. NULL,
  1418. #endif
  1419. NULL, NULL,
  1420. #ifdef USE_LIBRTMP
  1421. &Curl_handler_rtmp,
  1422. #else
  1423. NULL,
  1424. #endif
  1425. NULL, NULL, NULL,
  1426. #ifdef USE_LIBRTMP
  1427. &Curl_handler_rtmpte,
  1428. #else
  1429. NULL,
  1430. #endif
  1431. NULL, NULL, NULL,
  1432. #ifndef CURL_DISABLE_DICT
  1433. &Curl_handler_dict,
  1434. #else
  1435. NULL,
  1436. #endif
  1437. NULL, NULL, NULL,
  1438. #ifndef CURL_DISABLE_MQTT
  1439. &Curl_handler_mqtt,
  1440. #else
  1441. NULL,
  1442. #endif
  1443. #if defined(USE_SSL) && !defined(CURL_DISABLE_POP3)
  1444. &Curl_handler_pop3s,
  1445. #else
  1446. NULL,
  1447. #endif
  1448. #if defined(USE_SSL) && !defined(CURL_DISABLE_IMAP)
  1449. &Curl_handler_imaps,
  1450. #else
  1451. NULL,
  1452. #endif
  1453. NULL,
  1454. #if !defined(CURL_DISABLE_WEBSOCKETS) && !defined(CURL_DISABLE_HTTP)
  1455. &Curl_handler_ws,
  1456. #else
  1457. NULL,
  1458. #endif
  1459. NULL,
  1460. #ifdef USE_LIBRTMP
  1461. &Curl_handler_rtmpts,
  1462. #else
  1463. NULL,
  1464. #endif
  1465. #ifndef CURL_DISABLE_LDAP
  1466. &Curl_handler_ldap,
  1467. #else
  1468. NULL,
  1469. #endif
  1470. NULL, NULL,
  1471. #ifndef CURL_DISABLE_FTP
  1472. &Curl_handler_ftp,
  1473. #else
  1474. NULL,
  1475. #endif
  1476. };
  1477. if(len && (len <= 7)) {
  1478. const char *s = scheme;
  1479. size_t l = len;
  1480. const struct Curl_handler *h;
  1481. unsigned int c = 978;
  1482. while(l) {
  1483. c <<= 5;
  1484. c += (unsigned int)Curl_raw_tolower(*s);
  1485. s++;
  1486. l--;
  1487. }
  1488. h = protocols[c % 67];
  1489. if(h && curl_strnequal(scheme, h->scheme, len) && !h->scheme[len])
  1490. return h;
  1491. }
  1492. return NULL;
  1493. }
  1494. static CURLcode findprotocol(struct Curl_easy *data,
  1495. struct connectdata *conn,
  1496. const char *protostr)
  1497. {
  1498. const struct Curl_handler *p = Curl_get_scheme_handler(protostr);
  1499. if(p && /* Protocol found in table. Check if allowed */
  1500. (data->set.allowed_protocols & p->protocol)) {
  1501. /* it is allowed for "normal" request, now do an extra check if this is
  1502. the result of a redirect */
  1503. if(data->state.this_is_a_follow &&
  1504. !(data->set.redir_protocols & p->protocol))
  1505. /* nope, get out */
  1506. ;
  1507. else {
  1508. /* Perform setup complement if some. */
  1509. conn->handler = conn->given = p;
  1510. /* 'port' and 'remote_port' are set in setup_connection_internals() */
  1511. return CURLE_OK;
  1512. }
  1513. }
  1514. /* The protocol was not found in the table, but we do not have to assign it
  1515. to anything since it is already assigned to a dummy-struct in the
  1516. create_conn() function when the connectdata struct is allocated. */
  1517. failf(data, "Protocol \"%s\" %s%s", protostr,
  1518. p ? "disabled" : "not supported",
  1519. data->state.this_is_a_follow ? " (in redirect)":"");
  1520. return CURLE_UNSUPPORTED_PROTOCOL;
  1521. }
  1522. CURLcode Curl_uc_to_curlcode(CURLUcode uc)
  1523. {
  1524. switch(uc) {
  1525. default:
  1526. return CURLE_URL_MALFORMAT;
  1527. case CURLUE_UNSUPPORTED_SCHEME:
  1528. return CURLE_UNSUPPORTED_PROTOCOL;
  1529. case CURLUE_OUT_OF_MEMORY:
  1530. return CURLE_OUT_OF_MEMORY;
  1531. case CURLUE_USER_NOT_ALLOWED:
  1532. return CURLE_LOGIN_DENIED;
  1533. }
  1534. }
  1535. #ifdef USE_IPV6
  1536. /*
  1537. * If the URL was set with an IPv6 numerical address with a zone id part, set
  1538. * the scope_id based on that!
  1539. */
  1540. static void zonefrom_url(CURLU *uh, struct Curl_easy *data,
  1541. struct connectdata *conn)
  1542. {
  1543. char *zoneid;
  1544. CURLUcode uc = curl_url_get(uh, CURLUPART_ZONEID, &zoneid, 0);
  1545. #ifdef CURL_DISABLE_VERBOSE_STRINGS
  1546. (void)data;
  1547. #endif
  1548. if(!uc && zoneid) {
  1549. const char *p = zoneid;
  1550. curl_off_t scope;
  1551. if(!curlx_str_number(&p, &scope, UINT_MAX))
  1552. /* A plain number, use it directly as a scope id. */
  1553. conn->scope_id = (unsigned int)scope;
  1554. #ifdef HAVE_IF_NAMETOINDEX
  1555. else {
  1556. #elif defined(_WIN32)
  1557. else if(Curl_if_nametoindex) {
  1558. #endif
  1559. #if defined(HAVE_IF_NAMETOINDEX) || defined(_WIN32)
  1560. /* Zone identifier is not numeric */
  1561. unsigned int scopeidx = 0;
  1562. #ifdef HAVE_IF_NAMETOINDEX
  1563. scopeidx = if_nametoindex(zoneid);
  1564. #else
  1565. scopeidx = Curl_if_nametoindex(zoneid);
  1566. #endif
  1567. if(!scopeidx) {
  1568. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  1569. char buffer[STRERROR_LEN];
  1570. infof(data, "Invalid zoneid: %s; %s", zoneid,
  1571. curlx_strerror(errno, buffer, sizeof(buffer)));
  1572. #endif
  1573. }
  1574. else
  1575. conn->scope_id = scopeidx;
  1576. }
  1577. #endif /* HAVE_IF_NAMETOINDEX || _WIN32 */
  1578. free(zoneid);
  1579. }
  1580. }
  1581. #else
  1582. #define zonefrom_url(a,b,c) Curl_nop_stmt
  1583. #endif
  1584. /*
  1585. * Parse URL and fill in the relevant members of the connection struct.
  1586. */
  1587. static CURLcode parseurlandfillconn(struct Curl_easy *data,
  1588. struct connectdata *conn)
  1589. {
  1590. CURLcode result;
  1591. CURLU *uh;
  1592. CURLUcode uc;
  1593. char *hostname;
  1594. bool use_set_uh = (data->set.uh && !data->state.this_is_a_follow);
  1595. up_free(data); /* cleanup previous leftovers first */
  1596. /* parse the URL */
  1597. if(use_set_uh) {
  1598. uh = data->state.uh = curl_url_dup(data->set.uh);
  1599. }
  1600. else {
  1601. uh = data->state.uh = curl_url();
  1602. }
  1603. if(!uh)
  1604. return CURLE_OUT_OF_MEMORY;
  1605. if(data->set.str[STRING_DEFAULT_PROTOCOL] &&
  1606. !Curl_is_absolute_url(data->state.url, NULL, 0, TRUE)) {
  1607. char *url = curl_maprintf("%s://%s",
  1608. data->set.str[STRING_DEFAULT_PROTOCOL],
  1609. data->state.url);
  1610. if(!url)
  1611. return CURLE_OUT_OF_MEMORY;
  1612. if(data->state.url_alloc)
  1613. free(data->state.url);
  1614. data->state.url = url;
  1615. data->state.url_alloc = TRUE;
  1616. }
  1617. if(!use_set_uh) {
  1618. char *newurl;
  1619. uc = curl_url_set(uh, CURLUPART_URL, data->state.url, (unsigned int)
  1620. (CURLU_GUESS_SCHEME |
  1621. CURLU_NON_SUPPORT_SCHEME |
  1622. (data->set.disallow_username_in_url ?
  1623. CURLU_DISALLOW_USER : 0) |
  1624. (data->set.path_as_is ? CURLU_PATH_AS_IS : 0)));
  1625. if(uc) {
  1626. failf(data, "URL rejected: %s", curl_url_strerror(uc));
  1627. return Curl_uc_to_curlcode(uc);
  1628. }
  1629. /* after it was parsed, get the generated normalized version */
  1630. uc = curl_url_get(uh, CURLUPART_URL, &newurl, 0);
  1631. if(uc)
  1632. return Curl_uc_to_curlcode(uc);
  1633. if(data->state.url_alloc)
  1634. free(data->state.url);
  1635. data->state.url = newurl;
  1636. data->state.url_alloc = TRUE;
  1637. }
  1638. uc = curl_url_get(uh, CURLUPART_SCHEME, &data->state.up.scheme, 0);
  1639. if(uc)
  1640. return Curl_uc_to_curlcode(uc);
  1641. uc = curl_url_get(uh, CURLUPART_HOST, &data->state.up.hostname, 0);
  1642. if(uc) {
  1643. if(!curl_strequal("file", data->state.up.scheme))
  1644. return CURLE_OUT_OF_MEMORY;
  1645. }
  1646. else if(strlen(data->state.up.hostname) > MAX_URL_LEN) {
  1647. failf(data, "Too long hostname (maximum is %d)", MAX_URL_LEN);
  1648. return CURLE_URL_MALFORMAT;
  1649. }
  1650. hostname = data->state.up.hostname;
  1651. if(hostname && hostname[0] == '[') {
  1652. /* This looks like an IPv6 address literal. See if there is an address
  1653. scope. */
  1654. size_t hlen;
  1655. conn->bits.ipv6_ip = TRUE;
  1656. /* cut off the brackets! */
  1657. hostname++;
  1658. hlen = strlen(hostname);
  1659. hostname[hlen - 1] = 0;
  1660. zonefrom_url(uh, data, conn);
  1661. }
  1662. /* make sure the connect struct gets its own copy of the hostname */
  1663. conn->host.rawalloc = strdup(hostname ? hostname : "");
  1664. if(!conn->host.rawalloc)
  1665. return CURLE_OUT_OF_MEMORY;
  1666. conn->host.name = conn->host.rawalloc;
  1667. /*************************************************************
  1668. * IDN-convert the hostnames
  1669. *************************************************************/
  1670. result = Curl_idnconvert_hostname(&conn->host);
  1671. if(result)
  1672. return result;
  1673. #ifndef CURL_DISABLE_HSTS
  1674. /* HSTS upgrade */
  1675. if(data->hsts && curl_strequal("http", data->state.up.scheme)) {
  1676. /* This MUST use the IDN decoded name */
  1677. if(Curl_hsts(data->hsts, conn->host.name, strlen(conn->host.name), TRUE)) {
  1678. char *url;
  1679. Curl_safefree(data->state.up.scheme);
  1680. uc = curl_url_set(uh, CURLUPART_SCHEME, "https", 0);
  1681. if(uc)
  1682. return Curl_uc_to_curlcode(uc);
  1683. if(data->state.url_alloc)
  1684. Curl_safefree(data->state.url);
  1685. /* after update, get the updated version */
  1686. uc = curl_url_get(uh, CURLUPART_URL, &url, 0);
  1687. if(uc)
  1688. return Curl_uc_to_curlcode(uc);
  1689. uc = curl_url_get(uh, CURLUPART_SCHEME, &data->state.up.scheme, 0);
  1690. if(uc) {
  1691. free(url);
  1692. return Curl_uc_to_curlcode(uc);
  1693. }
  1694. data->state.url = url;
  1695. data->state.url_alloc = TRUE;
  1696. infof(data, "Switched from HTTP to HTTPS due to HSTS => %s",
  1697. data->state.url);
  1698. }
  1699. }
  1700. #endif
  1701. result = findprotocol(data, conn, data->state.up.scheme);
  1702. if(result)
  1703. return result;
  1704. /*
  1705. * username and password set with their own options override the credentials
  1706. * possibly set in the URL, but netrc does not.
  1707. */
  1708. if(!data->state.aptr.passwd || (data->state.creds_from != CREDS_OPTION)) {
  1709. uc = curl_url_get(uh, CURLUPART_PASSWORD, &data->state.up.password, 0);
  1710. if(!uc) {
  1711. char *decoded;
  1712. result = Curl_urldecode(data->state.up.password, 0, &decoded, NULL,
  1713. conn->handler->flags&PROTOPT_USERPWDCTRL ?
  1714. REJECT_ZERO : REJECT_CTRL);
  1715. if(result)
  1716. return result;
  1717. conn->passwd = decoded;
  1718. result = Curl_setstropt(&data->state.aptr.passwd, decoded);
  1719. if(result)
  1720. return result;
  1721. data->state.creds_from = CREDS_URL;
  1722. }
  1723. else if(uc != CURLUE_NO_PASSWORD)
  1724. return Curl_uc_to_curlcode(uc);
  1725. }
  1726. if(!data->state.aptr.user || (data->state.creds_from != CREDS_OPTION)) {
  1727. /* we do not use the URL API's URL decoder option here since it rejects
  1728. control codes and we want to allow them for some schemes in the user
  1729. and password fields */
  1730. uc = curl_url_get(uh, CURLUPART_USER, &data->state.up.user, 0);
  1731. if(!uc) {
  1732. char *decoded;
  1733. result = Curl_urldecode(data->state.up.user, 0, &decoded, NULL,
  1734. conn->handler->flags&PROTOPT_USERPWDCTRL ?
  1735. REJECT_ZERO : REJECT_CTRL);
  1736. if(result)
  1737. return result;
  1738. conn->user = decoded;
  1739. result = Curl_setstropt(&data->state.aptr.user, decoded);
  1740. data->state.creds_from = CREDS_URL;
  1741. }
  1742. else if(uc != CURLUE_NO_USER)
  1743. return Curl_uc_to_curlcode(uc);
  1744. if(result)
  1745. return result;
  1746. }
  1747. uc = curl_url_get(uh, CURLUPART_OPTIONS, &data->state.up.options,
  1748. CURLU_URLDECODE);
  1749. if(!uc) {
  1750. conn->options = strdup(data->state.up.options);
  1751. if(!conn->options)
  1752. return CURLE_OUT_OF_MEMORY;
  1753. }
  1754. else if(uc != CURLUE_NO_OPTIONS)
  1755. return Curl_uc_to_curlcode(uc);
  1756. uc = curl_url_get(uh, CURLUPART_PATH, &data->state.up.path,
  1757. CURLU_URLENCODE);
  1758. if(uc)
  1759. return Curl_uc_to_curlcode(uc);
  1760. uc = curl_url_get(uh, CURLUPART_PORT, &data->state.up.port,
  1761. CURLU_DEFAULT_PORT);
  1762. if(uc) {
  1763. if(!curl_strequal("file", data->state.up.scheme))
  1764. return CURLE_OUT_OF_MEMORY;
  1765. }
  1766. else {
  1767. curl_off_t port;
  1768. bool valid = TRUE;
  1769. if(data->set.use_port && data->state.allow_port)
  1770. port = data->set.use_port;
  1771. else {
  1772. const char *p = data->state.up.port;
  1773. if(curlx_str_number(&p, &port, 0xffff))
  1774. valid = FALSE;
  1775. }
  1776. if(valid)
  1777. conn->remote_port = (unsigned short)port;
  1778. }
  1779. (void)curl_url_get(uh, CURLUPART_QUERY, &data->state.up.query, 0);
  1780. #ifdef USE_IPV6
  1781. if(data->set.scope_id)
  1782. /* Override any scope that was set above. */
  1783. conn->scope_id = data->set.scope_id;
  1784. #endif
  1785. return CURLE_OK;
  1786. }
  1787. /*
  1788. * If we are doing a resumed transfer, we need to setup our stuff
  1789. * properly.
  1790. */
  1791. static CURLcode setup_range(struct Curl_easy *data)
  1792. {
  1793. struct UrlState *s = &data->state;
  1794. s->resume_from = data->set.set_resume_from;
  1795. if(s->resume_from || data->set.str[STRING_SET_RANGE]) {
  1796. if(s->rangestringalloc)
  1797. free(s->range);
  1798. if(s->resume_from)
  1799. s->range = curl_maprintf("%" FMT_OFF_T "-", s->resume_from);
  1800. else
  1801. s->range = strdup(data->set.str[STRING_SET_RANGE]);
  1802. if(!s->range)
  1803. return CURLE_OUT_OF_MEMORY;
  1804. s->rangestringalloc = TRUE;
  1805. /* tell ourselves to fetch this range */
  1806. s->use_range = TRUE; /* enable range download */
  1807. }
  1808. else
  1809. s->use_range = FALSE; /* disable range download */
  1810. return CURLE_OK;
  1811. }
  1812. /*
  1813. * setup_connection_internals() -
  1814. *
  1815. * Setup connection internals specific to the requested protocol in the
  1816. * Curl_easy. This is inited and setup before the connection is made but
  1817. * is about the particular protocol that is to be used.
  1818. *
  1819. * This MUST get called after proxy magic has been figured out.
  1820. */
  1821. static CURLcode setup_connection_internals(struct Curl_easy *data,
  1822. struct connectdata *conn)
  1823. {
  1824. const char *hostname;
  1825. int port;
  1826. CURLcode result;
  1827. if(conn->handler->setup_connection) {
  1828. result = conn->handler->setup_connection(data, conn);
  1829. if(result)
  1830. return result;
  1831. }
  1832. /* Now create the destination name */
  1833. #ifndef CURL_DISABLE_PROXY
  1834. if(conn->bits.httpproxy && !conn->bits.tunnel_proxy) {
  1835. hostname = conn->http_proxy.host.name;
  1836. port = conn->http_proxy.port;
  1837. }
  1838. else
  1839. #endif
  1840. {
  1841. port = conn->remote_port;
  1842. if(conn->bits.conn_to_host)
  1843. hostname = conn->conn_to_host.name;
  1844. else
  1845. hostname = conn->host.name;
  1846. }
  1847. #ifdef USE_IPV6
  1848. conn->destination = curl_maprintf("%u/%d/%s", conn->scope_id, port,
  1849. hostname);
  1850. #else
  1851. conn->destination = curl_maprintf("%d/%s", port, hostname);
  1852. #endif
  1853. if(!conn->destination)
  1854. return CURLE_OUT_OF_MEMORY;
  1855. Curl_strntolower(conn->destination, conn->destination,
  1856. strlen(conn->destination));
  1857. return CURLE_OK;
  1858. }
  1859. #ifndef CURL_DISABLE_PROXY
  1860. #ifndef CURL_DISABLE_HTTP
  1861. /****************************************************************
  1862. * Detect what (if any) proxy to use. Remember that this selects a host
  1863. * name and is not limited to HTTP proxies only.
  1864. * The returned pointer must be freed by the caller (unless NULL)
  1865. ****************************************************************/
  1866. static char *detect_proxy(struct Curl_easy *data,
  1867. struct connectdata *conn)
  1868. {
  1869. char *proxy = NULL;
  1870. /* If proxy was not specified, we check for default proxy environment
  1871. * variables, to enable i.e Lynx compliance:
  1872. *
  1873. * http_proxy=http://some.server.dom:port/
  1874. * https_proxy=http://some.server.dom:port/
  1875. * ftp_proxy=http://some.server.dom:port/
  1876. * no_proxy=domain1.dom,host.domain2.dom
  1877. * (a comma-separated list of hosts which should
  1878. * not be proxied, or an asterisk to override
  1879. * all proxy variables)
  1880. * all_proxy=http://some.server.dom:port/
  1881. * (seems to exist for the CERN www lib. Probably
  1882. * the first to check for.)
  1883. *
  1884. * For compatibility, the all-uppercase versions of these variables are
  1885. * checked if the lowercase versions do not exist.
  1886. */
  1887. char proxy_env[20];
  1888. const char *envp = proxy_env;
  1889. #ifdef CURL_DISABLE_VERBOSE_STRINGS
  1890. (void)data;
  1891. #endif
  1892. curl_msnprintf(proxy_env, sizeof(proxy_env), "%s_proxy",
  1893. conn->handler->scheme);
  1894. /* read the protocol proxy: */
  1895. proxy = curl_getenv(proxy_env);
  1896. /*
  1897. * We do not try the uppercase version of HTTP_PROXY because of
  1898. * security reasons:
  1899. *
  1900. * When curl is used in a webserver application
  1901. * environment (cgi or php), this environment variable can
  1902. * be controlled by the web server user by setting the
  1903. * http header 'Proxy:' to some value.
  1904. *
  1905. * This can cause 'internal' http/ftp requests to be
  1906. * arbitrarily redirected by any external attacker.
  1907. */
  1908. if(!proxy && !curl_strequal("http_proxy", proxy_env)) {
  1909. /* There was no lowercase variable, try the uppercase version: */
  1910. Curl_strntoupper(proxy_env, proxy_env, sizeof(proxy_env));
  1911. proxy = curl_getenv(proxy_env);
  1912. }
  1913. if(!proxy) {
  1914. #ifndef CURL_DISABLE_WEBSOCKETS
  1915. /* websocket proxy fallbacks */
  1916. if(curl_strequal("ws_proxy", proxy_env)) {
  1917. proxy = curl_getenv("http_proxy");
  1918. }
  1919. else if(curl_strequal("wss_proxy", proxy_env)) {
  1920. proxy = curl_getenv("https_proxy");
  1921. if(!proxy)
  1922. proxy = curl_getenv("HTTPS_PROXY");
  1923. }
  1924. if(!proxy) {
  1925. #endif
  1926. envp = "all_proxy";
  1927. proxy = curl_getenv(envp); /* default proxy to use */
  1928. if(!proxy) {
  1929. envp = "ALL_PROXY";
  1930. proxy = curl_getenv(envp);
  1931. }
  1932. #ifndef CURL_DISABLE_WEBSOCKETS
  1933. }
  1934. #endif
  1935. }
  1936. if(proxy)
  1937. infof(data, "Uses proxy env variable %s == '%s'", envp, proxy);
  1938. return proxy;
  1939. }
  1940. #endif /* CURL_DISABLE_HTTP */
  1941. /*
  1942. * If this is supposed to use a proxy, we need to figure out the proxy
  1943. * hostname, so that we can reuse an existing connection
  1944. * that may exist registered to the same proxy host.
  1945. */
  1946. static CURLcode parse_proxy(struct Curl_easy *data,
  1947. struct connectdata *conn, char *proxy,
  1948. long proxytype)
  1949. {
  1950. char *portptr = NULL;
  1951. int port = -1;
  1952. char *proxyuser = NULL;
  1953. char *proxypasswd = NULL;
  1954. char *host = NULL;
  1955. bool sockstype;
  1956. CURLUcode uc;
  1957. struct proxy_info *proxyinfo;
  1958. CURLU *uhp = curl_url();
  1959. CURLcode result = CURLE_OK;
  1960. char *scheme = NULL;
  1961. #ifdef USE_UNIX_SOCKETS
  1962. char *path = NULL;
  1963. bool is_unix_proxy = FALSE;
  1964. #endif
  1965. if(!uhp) {
  1966. result = CURLE_OUT_OF_MEMORY;
  1967. goto error;
  1968. }
  1969. /* When parsing the proxy, allowing non-supported schemes since we have
  1970. these made up ones for proxies. Guess scheme for URLs without it. */
  1971. uc = curl_url_set(uhp, CURLUPART_URL, proxy,
  1972. CURLU_NON_SUPPORT_SCHEME|CURLU_GUESS_SCHEME);
  1973. if(!uc) {
  1974. /* parsed okay as a URL */
  1975. uc = curl_url_get(uhp, CURLUPART_SCHEME, &scheme, 0);
  1976. if(uc) {
  1977. result = CURLE_OUT_OF_MEMORY;
  1978. goto error;
  1979. }
  1980. if(curl_strequal("https", scheme)) {
  1981. if(proxytype != CURLPROXY_HTTPS2)
  1982. proxytype = CURLPROXY_HTTPS;
  1983. else
  1984. proxytype = CURLPROXY_HTTPS2;
  1985. }
  1986. else if(curl_strequal("socks5h", scheme))
  1987. proxytype = CURLPROXY_SOCKS5_HOSTNAME;
  1988. else if(curl_strequal("socks5", scheme))
  1989. proxytype = CURLPROXY_SOCKS5;
  1990. else if(curl_strequal("socks4a", scheme))
  1991. proxytype = CURLPROXY_SOCKS4A;
  1992. else if(curl_strequal("socks4", scheme) ||
  1993. curl_strequal("socks", scheme))
  1994. proxytype = CURLPROXY_SOCKS4;
  1995. else if(curl_strequal("http", scheme))
  1996. ; /* leave it as HTTP or HTTP/1.0 */
  1997. else {
  1998. /* Any other xxx:// reject! */
  1999. failf(data, "Unsupported proxy scheme for \'%s\'", proxy);
  2000. result = CURLE_COULDNT_CONNECT;
  2001. goto error;
  2002. }
  2003. }
  2004. else {
  2005. failf(data, "Unsupported proxy syntax in \'%s\': %s", proxy,
  2006. curl_url_strerror(uc));
  2007. result = CURLE_COULDNT_RESOLVE_PROXY;
  2008. goto error;
  2009. }
  2010. #ifdef USE_SSL
  2011. if(!Curl_ssl_supports(data, SSLSUPP_HTTPS_PROXY))
  2012. #endif
  2013. if(IS_HTTPS_PROXY(proxytype)) {
  2014. failf(data, "Unsupported proxy \'%s\', libcurl is built without the "
  2015. "HTTPS-proxy support.", proxy);
  2016. result = CURLE_NOT_BUILT_IN;
  2017. goto error;
  2018. }
  2019. sockstype =
  2020. proxytype == CURLPROXY_SOCKS5_HOSTNAME ||
  2021. proxytype == CURLPROXY_SOCKS5 ||
  2022. proxytype == CURLPROXY_SOCKS4A ||
  2023. proxytype == CURLPROXY_SOCKS4;
  2024. proxyinfo = sockstype ? &conn->socks_proxy : &conn->http_proxy;
  2025. proxyinfo->proxytype = (unsigned char)proxytype;
  2026. /* Is there a username and password given in this proxy url? */
  2027. uc = curl_url_get(uhp, CURLUPART_USER, &proxyuser, CURLU_URLDECODE);
  2028. if(uc && (uc != CURLUE_NO_USER))
  2029. goto error;
  2030. uc = curl_url_get(uhp, CURLUPART_PASSWORD, &proxypasswd, CURLU_URLDECODE);
  2031. if(uc && (uc != CURLUE_NO_PASSWORD))
  2032. goto error;
  2033. if(proxyuser || proxypasswd) {
  2034. free(proxyinfo->user);
  2035. proxyinfo->user = proxyuser;
  2036. result = Curl_setstropt(&data->state.aptr.proxyuser, proxyuser);
  2037. proxyuser = NULL;
  2038. if(result)
  2039. goto error;
  2040. Curl_safefree(proxyinfo->passwd);
  2041. if(!proxypasswd) {
  2042. proxypasswd = strdup("");
  2043. if(!proxypasswd) {
  2044. result = CURLE_OUT_OF_MEMORY;
  2045. goto error;
  2046. }
  2047. }
  2048. proxyinfo->passwd = proxypasswd;
  2049. result = Curl_setstropt(&data->state.aptr.proxypasswd, proxypasswd);
  2050. proxypasswd = NULL;
  2051. if(result)
  2052. goto error;
  2053. conn->bits.proxy_user_passwd = TRUE; /* enable it */
  2054. }
  2055. (void)curl_url_get(uhp, CURLUPART_PORT, &portptr, 0);
  2056. if(portptr) {
  2057. curl_off_t num;
  2058. const char *p = portptr;
  2059. if(!curlx_str_number(&p, &num, 0xffff))
  2060. port = (int)num;
  2061. free(portptr);
  2062. }
  2063. else {
  2064. if(data->set.proxyport)
  2065. /* None given in the proxy string, then get the default one if it is
  2066. given */
  2067. port = (int)data->set.proxyport;
  2068. else {
  2069. if(IS_HTTPS_PROXY(proxytype))
  2070. port = CURL_DEFAULT_HTTPS_PROXY_PORT;
  2071. else
  2072. port = CURL_DEFAULT_PROXY_PORT;
  2073. }
  2074. }
  2075. if(port >= 0)
  2076. proxyinfo->port = port;
  2077. /* now, clone the proxy hostname */
  2078. uc = curl_url_get(uhp, CURLUPART_HOST, &host, CURLU_URLDECODE);
  2079. if(uc) {
  2080. result = CURLE_OUT_OF_MEMORY;
  2081. goto error;
  2082. }
  2083. #ifdef USE_UNIX_SOCKETS
  2084. if(sockstype && curl_strequal(UNIX_SOCKET_PREFIX, host)) {
  2085. uc = curl_url_get(uhp, CURLUPART_PATH, &path, CURLU_URLDECODE);
  2086. if(uc) {
  2087. result = CURLE_OUT_OF_MEMORY;
  2088. goto error;
  2089. }
  2090. /* path will be "/", if no path was found */
  2091. if(strcmp("/", path)) {
  2092. is_unix_proxy = TRUE;
  2093. free(host);
  2094. host = curl_maprintf(UNIX_SOCKET_PREFIX"%s", path);
  2095. if(!host) {
  2096. result = CURLE_OUT_OF_MEMORY;
  2097. goto error;
  2098. }
  2099. free(proxyinfo->host.rawalloc);
  2100. proxyinfo->host.rawalloc = host;
  2101. proxyinfo->host.name = host;
  2102. host = NULL;
  2103. }
  2104. }
  2105. if(!is_unix_proxy) {
  2106. #endif
  2107. free(proxyinfo->host.rawalloc);
  2108. proxyinfo->host.rawalloc = host;
  2109. if(host[0] == '[') {
  2110. /* this is a numerical IPv6, strip off the brackets */
  2111. size_t len = strlen(host);
  2112. host[len-1] = 0; /* clear the trailing bracket */
  2113. host++;
  2114. zonefrom_url(uhp, data, conn);
  2115. }
  2116. proxyinfo->host.name = host;
  2117. host = NULL;
  2118. #ifdef USE_UNIX_SOCKETS
  2119. }
  2120. #endif
  2121. error:
  2122. free(proxyuser);
  2123. free(proxypasswd);
  2124. free(host);
  2125. free(scheme);
  2126. #ifdef USE_UNIX_SOCKETS
  2127. free(path);
  2128. #endif
  2129. curl_url_cleanup(uhp);
  2130. return result;
  2131. }
  2132. /*
  2133. * Extract the user and password from the authentication string
  2134. */
  2135. static CURLcode parse_proxy_auth(struct Curl_easy *data,
  2136. struct connectdata *conn)
  2137. {
  2138. const char *proxyuser = data->state.aptr.proxyuser ?
  2139. data->state.aptr.proxyuser : "";
  2140. const char *proxypasswd = data->state.aptr.proxypasswd ?
  2141. data->state.aptr.proxypasswd : "";
  2142. CURLcode result = CURLE_OUT_OF_MEMORY;
  2143. conn->http_proxy.user = strdup(proxyuser);
  2144. if(conn->http_proxy.user) {
  2145. conn->http_proxy.passwd = strdup(proxypasswd);
  2146. if(conn->http_proxy.passwd)
  2147. result = CURLE_OK;
  2148. else
  2149. Curl_safefree(conn->http_proxy.user);
  2150. }
  2151. return result;
  2152. }
  2153. /* create_conn helper to parse and init proxy values. to be called after Unix
  2154. socket init but before any proxy vars are evaluated. */
  2155. static CURLcode create_conn_helper_init_proxy(struct Curl_easy *data,
  2156. struct connectdata *conn)
  2157. {
  2158. char *proxy = NULL;
  2159. char *socksproxy = NULL;
  2160. char *no_proxy = NULL;
  2161. CURLcode result = CURLE_OK;
  2162. /*************************************************************
  2163. * Extract the user and password from the authentication string
  2164. *************************************************************/
  2165. if(conn->bits.proxy_user_passwd) {
  2166. result = parse_proxy_auth(data, conn);
  2167. if(result)
  2168. goto out;
  2169. }
  2170. /*************************************************************
  2171. * Detect what (if any) proxy to use
  2172. *************************************************************/
  2173. if(data->set.str[STRING_PROXY]) {
  2174. proxy = strdup(data->set.str[STRING_PROXY]);
  2175. /* if global proxy is set, this is it */
  2176. if(!proxy) {
  2177. failf(data, "memory shortage");
  2178. result = CURLE_OUT_OF_MEMORY;
  2179. goto out;
  2180. }
  2181. }
  2182. if(data->set.str[STRING_PRE_PROXY]) {
  2183. socksproxy = strdup(data->set.str[STRING_PRE_PROXY]);
  2184. /* if global socks proxy is set, this is it */
  2185. if(!socksproxy) {
  2186. failf(data, "memory shortage");
  2187. result = CURLE_OUT_OF_MEMORY;
  2188. goto out;
  2189. }
  2190. }
  2191. if(!data->set.str[STRING_NOPROXY]) {
  2192. const char *p = "no_proxy";
  2193. no_proxy = curl_getenv(p);
  2194. if(!no_proxy) {
  2195. p = "NO_PROXY";
  2196. no_proxy = curl_getenv(p);
  2197. }
  2198. if(no_proxy) {
  2199. infof(data, "Uses proxy env variable %s == '%s'", p, no_proxy);
  2200. }
  2201. }
  2202. if(Curl_check_noproxy(conn->host.name, data->set.str[STRING_NOPROXY] ?
  2203. data->set.str[STRING_NOPROXY] : no_proxy)) {
  2204. Curl_safefree(proxy);
  2205. Curl_safefree(socksproxy);
  2206. }
  2207. #ifndef CURL_DISABLE_HTTP
  2208. else if(!proxy && !socksproxy)
  2209. /* if the host is not in the noproxy list, detect proxy. */
  2210. proxy = detect_proxy(data, conn);
  2211. #endif /* CURL_DISABLE_HTTP */
  2212. Curl_safefree(no_proxy);
  2213. #ifdef USE_UNIX_SOCKETS
  2214. /* For the time being do not mix proxy and Unix domain sockets. See #1274 */
  2215. if(proxy && conn->unix_domain_socket) {
  2216. free(proxy);
  2217. proxy = NULL;
  2218. }
  2219. #endif
  2220. if(proxy && (!*proxy || (conn->handler->flags & PROTOPT_NONETWORK))) {
  2221. free(proxy); /* Do not bother with an empty proxy string or if the
  2222. protocol does not work with network */
  2223. proxy = NULL;
  2224. }
  2225. if(socksproxy && (!*socksproxy ||
  2226. (conn->handler->flags & PROTOPT_NONETWORK))) {
  2227. free(socksproxy); /* Do not bother with an empty socks proxy string or if
  2228. the protocol does not work with network */
  2229. socksproxy = NULL;
  2230. }
  2231. /***********************************************************************
  2232. * If this is supposed to use a proxy, we need to figure out the proxy host
  2233. * name, proxy type and port number, so that we can reuse an existing
  2234. * connection that may exist registered to the same proxy host.
  2235. ***********************************************************************/
  2236. if(proxy || socksproxy) {
  2237. long ptype = conn->http_proxy.proxytype;
  2238. if(proxy) {
  2239. result = parse_proxy(data, conn, proxy, ptype);
  2240. Curl_safefree(proxy); /* parse_proxy copies the proxy string */
  2241. if(result)
  2242. goto out;
  2243. }
  2244. if(socksproxy) {
  2245. result = parse_proxy(data, conn, socksproxy, ptype);
  2246. /* parse_proxy copies the socks proxy string */
  2247. Curl_safefree(socksproxy);
  2248. if(result)
  2249. goto out;
  2250. }
  2251. if(conn->http_proxy.host.rawalloc) {
  2252. #ifdef CURL_DISABLE_HTTP
  2253. /* asking for an HTTP proxy is a bit funny when HTTP is disabled... */
  2254. result = CURLE_UNSUPPORTED_PROTOCOL;
  2255. goto out;
  2256. #else
  2257. /* force this connection's protocol to become HTTP if compatible */
  2258. if(!(conn->handler->protocol & PROTO_FAMILY_HTTP)) {
  2259. if((conn->handler->flags & PROTOPT_PROXY_AS_HTTP) &&
  2260. !conn->bits.tunnel_proxy)
  2261. conn->handler = &Curl_handler_http;
  2262. else
  2263. /* if not converting to HTTP over the proxy, enforce tunneling */
  2264. conn->bits.tunnel_proxy = TRUE;
  2265. }
  2266. conn->bits.httpproxy = TRUE;
  2267. #endif
  2268. }
  2269. else {
  2270. conn->bits.httpproxy = FALSE; /* not an HTTP proxy */
  2271. conn->bits.tunnel_proxy = FALSE; /* no tunneling if not HTTP */
  2272. }
  2273. if(conn->socks_proxy.host.rawalloc) {
  2274. if(!conn->http_proxy.host.rawalloc) {
  2275. /* once a socks proxy */
  2276. if(!conn->socks_proxy.user) {
  2277. conn->socks_proxy.user = conn->http_proxy.user;
  2278. conn->http_proxy.user = NULL;
  2279. free(conn->socks_proxy.passwd);
  2280. conn->socks_proxy.passwd = conn->http_proxy.passwd;
  2281. conn->http_proxy.passwd = NULL;
  2282. }
  2283. }
  2284. conn->bits.socksproxy = TRUE;
  2285. }
  2286. else
  2287. conn->bits.socksproxy = FALSE; /* not a socks proxy */
  2288. }
  2289. else {
  2290. conn->bits.socksproxy = FALSE;
  2291. conn->bits.httpproxy = FALSE;
  2292. }
  2293. conn->bits.proxy = conn->bits.httpproxy || conn->bits.socksproxy;
  2294. if(!conn->bits.proxy) {
  2295. /* we are not using the proxy after all... */
  2296. conn->bits.proxy = FALSE;
  2297. conn->bits.httpproxy = FALSE;
  2298. conn->bits.socksproxy = FALSE;
  2299. conn->bits.proxy_user_passwd = FALSE;
  2300. conn->bits.tunnel_proxy = FALSE;
  2301. /* CURLPROXY_HTTPS does not have its own flag in conn->bits, yet we need
  2302. to signal that CURLPROXY_HTTPS is not used for this connection */
  2303. conn->http_proxy.proxytype = CURLPROXY_HTTP;
  2304. }
  2305. out:
  2306. free(socksproxy);
  2307. free(proxy);
  2308. return result;
  2309. }
  2310. #endif /* CURL_DISABLE_PROXY */
  2311. /*
  2312. * Curl_parse_login_details()
  2313. *
  2314. * This is used to parse a login string for username, password and options in
  2315. * the following formats:
  2316. *
  2317. * user
  2318. * user:password
  2319. * user:password;options
  2320. * user;options
  2321. * user;options:password
  2322. * :password
  2323. * :password;options
  2324. * ;options
  2325. * ;options:password
  2326. *
  2327. * Parameters:
  2328. *
  2329. * login [in] - login string.
  2330. * len [in] - length of the login string.
  2331. * userp [in/out] - address where a pointer to newly allocated memory
  2332. * holding the user will be stored upon completion.
  2333. * passwdp [in/out] - address where a pointer to newly allocated memory
  2334. * holding the password will be stored upon completion.
  2335. * optionsp [in/out] - OPTIONAL address where a pointer to newly allocated
  2336. * memory holding the options will be stored upon
  2337. * completion.
  2338. *
  2339. * Returns CURLE_OK on success.
  2340. */
  2341. CURLcode Curl_parse_login_details(const char *login, const size_t len,
  2342. char **userp, char **passwdp,
  2343. char **optionsp)
  2344. {
  2345. char *ubuf = NULL;
  2346. char *pbuf = NULL;
  2347. const char *psep = NULL;
  2348. const char *osep = NULL;
  2349. size_t ulen;
  2350. size_t plen;
  2351. size_t olen;
  2352. DEBUGASSERT(userp);
  2353. DEBUGASSERT(passwdp);
  2354. /* Attempt to find the password separator */
  2355. psep = memchr(login, ':', len);
  2356. /* Attempt to find the options separator */
  2357. if(optionsp)
  2358. osep = memchr(login, ';', len);
  2359. /* Calculate the portion lengths */
  2360. ulen = (psep ?
  2361. (size_t)(osep && psep > osep ? osep - login : psep - login) :
  2362. (osep ? (size_t)(osep - login) : len));
  2363. plen = (psep ?
  2364. (osep && osep > psep ? (size_t)(osep - psep) :
  2365. (size_t)(login + len - psep)) - 1 : 0);
  2366. olen = (osep ?
  2367. (psep && psep > osep ? (size_t)(psep - osep) :
  2368. (size_t)(login + len - osep)) - 1 : 0);
  2369. /* Clone the user portion buffer, which can be zero length */
  2370. ubuf = Curl_memdup0(login, ulen);
  2371. if(!ubuf)
  2372. goto error;
  2373. /* Clone the password portion buffer */
  2374. if(psep) {
  2375. pbuf = Curl_memdup0(&psep[1], plen);
  2376. if(!pbuf)
  2377. goto error;
  2378. }
  2379. /* Allocate the options portion buffer */
  2380. if(optionsp) {
  2381. char *obuf = NULL;
  2382. if(olen) {
  2383. obuf = Curl_memdup0(&osep[1], olen);
  2384. if(!obuf)
  2385. goto error;
  2386. }
  2387. *optionsp = obuf;
  2388. }
  2389. *userp = ubuf;
  2390. *passwdp = pbuf;
  2391. return CURLE_OK;
  2392. error:
  2393. free(ubuf);
  2394. free(pbuf);
  2395. return CURLE_OUT_OF_MEMORY;
  2396. }
  2397. /*************************************************************
  2398. * Figure out the remote port number and fix it in the URL
  2399. *
  2400. * No matter if we use a proxy or not, we have to figure out the remote
  2401. * port number of various reasons.
  2402. *
  2403. * The port number embedded in the URL is replaced, if necessary.
  2404. *************************************************************/
  2405. static CURLcode parse_remote_port(struct Curl_easy *data,
  2406. struct connectdata *conn)
  2407. {
  2408. if(data->set.use_port && data->state.allow_port) {
  2409. /* if set, we use this instead of the port possibly given in the URL */
  2410. char portbuf[16];
  2411. CURLUcode uc;
  2412. conn->remote_port = data->set.use_port;
  2413. curl_msnprintf(portbuf, sizeof(portbuf), "%d", conn->remote_port);
  2414. uc = curl_url_set(data->state.uh, CURLUPART_PORT, portbuf, 0);
  2415. if(uc)
  2416. return CURLE_OUT_OF_MEMORY;
  2417. }
  2418. return CURLE_OK;
  2419. }
  2420. #ifndef CURL_DISABLE_NETRC
  2421. static bool str_has_ctrl(const char *input)
  2422. {
  2423. if(input) {
  2424. const unsigned char *str = (const unsigned char *)input;
  2425. while(*str) {
  2426. if(*str < 0x20)
  2427. return TRUE;
  2428. str++;
  2429. }
  2430. }
  2431. return FALSE;
  2432. }
  2433. #endif
  2434. /*
  2435. * Override the login details from the URL with that in the CURLOPT_USERPWD
  2436. * option or a .netrc file, if applicable.
  2437. */
  2438. static CURLcode override_login(struct Curl_easy *data,
  2439. struct connectdata *conn)
  2440. {
  2441. CURLUcode uc;
  2442. char **userp = &conn->user;
  2443. char **passwdp = &conn->passwd;
  2444. char **optionsp = &conn->options;
  2445. if(data->set.str[STRING_OPTIONS]) {
  2446. free(*optionsp);
  2447. *optionsp = strdup(data->set.str[STRING_OPTIONS]);
  2448. if(!*optionsp)
  2449. return CURLE_OUT_OF_MEMORY;
  2450. }
  2451. #ifndef CURL_DISABLE_NETRC
  2452. if(data->set.use_netrc == CURL_NETRC_REQUIRED) {
  2453. Curl_safefree(*userp);
  2454. Curl_safefree(*passwdp);
  2455. }
  2456. conn->bits.netrc = FALSE;
  2457. if(data->set.use_netrc && !data->set.str[STRING_USERNAME]) {
  2458. bool url_provided = FALSE;
  2459. if(data->state.aptr.user &&
  2460. (data->state.creds_from != CREDS_NETRC)) {
  2461. /* there was a username with a length in the URL. Use the URL decoded
  2462. version */
  2463. userp = &data->state.aptr.user;
  2464. url_provided = TRUE;
  2465. }
  2466. if(!*passwdp) {
  2467. NETRCcode ret = Curl_parsenetrc(&data->state.netrc, conn->host.name,
  2468. userp, passwdp,
  2469. data->set.str[STRING_NETRC_FILE]);
  2470. if(ret && ((ret == NETRC_NO_MATCH) ||
  2471. (data->set.use_netrc == CURL_NETRC_OPTIONAL))) {
  2472. infof(data, "Couldn't find host %s in the %s file; using defaults",
  2473. conn->host.name,
  2474. (data->set.str[STRING_NETRC_FILE] ?
  2475. data->set.str[STRING_NETRC_FILE] : ".netrc"));
  2476. }
  2477. else if(ret) {
  2478. const char *m = Curl_netrc_strerror(ret);
  2479. failf(data, ".netrc error: %s", m);
  2480. return CURLE_READ_ERROR;
  2481. }
  2482. else {
  2483. if(!(conn->handler->flags&PROTOPT_USERPWDCTRL)) {
  2484. /* if the protocol can't handle control codes in credentials, make
  2485. sure there are none */
  2486. if(str_has_ctrl(*userp) || str_has_ctrl(*passwdp)) {
  2487. failf(data, "control code detected in .netrc credentials");
  2488. return CURLE_READ_ERROR;
  2489. }
  2490. }
  2491. /* set bits.netrc TRUE to remember that we got the name from a .netrc
  2492. file, so that it is safe to use even if we followed a Location: to a
  2493. different host or similar. */
  2494. conn->bits.netrc = TRUE;
  2495. }
  2496. }
  2497. if(url_provided) {
  2498. free(conn->user);
  2499. conn->user = strdup(*userp);
  2500. if(!conn->user)
  2501. return CURLE_OUT_OF_MEMORY;
  2502. }
  2503. /* no user was set but a password, set a blank user */
  2504. if(!*userp && *passwdp) {
  2505. *userp = strdup("");
  2506. if(!*userp)
  2507. return CURLE_OUT_OF_MEMORY;
  2508. }
  2509. }
  2510. #endif
  2511. /* for updated strings, we update them in the URL */
  2512. if(*userp) {
  2513. CURLcode result;
  2514. if(data->state.aptr.user != *userp) {
  2515. /* nothing to do then */
  2516. result = Curl_setstropt(&data->state.aptr.user, *userp);
  2517. if(result)
  2518. return result;
  2519. data->state.creds_from = CREDS_NETRC;
  2520. }
  2521. }
  2522. if(data->state.aptr.user) {
  2523. uc = curl_url_set(data->state.uh, CURLUPART_USER, data->state.aptr.user,
  2524. CURLU_URLENCODE);
  2525. if(uc)
  2526. return Curl_uc_to_curlcode(uc);
  2527. if(!*userp) {
  2528. *userp = strdup(data->state.aptr.user);
  2529. if(!*userp)
  2530. return CURLE_OUT_OF_MEMORY;
  2531. }
  2532. }
  2533. if(*passwdp) {
  2534. CURLcode result = Curl_setstropt(&data->state.aptr.passwd, *passwdp);
  2535. if(result)
  2536. return result;
  2537. data->state.creds_from = CREDS_NETRC;
  2538. }
  2539. if(data->state.aptr.passwd) {
  2540. uc = curl_url_set(data->state.uh, CURLUPART_PASSWORD,
  2541. data->state.aptr.passwd, CURLU_URLENCODE);
  2542. if(uc)
  2543. return Curl_uc_to_curlcode(uc);
  2544. if(!*passwdp) {
  2545. *passwdp = strdup(data->state.aptr.passwd);
  2546. if(!*passwdp)
  2547. return CURLE_OUT_OF_MEMORY;
  2548. }
  2549. }
  2550. return CURLE_OK;
  2551. }
  2552. /*
  2553. * Set the login details so they are available in the connection
  2554. */
  2555. static CURLcode set_login(struct Curl_easy *data,
  2556. struct connectdata *conn)
  2557. {
  2558. CURLcode result = CURLE_OK;
  2559. const char *setuser = CURL_DEFAULT_USER;
  2560. const char *setpasswd = CURL_DEFAULT_PASSWORD;
  2561. /* If our protocol needs a password and we have none, use the defaults */
  2562. if((conn->handler->flags & PROTOPT_NEEDSPWD) && !data->state.aptr.user)
  2563. ;
  2564. else {
  2565. setuser = "";
  2566. setpasswd = "";
  2567. }
  2568. /* Store the default user */
  2569. if(!conn->user) {
  2570. conn->user = strdup(setuser);
  2571. if(!conn->user)
  2572. return CURLE_OUT_OF_MEMORY;
  2573. }
  2574. /* Store the default password */
  2575. if(!conn->passwd) {
  2576. conn->passwd = strdup(setpasswd);
  2577. if(!conn->passwd)
  2578. result = CURLE_OUT_OF_MEMORY;
  2579. }
  2580. return result;
  2581. }
  2582. /*
  2583. * Parses a "host:port" string to connect to.
  2584. * The hostname and the port may be empty; in this case, NULL is returned for
  2585. * the hostname and -1 for the port.
  2586. */
  2587. static CURLcode parse_connect_to_host_port(struct Curl_easy *data,
  2588. const char *host,
  2589. char **hostname_result,
  2590. int *port_result)
  2591. {
  2592. char *host_dup;
  2593. char *hostptr;
  2594. char *host_portno;
  2595. char *portptr;
  2596. int port = -1;
  2597. CURLcode result = CURLE_OK;
  2598. #ifdef CURL_DISABLE_VERBOSE_STRINGS
  2599. (void)data;
  2600. #endif
  2601. *hostname_result = NULL;
  2602. *port_result = -1;
  2603. if(!host || !*host)
  2604. return CURLE_OK;
  2605. host_dup = strdup(host);
  2606. if(!host_dup)
  2607. return CURLE_OUT_OF_MEMORY;
  2608. hostptr = host_dup;
  2609. /* start scanning for port number at this point */
  2610. portptr = hostptr;
  2611. /* detect and extract RFC6874-style IPv6-addresses */
  2612. if(*hostptr == '[') {
  2613. #ifdef USE_IPV6
  2614. char *ptr = ++hostptr; /* advance beyond the initial bracket */
  2615. while(*ptr && (ISXDIGIT(*ptr) || (*ptr == ':') || (*ptr == '.')))
  2616. ptr++;
  2617. if(*ptr == '%') {
  2618. /* There might be a zone identifier */
  2619. if(strncmp("%25", ptr, 3))
  2620. infof(data, "Please URL encode %% as %%25, see RFC 6874.");
  2621. ptr++;
  2622. /* Allow unreserved characters as defined in RFC 3986 */
  2623. while(*ptr && (ISALPHA(*ptr) || ISXDIGIT(*ptr) || (*ptr == '-') ||
  2624. (*ptr == '.') || (*ptr == '_') || (*ptr == '~')))
  2625. ptr++;
  2626. }
  2627. if(*ptr == ']')
  2628. /* yeps, it ended nicely with a bracket as well */
  2629. *ptr++ = '\0';
  2630. else
  2631. infof(data, "Invalid IPv6 address format");
  2632. portptr = ptr;
  2633. /* Note that if this did not end with a bracket, we still advanced the
  2634. * hostptr first, but I cannot see anything wrong with that as no host
  2635. * name nor a numeric can legally start with a bracket.
  2636. */
  2637. #else
  2638. failf(data, "Use of IPv6 in *_CONNECT_TO without IPv6 support built-in");
  2639. result = CURLE_NOT_BUILT_IN;
  2640. goto error;
  2641. #endif
  2642. }
  2643. /* Get port number off server.com:1080 */
  2644. host_portno = strchr(portptr, ':');
  2645. if(host_portno) {
  2646. *host_portno = '\0'; /* cut off number from hostname */
  2647. host_portno++;
  2648. if(*host_portno) {
  2649. curl_off_t portparse;
  2650. const char *p = host_portno;
  2651. if(curlx_str_number(&p, &portparse, 0xffff)) {
  2652. failf(data, "No valid port number in connect to host string (%s)",
  2653. host_portno);
  2654. result = CURLE_SETOPT_OPTION_SYNTAX;
  2655. goto error;
  2656. }
  2657. port = (int)portparse; /* we know it will fit */
  2658. }
  2659. }
  2660. /* now, clone the cleaned hostname */
  2661. DEBUGASSERT(hostptr);
  2662. *hostname_result = strdup(hostptr);
  2663. if(!*hostname_result) {
  2664. result = CURLE_OUT_OF_MEMORY;
  2665. goto error;
  2666. }
  2667. *port_result = port;
  2668. error:
  2669. free(host_dup);
  2670. return result;
  2671. }
  2672. /*
  2673. * Parses one "connect to" string in the form:
  2674. * "HOST:PORT:CONNECT-TO-HOST:CONNECT-TO-PORT".
  2675. */
  2676. static CURLcode parse_connect_to_string(struct Curl_easy *data,
  2677. struct connectdata *conn,
  2678. const char *conn_to_host,
  2679. char **host_result,
  2680. int *port_result)
  2681. {
  2682. CURLcode result = CURLE_OK;
  2683. const char *ptr = conn_to_host;
  2684. bool host_match = FALSE;
  2685. bool port_match = FALSE;
  2686. *host_result = NULL;
  2687. *port_result = -1;
  2688. if(*ptr == ':') {
  2689. /* an empty hostname always matches */
  2690. host_match = TRUE;
  2691. ptr++;
  2692. }
  2693. else {
  2694. /* check whether the URL's hostname matches */
  2695. size_t hostname_to_match_len;
  2696. char *hostname_to_match = curl_maprintf("%s%s%s",
  2697. conn->bits.ipv6_ip ? "[" : "",
  2698. conn->host.name,
  2699. conn->bits.ipv6_ip ? "]" : "");
  2700. if(!hostname_to_match)
  2701. return CURLE_OUT_OF_MEMORY;
  2702. hostname_to_match_len = strlen(hostname_to_match);
  2703. host_match = curl_strnequal(ptr, hostname_to_match,
  2704. hostname_to_match_len);
  2705. free(hostname_to_match);
  2706. ptr += hostname_to_match_len;
  2707. host_match = host_match && *ptr == ':';
  2708. ptr++;
  2709. }
  2710. if(host_match) {
  2711. if(*ptr == ':') {
  2712. /* an empty port always matches */
  2713. port_match = TRUE;
  2714. ptr++;
  2715. }
  2716. else {
  2717. /* check whether the URL's port matches */
  2718. char *ptr_next = strchr(ptr, ':');
  2719. if(ptr_next) {
  2720. curl_off_t port_to_match;
  2721. if(!curlx_str_number(&ptr, &port_to_match, 0xffff) &&
  2722. (port_to_match == (curl_off_t)conn->remote_port))
  2723. port_match = TRUE;
  2724. ptr = ptr_next + 1;
  2725. }
  2726. }
  2727. }
  2728. if(host_match && port_match) {
  2729. /* parse the hostname and port to connect to */
  2730. result = parse_connect_to_host_port(data, ptr, host_result, port_result);
  2731. }
  2732. return result;
  2733. }
  2734. /*
  2735. * Processes all strings in the "connect to" slist, and uses the "connect
  2736. * to host" and "connect to port" of the first string that matches.
  2737. */
  2738. static CURLcode parse_connect_to_slist(struct Curl_easy *data,
  2739. struct connectdata *conn,
  2740. struct curl_slist *conn_to_host)
  2741. {
  2742. CURLcode result = CURLE_OK;
  2743. char *host = NULL;
  2744. int port = -1;
  2745. while(conn_to_host && !host && port == -1) {
  2746. result = parse_connect_to_string(data, conn, conn_to_host->data,
  2747. &host, &port);
  2748. if(result)
  2749. return result;
  2750. if(host && *host) {
  2751. conn->conn_to_host.rawalloc = host;
  2752. conn->conn_to_host.name = host;
  2753. conn->bits.conn_to_host = TRUE;
  2754. infof(data, "Connecting to hostname: %s", host);
  2755. }
  2756. else {
  2757. /* no "connect to host" */
  2758. conn->bits.conn_to_host = FALSE;
  2759. Curl_safefree(host);
  2760. }
  2761. if(port >= 0) {
  2762. conn->conn_to_port = port;
  2763. conn->bits.conn_to_port = TRUE;
  2764. infof(data, "Connecting to port: %d", port);
  2765. }
  2766. else {
  2767. /* no "connect to port" */
  2768. conn->bits.conn_to_port = FALSE;
  2769. port = -1;
  2770. }
  2771. conn_to_host = conn_to_host->next;
  2772. }
  2773. #ifndef CURL_DISABLE_ALTSVC
  2774. if(data->asi && !host && (port == -1) &&
  2775. ((conn->handler->protocol == CURLPROTO_HTTPS) ||
  2776. #ifdef DEBUGBUILD
  2777. /* allow debug builds to circumvent the HTTPS restriction */
  2778. getenv("CURL_ALTSVC_HTTP")
  2779. #else
  2780. 0
  2781. #endif
  2782. )) {
  2783. /* no connect_to match, try alt-svc! */
  2784. enum alpnid srcalpnid = ALPN_none;
  2785. bool hit = FALSE;
  2786. struct altsvc *as = NULL;
  2787. int allowed_alpns = ALPN_none;
  2788. struct http_negotiation *neg = &data->state.http_neg;
  2789. DEBUGF(infof(data, "Alt-svc check wanted=%x, allowed=%x",
  2790. neg->wanted, neg->allowed));
  2791. #ifdef USE_HTTP3
  2792. if(neg->allowed & CURL_HTTP_V3x)
  2793. allowed_alpns |= ALPN_h3;
  2794. #endif
  2795. #ifdef USE_HTTP2
  2796. if(neg->allowed & CURL_HTTP_V2x)
  2797. allowed_alpns |= ALPN_h2;
  2798. #endif
  2799. if(neg->allowed & CURL_HTTP_V1x)
  2800. allowed_alpns |= ALPN_h1;
  2801. allowed_alpns &= (int)data->asi->flags;
  2802. host = conn->host.rawalloc;
  2803. DEBUGF(infof(data, "check Alt-Svc for host %s", host));
  2804. #ifdef USE_HTTP3
  2805. if(!hit && (neg->wanted & CURL_HTTP_V3x)) {
  2806. srcalpnid = ALPN_h3;
  2807. hit = Curl_altsvc_lookup(data->asi,
  2808. ALPN_h3, host, conn->remote_port, /* from */
  2809. &as /* to */,
  2810. allowed_alpns);
  2811. }
  2812. #endif
  2813. #ifdef USE_HTTP2
  2814. if(!hit && (neg->wanted & CURL_HTTP_V2x) &&
  2815. !neg->h2_prior_knowledge) {
  2816. srcalpnid = ALPN_h2;
  2817. hit = Curl_altsvc_lookup(data->asi,
  2818. ALPN_h2, host, conn->remote_port, /* from */
  2819. &as /* to */,
  2820. allowed_alpns);
  2821. }
  2822. #endif
  2823. if(!hit && (neg->wanted & CURL_HTTP_V1x) &&
  2824. !neg->only_10) {
  2825. srcalpnid = ALPN_h1;
  2826. hit = Curl_altsvc_lookup(data->asi,
  2827. ALPN_h1, host, conn->remote_port, /* from */
  2828. &as /* to */,
  2829. allowed_alpns);
  2830. }
  2831. if(hit) {
  2832. char *hostd = strdup((char *)as->dst.host);
  2833. if(!hostd)
  2834. return CURLE_OUT_OF_MEMORY;
  2835. conn->conn_to_host.rawalloc = hostd;
  2836. conn->conn_to_host.name = hostd;
  2837. conn->bits.conn_to_host = TRUE;
  2838. conn->conn_to_port = as->dst.port;
  2839. conn->bits.conn_to_port = TRUE;
  2840. conn->bits.altused = TRUE;
  2841. infof(data, "Alt-svc connecting from [%s]%s:%d to [%s]%s:%d",
  2842. Curl_alpnid2str(srcalpnid), host, conn->remote_port,
  2843. Curl_alpnid2str(as->dst.alpnid), hostd, as->dst.port);
  2844. if(srcalpnid != as->dst.alpnid) {
  2845. /* protocol version switch */
  2846. switch(as->dst.alpnid) {
  2847. case ALPN_h1:
  2848. neg->wanted = neg->allowed = CURL_HTTP_V1x;
  2849. neg->only_10 = FALSE;
  2850. break;
  2851. case ALPN_h2:
  2852. neg->wanted = neg->allowed = CURL_HTTP_V2x;
  2853. break;
  2854. case ALPN_h3:
  2855. conn->transport_wanted = TRNSPRT_QUIC;
  2856. neg->wanted = neg->allowed = CURL_HTTP_V3x;
  2857. break;
  2858. default: /* should not be possible */
  2859. break;
  2860. }
  2861. }
  2862. }
  2863. }
  2864. #endif
  2865. return result;
  2866. }
  2867. #ifdef USE_UNIX_SOCKETS
  2868. static CURLcode resolve_unix(struct Curl_easy *data,
  2869. struct connectdata *conn,
  2870. char *unix_path,
  2871. struct Curl_dns_entry **pdns)
  2872. {
  2873. struct Curl_dns_entry *hostaddr;
  2874. bool longpath = FALSE;
  2875. DEBUGASSERT(unix_path);
  2876. *pdns = NULL;
  2877. /* Unix domain sockets are local. The host gets ignored, just use the
  2878. * specified domain socket address. Do not cache "DNS entries". There is
  2879. * no DNS involved and we already have the file system path available. */
  2880. hostaddr = calloc(1, sizeof(struct Curl_dns_entry));
  2881. if(!hostaddr)
  2882. return CURLE_OUT_OF_MEMORY;
  2883. hostaddr->addr = Curl_unix2addr(unix_path, &longpath,
  2884. conn->bits.abstract_unix_socket);
  2885. if(!hostaddr->addr) {
  2886. if(longpath)
  2887. /* Long paths are not supported for now */
  2888. failf(data, "Unix socket path too long: '%s'", unix_path);
  2889. free(hostaddr);
  2890. return longpath ? CURLE_COULDNT_RESOLVE_HOST : CURLE_OUT_OF_MEMORY;
  2891. }
  2892. hostaddr->refcount = 1; /* connection is the only one holding this */
  2893. *pdns = hostaddr;
  2894. return CURLE_OK;
  2895. }
  2896. #endif
  2897. /*************************************************************
  2898. * Resolve the address of the server or proxy
  2899. *************************************************************/
  2900. static CURLcode resolve_server(struct Curl_easy *data,
  2901. struct connectdata *conn,
  2902. bool *async,
  2903. struct Curl_dns_entry **pdns)
  2904. {
  2905. struct hostname *ehost;
  2906. int eport;
  2907. timediff_t timeout_ms = Curl_timeleft(data, NULL, TRUE);
  2908. const char *peertype = "host";
  2909. CURLcode result;
  2910. *pdns = NULL;
  2911. #ifdef USE_UNIX_SOCKETS
  2912. {
  2913. char *unix_path = conn->unix_domain_socket;
  2914. #ifndef CURL_DISABLE_PROXY
  2915. if(!unix_path && CONN_IS_PROXIED(conn) && conn->socks_proxy.host.name &&
  2916. !strncmp(UNIX_SOCKET_PREFIX"/",
  2917. conn->socks_proxy.host.name, sizeof(UNIX_SOCKET_PREFIX)))
  2918. unix_path = conn->socks_proxy.host.name + sizeof(UNIX_SOCKET_PREFIX) - 1;
  2919. #endif
  2920. if(unix_path) {
  2921. /* This only works if previous transport is TRNSPRT_TCP. Check it? */
  2922. conn->transport_wanted = TRNSPRT_UNIX;
  2923. return resolve_unix(data, conn, unix_path, pdns);
  2924. }
  2925. }
  2926. #endif
  2927. #ifndef CURL_DISABLE_PROXY
  2928. if(CONN_IS_PROXIED(conn)) {
  2929. ehost = conn->bits.socksproxy ? &conn->socks_proxy.host :
  2930. &conn->http_proxy.host;
  2931. eport = conn->bits.socksproxy ? conn->socks_proxy.port :
  2932. conn->http_proxy.port;
  2933. peertype = "proxy";
  2934. }
  2935. else
  2936. #endif
  2937. {
  2938. ehost = conn->bits.conn_to_host ? &conn->conn_to_host : &conn->host;
  2939. /* If not connecting via a proxy, extract the port from the URL, if it is
  2940. * there, thus overriding any defaults that might have been set above. */
  2941. eport = conn->bits.conn_to_port ? conn->conn_to_port : conn->remote_port;
  2942. }
  2943. /* Resolve target host right on */
  2944. conn->hostname_resolve = strdup(ehost->name);
  2945. if(!conn->hostname_resolve)
  2946. return CURLE_OUT_OF_MEMORY;
  2947. result = Curl_resolv_timeout(data, conn->hostname_resolve,
  2948. eport, conn->ip_version,
  2949. pdns, timeout_ms);
  2950. DEBUGASSERT(!result || !*pdns);
  2951. if(result == CURLE_AGAIN) {
  2952. *async = TRUE;
  2953. return CURLE_OK;
  2954. }
  2955. else if(result == CURLE_OPERATION_TIMEDOUT) {
  2956. failf(data, "Failed to resolve %s '%s' with timeout after %"
  2957. FMT_TIMEDIFF_T " ms", peertype, ehost->dispname,
  2958. curlx_timediff(curlx_now(), data->progress.t_startsingle));
  2959. return CURLE_OPERATION_TIMEDOUT;
  2960. }
  2961. else if(result) {
  2962. failf(data, "Could not resolve %s: %s", peertype, ehost->dispname);
  2963. return result;
  2964. }
  2965. DEBUGASSERT(*pdns);
  2966. return CURLE_OK;
  2967. }
  2968. static void url_move_hostname(struct hostname *dest, struct hostname *src)
  2969. {
  2970. Curl_safefree(dest->rawalloc);
  2971. Curl_free_idnconverted_hostname(dest);
  2972. *dest = *src;
  2973. memset(src, 0, sizeof(*src));
  2974. }
  2975. /*
  2976. * Cleanup the connection `temp`, just allocated for `data`, before using the
  2977. * previously `existing` one for `data`. All relevant info is copied over
  2978. * and `temp` is freed.
  2979. */
  2980. static void reuse_conn(struct Curl_easy *data,
  2981. struct connectdata *temp,
  2982. struct connectdata *existing)
  2983. {
  2984. /* get the user+password information from the temp struct since it may
  2985. * be new for this request even when we reuse an existing connection */
  2986. if(temp->user) {
  2987. /* use the new username and password though */
  2988. free(existing->user);
  2989. free(existing->passwd);
  2990. existing->user = temp->user;
  2991. existing->passwd = temp->passwd;
  2992. temp->user = NULL;
  2993. temp->passwd = NULL;
  2994. }
  2995. #ifndef CURL_DISABLE_PROXY
  2996. existing->bits.proxy_user_passwd = temp->bits.proxy_user_passwd;
  2997. if(existing->bits.proxy_user_passwd) {
  2998. /* use the new proxy username and proxy password though */
  2999. free(existing->http_proxy.user);
  3000. free(existing->socks_proxy.user);
  3001. free(existing->http_proxy.passwd);
  3002. free(existing->socks_proxy.passwd);
  3003. existing->http_proxy.user = temp->http_proxy.user;
  3004. existing->socks_proxy.user = temp->socks_proxy.user;
  3005. existing->http_proxy.passwd = temp->http_proxy.passwd;
  3006. existing->socks_proxy.passwd = temp->socks_proxy.passwd;
  3007. temp->http_proxy.user = NULL;
  3008. temp->socks_proxy.user = NULL;
  3009. temp->http_proxy.passwd = NULL;
  3010. temp->socks_proxy.passwd = NULL;
  3011. }
  3012. #endif
  3013. /* Finding a connection for reuse in the cpool matches, among other
  3014. * things on the "remote-relevant" hostname. This is not necessarily
  3015. * the authority of the URL, e.g. conn->host. For example:
  3016. * - we use a proxy (not tunneling). we want to send all requests
  3017. * that use the same proxy on this connection.
  3018. * - we have a "connect-to" setting that may redirect the hostname of
  3019. * a new request to the same remote endpoint of an existing conn.
  3020. * We want to reuse an existing conn to the remote endpoint.
  3021. * Since connection reuse does not match on conn->host necessarily, we
  3022. * switch `existing` conn to `temp` conn's host settings.
  3023. * Is this correct in the case of TLS connections that have
  3024. * used the original hostname in SNI to negotiate? Do we send
  3025. * requests for another host through the different SNI?
  3026. */
  3027. url_move_hostname(&existing->host, &temp->host);
  3028. url_move_hostname(&existing->conn_to_host, &temp->conn_to_host);
  3029. existing->conn_to_port = temp->conn_to_port;
  3030. existing->remote_port = temp->remote_port;
  3031. free(existing->hostname_resolve);
  3032. existing->hostname_resolve = temp->hostname_resolve;
  3033. temp->hostname_resolve = NULL;
  3034. /* reuse init */
  3035. existing->bits.reuse = TRUE; /* yes, we are reusing here */
  3036. Curl_conn_free(data, temp);
  3037. }
  3038. static void conn_meta_freeentry(void *p)
  3039. {
  3040. (void)p;
  3041. /* Will always be FALSE. Cannot use a 0 assert here since compilers
  3042. * are not in agreement if they then want a NORETURN attribute or
  3043. * not. *sigh* */
  3044. DEBUGASSERT(p == NULL);
  3045. }
  3046. /**
  3047. * create_conn() sets up a new connectdata struct, or reuses an already
  3048. * existing one, and resolves hostname.
  3049. *
  3050. * if this function returns CURLE_OK and *async is set to TRUE, the resolve
  3051. * response will be coming asynchronously. If *async is FALSE, the name is
  3052. * already resolved.
  3053. *
  3054. * @param data The sessionhandle pointer
  3055. * @param in_connect is set to the next connection data pointer
  3056. * @param reusedp is set to to TRUE if connection was reused
  3057. * @see Curl_setup_conn()
  3058. *
  3059. */
  3060. static CURLcode create_conn(struct Curl_easy *data,
  3061. struct connectdata **in_connect,
  3062. bool *reusedp)
  3063. {
  3064. CURLcode result = CURLE_OK;
  3065. struct connectdata *conn;
  3066. struct connectdata *existing = NULL;
  3067. bool reuse;
  3068. bool connections_available = TRUE;
  3069. bool force_reuse = FALSE;
  3070. bool waitpipe = FALSE;
  3071. *reusedp = FALSE;
  3072. *in_connect = NULL;
  3073. /*************************************************************
  3074. * Check input data
  3075. *************************************************************/
  3076. if(!data->state.url) {
  3077. result = CURLE_URL_MALFORMAT;
  3078. goto out;
  3079. }
  3080. /* First, split up the current URL in parts so that we can use the
  3081. parts for checking against the already present connections. In order
  3082. to not have to modify everything at once, we allocate a temporary
  3083. connection data struct and fill in for comparison purposes. */
  3084. conn = allocate_conn(data);
  3085. if(!conn) {
  3086. result = CURLE_OUT_OF_MEMORY;
  3087. goto out;
  3088. }
  3089. /* We must set the return variable as soon as possible, so that our
  3090. parent can cleanup any possible allocs we may have done before
  3091. any failure */
  3092. *in_connect = conn;
  3093. /* Do the unfailable inits first, before checks that may early return */
  3094. Curl_hash_init(&conn->meta_hash, 23,
  3095. Curl_hash_str, curlx_str_key_compare, conn_meta_freeentry);
  3096. result = parseurlandfillconn(data, conn);
  3097. if(result)
  3098. goto out;
  3099. if(data->set.str[STRING_SASL_AUTHZID]) {
  3100. conn->sasl_authzid = strdup(data->set.str[STRING_SASL_AUTHZID]);
  3101. if(!conn->sasl_authzid) {
  3102. result = CURLE_OUT_OF_MEMORY;
  3103. goto out;
  3104. }
  3105. }
  3106. if(data->set.str[STRING_BEARER]) {
  3107. conn->oauth_bearer = strdup(data->set.str[STRING_BEARER]);
  3108. if(!conn->oauth_bearer) {
  3109. result = CURLE_OUT_OF_MEMORY;
  3110. goto out;
  3111. }
  3112. }
  3113. #ifdef USE_UNIX_SOCKETS
  3114. if(data->set.str[STRING_UNIX_SOCKET_PATH]) {
  3115. conn->unix_domain_socket = strdup(data->set.str[STRING_UNIX_SOCKET_PATH]);
  3116. if(!conn->unix_domain_socket) {
  3117. result = CURLE_OUT_OF_MEMORY;
  3118. goto out;
  3119. }
  3120. conn->bits.abstract_unix_socket = data->set.abstract_unix_socket;
  3121. }
  3122. #endif
  3123. /* After the Unix socket init but before the proxy vars are used, parse and
  3124. initialize the proxy vars */
  3125. #ifndef CURL_DISABLE_PROXY
  3126. result = create_conn_helper_init_proxy(data, conn);
  3127. if(result)
  3128. goto out;
  3129. /*************************************************************
  3130. * If the protocol is using SSL and HTTP proxy is used, we set
  3131. * the tunnel_proxy bit.
  3132. *************************************************************/
  3133. if((conn->given->flags&PROTOPT_SSL) && conn->bits.httpproxy)
  3134. conn->bits.tunnel_proxy = TRUE;
  3135. #endif
  3136. /*************************************************************
  3137. * Figure out the remote port number and fix it in the URL
  3138. *************************************************************/
  3139. result = parse_remote_port(data, conn);
  3140. if(result)
  3141. goto out;
  3142. /* Check for overridden login details and set them accordingly so that
  3143. they are known when protocol->setup_connection is called! */
  3144. result = override_login(data, conn);
  3145. if(result)
  3146. goto out;
  3147. result = set_login(data, conn); /* default credentials */
  3148. if(result)
  3149. goto out;
  3150. /*************************************************************
  3151. * Process the "connect to" linked list of hostname/port mappings.
  3152. * Do this after the remote port number has been fixed in the URL.
  3153. *************************************************************/
  3154. result = parse_connect_to_slist(data, conn, data->set.connect_to);
  3155. if(result)
  3156. goto out;
  3157. /*************************************************************
  3158. * IDN-convert the proxy hostnames
  3159. *************************************************************/
  3160. #ifndef CURL_DISABLE_PROXY
  3161. if(conn->bits.httpproxy) {
  3162. result = Curl_idnconvert_hostname(&conn->http_proxy.host);
  3163. if(result)
  3164. return result;
  3165. }
  3166. if(conn->bits.socksproxy) {
  3167. result = Curl_idnconvert_hostname(&conn->socks_proxy.host);
  3168. if(result)
  3169. return result;
  3170. }
  3171. #endif
  3172. if(conn->bits.conn_to_host) {
  3173. result = Curl_idnconvert_hostname(&conn->conn_to_host);
  3174. if(result)
  3175. return result;
  3176. }
  3177. /*************************************************************
  3178. * Check whether the host and the "connect to host" are equal.
  3179. * Do this after the hostnames have been IDN-converted.
  3180. *************************************************************/
  3181. if(conn->bits.conn_to_host &&
  3182. curl_strequal(conn->conn_to_host.name, conn->host.name)) {
  3183. conn->bits.conn_to_host = FALSE;
  3184. }
  3185. /*************************************************************
  3186. * Check whether the port and the "connect to port" are equal.
  3187. * Do this after the remote port number has been fixed in the URL.
  3188. *************************************************************/
  3189. if(conn->bits.conn_to_port && conn->conn_to_port == conn->remote_port) {
  3190. conn->bits.conn_to_port = FALSE;
  3191. }
  3192. #ifndef CURL_DISABLE_PROXY
  3193. /*************************************************************
  3194. * If the "connect to" feature is used with an HTTP proxy,
  3195. * we set the tunnel_proxy bit.
  3196. *************************************************************/
  3197. if((conn->bits.conn_to_host || conn->bits.conn_to_port) &&
  3198. conn->bits.httpproxy)
  3199. conn->bits.tunnel_proxy = TRUE;
  3200. #endif
  3201. /*************************************************************
  3202. * Setup internals depending on protocol. Needs to be done after
  3203. * we figured out what/if proxy to use.
  3204. *************************************************************/
  3205. result = setup_connection_internals(data, conn);
  3206. if(result)
  3207. goto out;
  3208. /***********************************************************************
  3209. * file: is a special case in that it does not need a network connection
  3210. ***********************************************************************/
  3211. #ifndef CURL_DISABLE_FILE
  3212. if(conn->handler->flags & PROTOPT_NONETWORK) {
  3213. bool done;
  3214. /* this is supposed to be the connect function so we better at least check
  3215. that the file is present here! */
  3216. DEBUGASSERT(conn->handler->connect_it);
  3217. data->info.conn_scheme = conn->handler->scheme;
  3218. /* conn_protocol can only provide "old" protocols */
  3219. data->info.conn_protocol = (conn->handler->protocol) & CURLPROTO_MASK;
  3220. result = conn->handler->connect_it(data, &done);
  3221. if(result)
  3222. goto out;
  3223. /* Setup a "faked" transfer that will do nothing */
  3224. Curl_attach_connection(data, conn);
  3225. result = Curl_cpool_add(data, conn);
  3226. if(!result) {
  3227. /* Setup whatever necessary for a resumed transfer */
  3228. result = setup_range(data);
  3229. if(!result) {
  3230. Curl_xfer_setup_nop(data);
  3231. result = Curl_init_do(data, conn);
  3232. }
  3233. }
  3234. if(result) {
  3235. DEBUGASSERT(conn->handler->done);
  3236. /* we ignore the return code for the protocol-specific DONE */
  3237. (void)conn->handler->done(data, result, FALSE);
  3238. }
  3239. goto out;
  3240. }
  3241. #endif
  3242. /* Setup filter for network connections */
  3243. conn->recv[FIRSTSOCKET] = Curl_cf_recv;
  3244. conn->send[FIRSTSOCKET] = Curl_cf_send;
  3245. conn->recv[SECONDARYSOCKET] = Curl_cf_recv;
  3246. conn->send[SECONDARYSOCKET] = Curl_cf_send;
  3247. conn->bits.tcp_fastopen = data->set.tcp_fastopen;
  3248. /* Complete the easy's SSL configuration for connection cache matching */
  3249. result = Curl_ssl_easy_config_complete(data);
  3250. if(result)
  3251. goto out;
  3252. Curl_cpool_prune_dead(data);
  3253. /*************************************************************
  3254. * Check the current list of connections to see if we can
  3255. * reuse an already existing one or if we have to create a
  3256. * new one.
  3257. *************************************************************/
  3258. DEBUGASSERT(conn->user);
  3259. DEBUGASSERT(conn->passwd);
  3260. /* reuse_fresh is TRUE if we are told to use a new connection by force, but
  3261. we only acknowledge this option if this is not a reused connection
  3262. already (which happens due to follow-location or during an HTTP
  3263. authentication phase). CONNECT_ONLY transfers also refuse reuse. */
  3264. if((data->set.reuse_fresh && !data->state.followlocation) ||
  3265. data->set.connect_only)
  3266. reuse = FALSE;
  3267. else
  3268. reuse = ConnectionExists(data, conn, &existing, &force_reuse, &waitpipe);
  3269. if(reuse) {
  3270. /*
  3271. * We already have a connection for this, we got the former connection in
  3272. * `existing` and thus we need to cleanup the one we just
  3273. * allocated before we can move along and use `existing`.
  3274. */
  3275. bool tls_upgraded = (!(conn->given->flags & PROTOPT_SSL) &&
  3276. Curl_conn_is_ssl(conn, FIRSTSOCKET));
  3277. reuse_conn(data, conn, existing);
  3278. conn = existing;
  3279. *in_connect = conn;
  3280. #ifndef CURL_DISABLE_PROXY
  3281. infof(data, "Reusing existing %s: connection%s with %s %s",
  3282. conn->given->scheme,
  3283. tls_upgraded ? " (upgraded to SSL)" : "",
  3284. conn->bits.proxy ? "proxy" : "host",
  3285. conn->socks_proxy.host.name ? conn->socks_proxy.host.dispname :
  3286. conn->http_proxy.host.name ? conn->http_proxy.host.dispname :
  3287. conn->host.dispname);
  3288. #else
  3289. infof(data, "Reusing existing %s: connection%s with host %s",
  3290. conn->given->scheme,
  3291. tls_upgraded ? " (upgraded to SSL)" : "",
  3292. conn->host.dispname);
  3293. #endif
  3294. }
  3295. else {
  3296. /* We have decided that we want a new connection. However, we may not
  3297. be able to do that if we have reached the limit of how many
  3298. connections we are allowed to open. */
  3299. if(conn->handler->flags & PROTOPT_ALPN) {
  3300. /* The protocol wants it, so set the bits if enabled in the easy handle
  3301. (default) */
  3302. if(data->set.ssl_enable_alpn)
  3303. conn->bits.tls_enable_alpn = TRUE;
  3304. }
  3305. if(waitpipe) {
  3306. /* There is a connection that *might* become usable for multiplexing
  3307. "soon", and we wait for that */
  3308. infof(data, "Waiting on connection to negotiate possible multiplexing.");
  3309. connections_available = FALSE;
  3310. }
  3311. else {
  3312. switch(Curl_cpool_check_limits(data, conn)) {
  3313. case CPOOL_LIMIT_DEST:
  3314. infof(data, "No more connections allowed to host");
  3315. connections_available = FALSE;
  3316. break;
  3317. case CPOOL_LIMIT_TOTAL:
  3318. if(data->master_mid != UINT_MAX)
  3319. CURL_TRC_M(data, "Allowing sub-requests (like DoH) to override "
  3320. "max connection limit");
  3321. else {
  3322. infof(data, "No connections available, total of %ld reached.",
  3323. data->multi->max_total_connections);
  3324. connections_available = FALSE;
  3325. }
  3326. break;
  3327. default:
  3328. break;
  3329. }
  3330. }
  3331. if(!connections_available) {
  3332. Curl_conn_free(data, conn);
  3333. *in_connect = NULL;
  3334. result = CURLE_NO_CONNECTION_AVAILABLE;
  3335. goto out;
  3336. }
  3337. else {
  3338. /*
  3339. * This is a brand new connection, so let's store it in the connection
  3340. * cache of ours!
  3341. */
  3342. result = Curl_ssl_conn_config_init(data, conn);
  3343. if(result) {
  3344. DEBUGF(curl_mfprintf(stderr, "Error: init connection ssl config\n"));
  3345. goto out;
  3346. }
  3347. Curl_attach_connection(data, conn);
  3348. result = Curl_cpool_add(data, conn);
  3349. if(result)
  3350. goto out;
  3351. }
  3352. #ifdef USE_NTLM
  3353. /* If NTLM is requested in a part of this connection, make sure we do not
  3354. assume the state is fine as this is a fresh connection and NTLM is
  3355. connection based. */
  3356. if((data->state.authhost.picked & CURLAUTH_NTLM) &&
  3357. data->state.authhost.done) {
  3358. infof(data, "NTLM picked AND auth done set, clear picked");
  3359. data->state.authhost.picked = CURLAUTH_NONE;
  3360. data->state.authhost.done = FALSE;
  3361. }
  3362. if((data->state.authproxy.picked & CURLAUTH_NTLM) &&
  3363. data->state.authproxy.done) {
  3364. infof(data, "NTLM-proxy picked AND auth done set, clear picked");
  3365. data->state.authproxy.picked = CURLAUTH_NONE;
  3366. data->state.authproxy.done = FALSE;
  3367. }
  3368. #endif
  3369. }
  3370. /* Setup and init stuff before DO starts, in preparing for the transfer. */
  3371. result = Curl_init_do(data, conn);
  3372. if(result)
  3373. goto out;
  3374. /*
  3375. * Setup whatever necessary for a resumed transfer
  3376. */
  3377. result = setup_range(data);
  3378. if(result)
  3379. goto out;
  3380. /* Continue connectdata initialization here. */
  3381. if(conn->bits.reuse) {
  3382. /* We are reusing the connection - no need to resolve anything, and
  3383. idnconvert_hostname() was called already in create_conn() for the reuse
  3384. case. */
  3385. *reusedp = TRUE;
  3386. }
  3387. /* persist the scheme and handler the transfer is using */
  3388. data->info.conn_scheme = conn->handler->scheme;
  3389. /* conn_protocol can only provide "old" protocols */
  3390. data->info.conn_protocol = (conn->handler->protocol) & CURLPROTO_MASK;
  3391. data->info.used_proxy =
  3392. #ifdef CURL_DISABLE_PROXY
  3393. 0
  3394. #else
  3395. conn->bits.proxy
  3396. #endif
  3397. ;
  3398. /* Everything general done, inform filters that they need
  3399. * to prepare for a data transfer. */
  3400. result = Curl_conn_ev_data_setup(data);
  3401. out:
  3402. return result;
  3403. }
  3404. /* Curl_setup_conn() is called after the name resolve initiated in
  3405. * create_conn() is all done.
  3406. *
  3407. * Curl_setup_conn() also handles reused connections
  3408. */
  3409. CURLcode Curl_setup_conn(struct Curl_easy *data,
  3410. struct Curl_dns_entry *dns,
  3411. bool *protocol_done)
  3412. {
  3413. CURLcode result = CURLE_OK;
  3414. struct connectdata *conn = data->conn;
  3415. DEBUGASSERT(dns);
  3416. Curl_pgrsTime(data, TIMER_NAMELOOKUP);
  3417. if(!conn->bits.reuse)
  3418. result = Curl_conn_setup(data, conn, FIRSTSOCKET, dns,
  3419. CURL_CF_SSL_DEFAULT);
  3420. if(!result)
  3421. result = Curl_headers_init(data);
  3422. /* not sure we need this flag to be passed around any more */
  3423. *protocol_done = FALSE;
  3424. return result;
  3425. }
  3426. CURLcode Curl_connect(struct Curl_easy *data,
  3427. bool *asyncp,
  3428. bool *protocol_done)
  3429. {
  3430. CURLcode result;
  3431. struct connectdata *conn;
  3432. bool reused = FALSE;
  3433. *asyncp = FALSE; /* assume synchronous resolves by default */
  3434. *protocol_done = FALSE;
  3435. /* Set the request to virgin state based on transfer settings */
  3436. Curl_req_hard_reset(&data->req, data);
  3437. /* call the stuff that needs to be called */
  3438. result = create_conn(data, &conn, &reused);
  3439. if(result == CURLE_NO_CONNECTION_AVAILABLE) {
  3440. DEBUGASSERT(!conn);
  3441. return result;
  3442. }
  3443. if(!result) {
  3444. DEBUGASSERT(conn);
  3445. if(reused) {
  3446. if(CONN_ATTACHED(conn) > 1)
  3447. /* multiplexed */
  3448. *protocol_done = TRUE;
  3449. }
  3450. else if(conn->handler->flags & PROTOPT_NONETWORK) {
  3451. *asyncp = FALSE;
  3452. Curl_pgrsTime(data, TIMER_NAMELOOKUP);
  3453. *protocol_done = TRUE;
  3454. }
  3455. else {
  3456. /*************************************************************
  3457. * Resolve the address of the server or proxy
  3458. *************************************************************/
  3459. struct Curl_dns_entry *dns;
  3460. result = resolve_server(data, conn, asyncp, &dns);
  3461. if(!result) {
  3462. *asyncp = !dns;
  3463. if(dns)
  3464. /* DNS resolution is done: that is either because this is a reused
  3465. connection, in which case DNS was unnecessary, or because DNS
  3466. really did finish already (synch resolver/fast async resolve) */
  3467. result = Curl_setup_conn(data, dns, protocol_done);
  3468. }
  3469. }
  3470. }
  3471. if(result && conn) {
  3472. /* We are not allowed to return failure with memory left allocated in the
  3473. connectdata struct, free those here */
  3474. Curl_detach_connection(data);
  3475. Curl_conn_terminate(data, conn, TRUE);
  3476. }
  3477. return result;
  3478. }
  3479. /*
  3480. * Curl_init_do() inits the readwrite session. This is inited each time (in
  3481. * the DO function before the protocol-specific DO functions are invoked) for
  3482. * a transfer, sometimes multiple times on the same Curl_easy. Make sure
  3483. * nothing in here depends on stuff that are setup dynamically for the
  3484. * transfer.
  3485. *
  3486. * Allow this function to get called with 'conn' set to NULL.
  3487. */
  3488. CURLcode Curl_init_do(struct Curl_easy *data, struct connectdata *conn)
  3489. {
  3490. /* if this is a pushed stream, we need this: */
  3491. CURLcode result;
  3492. if(conn) {
  3493. conn->bits.do_more = FALSE; /* by default there is no curl_do_more() to
  3494. use */
  3495. /* if the protocol used does not support wildcards, switch it off */
  3496. if(data->state.wildcardmatch &&
  3497. !(conn->handler->flags & PROTOPT_WILDCARD))
  3498. data->state.wildcardmatch = FALSE;
  3499. }
  3500. data->state.done = FALSE; /* *_done() is not called yet */
  3501. if(data->req.no_body)
  3502. /* in HTTP lingo, no body means using the HEAD request... */
  3503. data->state.httpreq = HTTPREQ_HEAD;
  3504. result = Curl_req_start(&data->req, data);
  3505. if(!result) {
  3506. Curl_speedinit(data);
  3507. Curl_pgrsSetUploadCounter(data, 0);
  3508. Curl_pgrsSetDownloadCounter(data, 0);
  3509. }
  3510. return result;
  3511. }
  3512. #if defined(USE_HTTP2) || defined(USE_HTTP3)
  3513. #ifdef USE_NGHTTP2
  3514. static void priority_remove_child(struct Curl_easy *parent,
  3515. struct Curl_easy *child)
  3516. {
  3517. struct Curl_data_prio_node **pnext = &parent->set.priority.children;
  3518. struct Curl_data_prio_node *pnode = parent->set.priority.children;
  3519. DEBUGASSERT(child->set.priority.parent == parent);
  3520. while(pnode && pnode->data != child) {
  3521. pnext = &pnode->next;
  3522. pnode = pnode->next;
  3523. }
  3524. DEBUGASSERT(pnode);
  3525. if(pnode) {
  3526. *pnext = pnode->next;
  3527. free(pnode);
  3528. }
  3529. child->set.priority.parent = 0;
  3530. child->set.priority.exclusive = FALSE;
  3531. }
  3532. CURLcode Curl_data_priority_add_child(struct Curl_easy *parent,
  3533. struct Curl_easy *child,
  3534. bool exclusive)
  3535. {
  3536. if(child->set.priority.parent) {
  3537. priority_remove_child(child->set.priority.parent, child);
  3538. }
  3539. if(parent) {
  3540. struct Curl_data_prio_node **tail;
  3541. struct Curl_data_prio_node *pnode;
  3542. pnode = calloc(1, sizeof(*pnode));
  3543. if(!pnode)
  3544. return CURLE_OUT_OF_MEMORY;
  3545. pnode->data = child;
  3546. if(parent->set.priority.children && exclusive) {
  3547. /* exclusive: move all existing children underneath the new child */
  3548. struct Curl_data_prio_node *node = parent->set.priority.children;
  3549. while(node) {
  3550. node->data->set.priority.parent = child;
  3551. node = node->next;
  3552. }
  3553. tail = &child->set.priority.children;
  3554. while(*tail)
  3555. tail = &(*tail)->next;
  3556. DEBUGASSERT(!*tail);
  3557. *tail = parent->set.priority.children;
  3558. parent->set.priority.children = 0;
  3559. }
  3560. tail = &parent->set.priority.children;
  3561. while(*tail) {
  3562. (*tail)->data->set.priority.exclusive = FALSE;
  3563. tail = &(*tail)->next;
  3564. }
  3565. DEBUGASSERT(!*tail);
  3566. *tail = pnode;
  3567. }
  3568. child->set.priority.parent = parent;
  3569. child->set.priority.exclusive = exclusive;
  3570. return CURLE_OK;
  3571. }
  3572. #endif /* USE_NGHTTP2 */
  3573. #ifdef USE_NGHTTP2
  3574. static void data_priority_cleanup(struct Curl_easy *data)
  3575. {
  3576. while(data->set.priority.children) {
  3577. struct Curl_easy *tmp = data->set.priority.children->data;
  3578. priority_remove_child(data, tmp);
  3579. if(data->set.priority.parent)
  3580. Curl_data_priority_add_child(data->set.priority.parent, tmp, FALSE);
  3581. }
  3582. if(data->set.priority.parent)
  3583. priority_remove_child(data->set.priority.parent, data);
  3584. }
  3585. #endif
  3586. void Curl_data_priority_clear_state(struct Curl_easy *data)
  3587. {
  3588. memset(&data->state.priority, 0, sizeof(data->state.priority));
  3589. }
  3590. #endif /* USE_HTTP2 || USE_HTTP3 */
  3591. CURLcode Curl_conn_meta_set(struct connectdata *conn, const char *key,
  3592. void *meta_data, Curl_meta_dtor *meta_dtor)
  3593. {
  3594. if(!Curl_hash_add2(&conn->meta_hash, CURL_UNCONST(key), strlen(key) + 1,
  3595. meta_data, meta_dtor)) {
  3596. meta_dtor(CURL_UNCONST(key), strlen(key) + 1, meta_data);
  3597. return CURLE_OUT_OF_MEMORY;
  3598. }
  3599. return CURLE_OK;
  3600. }
  3601. void Curl_conn_meta_remove(struct connectdata *conn, const char *key)
  3602. {
  3603. Curl_hash_delete(&conn->meta_hash, CURL_UNCONST(key), strlen(key) + 1);
  3604. }
  3605. void *Curl_conn_meta_get(struct connectdata *conn, const char *key)
  3606. {
  3607. return Curl_hash_pick(&conn->meta_hash, CURL_UNCONST(key), strlen(key) + 1);
  3608. }
  3609. CURLcode Curl_1st_err(CURLcode r1, CURLcode r2)
  3610. {
  3611. return r1 ? r1 : r2;
  3612. }
  3613. CURLcode Curl_1st_fatal(CURLcode r1, CURLcode r2)
  3614. {
  3615. if(r1 && (r1 != CURLE_AGAIN))
  3616. return r1;
  3617. if(r2 && (r2 != CURLE_AGAIN))
  3618. return r2;
  3619. return r1;
  3620. }