url.c 119 KB

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