url.c 185 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2014, 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 http://curl.haxx.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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #ifdef HAVE_NETINET_IN_H
  24. #include <netinet/in.h>
  25. #endif
  26. #ifdef HAVE_NETDB_H
  27. #include <netdb.h>
  28. #endif
  29. #ifdef HAVE_ARPA_INET_H
  30. #include <arpa/inet.h>
  31. #endif
  32. #ifdef HAVE_NET_IF_H
  33. #include <net/if.h>
  34. #endif
  35. #ifdef HAVE_SYS_IOCTL_H
  36. #include <sys/ioctl.h>
  37. #endif
  38. #ifdef HAVE_SYS_PARAM_H
  39. #include <sys/param.h>
  40. #endif
  41. #ifdef __VMS
  42. #include <in.h>
  43. #include <inet.h>
  44. #endif
  45. #ifdef HAVE_SYS_UN_H
  46. #include <sys/un.h>
  47. #endif
  48. #ifndef HAVE_SOCKET
  49. #error "We can't compile without socket() support!"
  50. #endif
  51. #ifdef HAVE_LIMITS_H
  52. #include <limits.h>
  53. #endif
  54. #ifdef USE_LIBIDN
  55. #include <idna.h>
  56. #include <tld.h>
  57. #include <stringprep.h>
  58. #ifdef HAVE_IDN_FREE_H
  59. #include <idn-free.h>
  60. #else
  61. /* prototype from idn-free.h, not provided by libidn 0.4.5's make install! */
  62. void idn_free (void *ptr);
  63. #endif
  64. #ifndef HAVE_IDN_FREE
  65. /* if idn_free() was not found in this version of libidn use free() instead */
  66. #define idn_free(x) (free)(x)
  67. #endif
  68. #elif defined(USE_WIN32_IDN)
  69. /* prototype for curl_win32_idn_to_ascii() */
  70. int curl_win32_idn_to_ascii(const char *in, char **out);
  71. #endif /* USE_LIBIDN */
  72. #include "urldata.h"
  73. #include "netrc.h"
  74. #include "formdata.h"
  75. #include "vtls/vtls.h"
  76. #include "hostip.h"
  77. #include "transfer.h"
  78. #include "sendf.h"
  79. #include "progress.h"
  80. #include "cookie.h"
  81. #include "strequal.h"
  82. #include "strerror.h"
  83. #include "escape.h"
  84. #include "strtok.h"
  85. #include "share.h"
  86. #include "content_encoding.h"
  87. #include "http_digest.h"
  88. #include "http_negotiate.h"
  89. #include "select.h"
  90. #include "multiif.h"
  91. #include "easyif.h"
  92. #include "speedcheck.h"
  93. #include "rawstr.h"
  94. #include "warnless.h"
  95. #include "non-ascii.h"
  96. #include "inet_pton.h"
  97. /* And now for the protocols */
  98. #include "ftp.h"
  99. #include "dict.h"
  100. #include "telnet.h"
  101. #include "tftp.h"
  102. #include "http.h"
  103. #include "file.h"
  104. #include "curl_ldap.h"
  105. #include "ssh.h"
  106. #include "imap.h"
  107. #include "url.h"
  108. #include "connect.h"
  109. #include "inet_ntop.h"
  110. #include "curl_ntlm.h"
  111. #include "curl_ntlm_wb.h"
  112. #include "socks.h"
  113. #include "curl_rtmp.h"
  114. #include "gopher.h"
  115. #include "http_proxy.h"
  116. #include "bundles.h"
  117. #include "conncache.h"
  118. #include "multihandle.h"
  119. #include "pipeline.h"
  120. #include "dotdot.h"
  121. #include "strdup.h"
  122. #define _MPRINTF_REPLACE /* use our functions only */
  123. #include <curl/mprintf.h>
  124. #include "curl_memory.h"
  125. /* The last #include file should be: */
  126. #include "memdebug.h"
  127. /* Local static prototypes */
  128. static struct connectdata *
  129. find_oldest_idle_connection(struct SessionHandle *data);
  130. static struct connectdata *
  131. find_oldest_idle_connection_in_bundle(struct SessionHandle *data,
  132. struct connectbundle *bundle);
  133. static void conn_free(struct connectdata *conn);
  134. static void signalPipeClose(struct curl_llist *pipeline, bool pipe_broke);
  135. static CURLcode do_init(struct connectdata *conn);
  136. static CURLcode parse_url_login(struct SessionHandle *data,
  137. struct connectdata *conn,
  138. char **userptr, char **passwdptr,
  139. char **optionsptr);
  140. static CURLcode parse_login_details(const char *login, const size_t len,
  141. char **userptr, char **passwdptr,
  142. char **optionsptr);
  143. /*
  144. * Protocol table.
  145. */
  146. static const struct Curl_handler * const protocols[] = {
  147. #ifndef CURL_DISABLE_HTTP
  148. &Curl_handler_http,
  149. #endif
  150. #if defined(USE_SSL) && !defined(CURL_DISABLE_HTTP)
  151. &Curl_handler_https,
  152. #endif
  153. #ifndef CURL_DISABLE_FTP
  154. &Curl_handler_ftp,
  155. #endif
  156. #if defined(USE_SSL) && !defined(CURL_DISABLE_FTP)
  157. &Curl_handler_ftps,
  158. #endif
  159. #ifndef CURL_DISABLE_TELNET
  160. &Curl_handler_telnet,
  161. #endif
  162. #ifndef CURL_DISABLE_DICT
  163. &Curl_handler_dict,
  164. #endif
  165. #ifndef CURL_DISABLE_LDAP
  166. &Curl_handler_ldap,
  167. #if !defined(CURL_DISABLE_LDAPS) && \
  168. ((defined(USE_OPENLDAP) && defined(USE_SSL)) || \
  169. (!defined(USE_OPENLDAP) && defined(HAVE_LDAP_SSL)))
  170. &Curl_handler_ldaps,
  171. #endif
  172. #endif
  173. #ifndef CURL_DISABLE_FILE
  174. &Curl_handler_file,
  175. #endif
  176. #ifndef CURL_DISABLE_TFTP
  177. &Curl_handler_tftp,
  178. #endif
  179. #ifdef USE_LIBSSH2
  180. &Curl_handler_scp,
  181. &Curl_handler_sftp,
  182. #endif
  183. #ifndef CURL_DISABLE_IMAP
  184. &Curl_handler_imap,
  185. #ifdef USE_SSL
  186. &Curl_handler_imaps,
  187. #endif
  188. #endif
  189. #ifndef CURL_DISABLE_POP3
  190. &Curl_handler_pop3,
  191. #ifdef USE_SSL
  192. &Curl_handler_pop3s,
  193. #endif
  194. #endif
  195. #if !defined(CURL_DISABLE_SMB) && defined(USE_NTLM) && \
  196. (CURL_SIZEOF_CURL_OFF_T > 4) && \
  197. (!defined(USE_WINDOWS_SSPI) || defined(USE_WIN32_CRYPTO))
  198. &Curl_handler_smb,
  199. #ifdef USE_SSL
  200. &Curl_handler_smbs,
  201. #endif
  202. #endif
  203. #ifndef CURL_DISABLE_SMTP
  204. &Curl_handler_smtp,
  205. #ifdef USE_SSL
  206. &Curl_handler_smtps,
  207. #endif
  208. #endif
  209. #ifndef CURL_DISABLE_RTSP
  210. &Curl_handler_rtsp,
  211. #endif
  212. #ifndef CURL_DISABLE_GOPHER
  213. &Curl_handler_gopher,
  214. #endif
  215. #ifdef USE_LIBRTMP
  216. &Curl_handler_rtmp,
  217. &Curl_handler_rtmpt,
  218. &Curl_handler_rtmpe,
  219. &Curl_handler_rtmpte,
  220. &Curl_handler_rtmps,
  221. &Curl_handler_rtmpts,
  222. #endif
  223. (struct Curl_handler *) NULL
  224. };
  225. /*
  226. * Dummy handler for undefined protocol schemes.
  227. */
  228. static const struct Curl_handler Curl_handler_dummy = {
  229. "<no protocol>", /* scheme */
  230. ZERO_NULL, /* setup_connection */
  231. ZERO_NULL, /* do_it */
  232. ZERO_NULL, /* done */
  233. ZERO_NULL, /* do_more */
  234. ZERO_NULL, /* connect_it */
  235. ZERO_NULL, /* connecting */
  236. ZERO_NULL, /* doing */
  237. ZERO_NULL, /* proto_getsock */
  238. ZERO_NULL, /* doing_getsock */
  239. ZERO_NULL, /* domore_getsock */
  240. ZERO_NULL, /* perform_getsock */
  241. ZERO_NULL, /* disconnect */
  242. ZERO_NULL, /* readwrite */
  243. 0, /* defport */
  244. 0, /* protocol */
  245. PROTOPT_NONE /* flags */
  246. };
  247. void Curl_freeset(struct SessionHandle *data)
  248. {
  249. /* Free all dynamic strings stored in the data->set substructure. */
  250. enum dupstring i;
  251. for(i=(enum dupstring)0; i < STRING_LAST; i++) {
  252. Curl_safefree(data->set.str[i]);
  253. }
  254. if(data->change.referer_alloc) {
  255. Curl_safefree(data->change.referer);
  256. data->change.referer_alloc = FALSE;
  257. }
  258. data->change.referer = NULL;
  259. if(data->change.url_alloc) {
  260. Curl_safefree(data->change.url);
  261. data->change.url_alloc = FALSE;
  262. }
  263. data->change.url = NULL;
  264. }
  265. static CURLcode setstropt(char **charp, char *s)
  266. {
  267. /* Release the previous storage at `charp' and replace by a dynamic storage
  268. copy of `s'. Return CURLE_OK or CURLE_OUT_OF_MEMORY. */
  269. Curl_safefree(*charp);
  270. if(s) {
  271. s = strdup(s);
  272. if(!s)
  273. return CURLE_OUT_OF_MEMORY;
  274. *charp = s;
  275. }
  276. return CURLE_OK;
  277. }
  278. static CURLcode setstropt_userpwd(char *option, char **userp, char **passwdp)
  279. {
  280. CURLcode result = CURLE_OK;
  281. char *user = NULL;
  282. char *passwd = NULL;
  283. /* Parse the login details if specified. It not then we treat NULL as a hint
  284. to clear the existing data */
  285. if(option) {
  286. result = parse_login_details(option, strlen(option),
  287. (userp ? &user : NULL),
  288. (passwdp ? &passwd : NULL),
  289. NULL);
  290. }
  291. if(!result) {
  292. /* Store the username part of option if required */
  293. if(userp) {
  294. if(!user && option && option[0] == ':') {
  295. /* Allocate an empty string instead of returning NULL as user name */
  296. user = strdup("");
  297. if(!user)
  298. result = CURLE_OUT_OF_MEMORY;
  299. }
  300. Curl_safefree(*userp);
  301. *userp = user;
  302. }
  303. /* Store the password part of option if required */
  304. if(passwdp) {
  305. Curl_safefree(*passwdp);
  306. *passwdp = passwd;
  307. }
  308. }
  309. return result;
  310. }
  311. CURLcode Curl_dupset(struct SessionHandle *dst, struct SessionHandle *src)
  312. {
  313. CURLcode result = CURLE_OK;
  314. enum dupstring i;
  315. /* Copy src->set into dst->set first, then deal with the strings
  316. afterwards */
  317. dst->set = src->set;
  318. /* clear all string pointers first */
  319. memset(dst->set.str, 0, STRING_LAST * sizeof(char *));
  320. /* duplicate all strings */
  321. for(i=(enum dupstring)0; i< STRING_LASTZEROTERMINATED; i++) {
  322. result = setstropt(&dst->set.str[i], src->set.str[i]);
  323. if(result)
  324. return result;
  325. }
  326. /* duplicate memory areas pointed to */
  327. i = STRING_COPYPOSTFIELDS;
  328. if(src->set.postfieldsize && src->set.str[i]) {
  329. /* postfieldsize is curl_off_t, Curl_memdup() takes a size_t ... */
  330. dst->set.str[i] = Curl_memdup(src->set.str[i],
  331. curlx_sotouz(src->set.postfieldsize));
  332. if(!dst->set.str[i])
  333. return CURLE_OUT_OF_MEMORY;
  334. /* point to the new copy */
  335. dst->set.postfields = dst->set.str[i];
  336. }
  337. return CURLE_OK;
  338. }
  339. /*
  340. * This is the internal function curl_easy_cleanup() calls. This should
  341. * cleanup and free all resources associated with this sessionhandle.
  342. *
  343. * NOTE: if we ever add something that attempts to write to a socket or
  344. * similar here, we must ignore SIGPIPE first. It is currently only done
  345. * when curl_easy_perform() is invoked.
  346. */
  347. CURLcode Curl_close(struct SessionHandle *data)
  348. {
  349. struct Curl_multi *m;
  350. if(!data)
  351. return CURLE_OK;
  352. Curl_expire(data, 0); /* shut off timers */
  353. m = data->multi;
  354. if(m)
  355. /* This handle is still part of a multi handle, take care of this first
  356. and detach this handle from there. */
  357. curl_multi_remove_handle(data->multi, data);
  358. if(data->multi_easy)
  359. /* when curl_easy_perform() is used, it creates its own multi handle to
  360. use and this is the one */
  361. curl_multi_cleanup(data->multi_easy);
  362. /* Destroy the timeout list that is held in the easy handle. It is
  363. /normally/ done by curl_multi_remove_handle() but this is "just in
  364. case" */
  365. if(data->state.timeoutlist) {
  366. Curl_llist_destroy(data->state.timeoutlist, NULL);
  367. data->state.timeoutlist = NULL;
  368. }
  369. data->magic = 0; /* force a clear AFTER the possibly enforced removal from
  370. the multi handle, since that function uses the magic
  371. field! */
  372. if(data->state.rangestringalloc)
  373. free(data->state.range);
  374. /* Free the pathbuffer */
  375. Curl_safefree(data->state.pathbuffer);
  376. data->state.path = NULL;
  377. /* freed here just in case DONE wasn't called */
  378. Curl_free_request_state(data);
  379. /* Close down all open SSL info and sessions */
  380. Curl_ssl_close_all(data);
  381. Curl_safefree(data->state.first_host);
  382. Curl_safefree(data->state.scratch);
  383. Curl_ssl_free_certinfo(data);
  384. /* Cleanup possible redirect junk */
  385. if(data->req.newurl) {
  386. free(data->req.newurl);
  387. data->req.newurl = NULL;
  388. }
  389. if(data->change.referer_alloc) {
  390. Curl_safefree(data->change.referer);
  391. data->change.referer_alloc = FALSE;
  392. }
  393. data->change.referer = NULL;
  394. if(data->change.url_alloc) {
  395. Curl_safefree(data->change.url);
  396. data->change.url_alloc = FALSE;
  397. }
  398. data->change.url = NULL;
  399. Curl_safefree(data->state.headerbuff);
  400. Curl_flush_cookies(data, 1);
  401. Curl_digest_cleanup(data);
  402. Curl_safefree(data->info.contenttype);
  403. Curl_safefree(data->info.wouldredirect);
  404. /* this destroys the channel and we cannot use it anymore after this */
  405. Curl_resolver_cleanup(data->state.resolver);
  406. Curl_convert_close(data);
  407. /* No longer a dirty share, if it exists */
  408. if(data->share) {
  409. Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
  410. data->share->dirty--;
  411. Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
  412. }
  413. Curl_freeset(data);
  414. free(data);
  415. return CURLE_OK;
  416. }
  417. /*
  418. * Initialize the UserDefined fields within a SessionHandle.
  419. * This may be safely called on a new or existing SessionHandle.
  420. */
  421. CURLcode Curl_init_userdefined(struct UserDefined *set)
  422. {
  423. CURLcode result = CURLE_OK;
  424. set->out = stdout; /* default output to stdout */
  425. set->in = stdin; /* default input from stdin */
  426. set->err = stderr; /* default stderr to stderr */
  427. /* use fwrite as default function to store output */
  428. set->fwrite_func = (curl_write_callback)fwrite;
  429. /* use fread as default function to read input */
  430. set->fread_func = (curl_read_callback)fread;
  431. set->is_fread_set = 0;
  432. set->is_fwrite_set = 0;
  433. set->seek_func = ZERO_NULL;
  434. set->seek_client = ZERO_NULL;
  435. /* conversion callbacks for non-ASCII hosts */
  436. set->convfromnetwork = ZERO_NULL;
  437. set->convtonetwork = ZERO_NULL;
  438. set->convfromutf8 = ZERO_NULL;
  439. set->filesize = -1; /* we don't know the size */
  440. set->postfieldsize = -1; /* unknown size */
  441. set->maxredirs = -1; /* allow any amount by default */
  442. set->httpreq = HTTPREQ_GET; /* Default HTTP request */
  443. set->rtspreq = RTSPREQ_OPTIONS; /* Default RTSP request */
  444. set->ftp_use_epsv = TRUE; /* FTP defaults to EPSV operations */
  445. set->ftp_use_eprt = TRUE; /* FTP defaults to EPRT operations */
  446. set->ftp_use_pret = FALSE; /* mainly useful for drftpd servers */
  447. set->ftp_filemethod = FTPFILE_MULTICWD;
  448. set->dns_cache_timeout = 60; /* Timeout every 60 seconds by default */
  449. /* Set the default size of the SSL session ID cache */
  450. set->ssl.max_ssl_sessions = 5;
  451. set->proxyport = CURL_DEFAULT_PROXY_PORT; /* from url.h */
  452. set->proxytype = CURLPROXY_HTTP; /* defaults to HTTP proxy */
  453. set->httpauth = CURLAUTH_BASIC; /* defaults to basic */
  454. set->proxyauth = CURLAUTH_BASIC; /* defaults to basic */
  455. /* make libcurl quiet by default: */
  456. set->hide_progress = TRUE; /* CURLOPT_NOPROGRESS changes these */
  457. /*
  458. * libcurl 7.10 introduced SSL verification *by default*! This needs to be
  459. * switched off unless wanted.
  460. */
  461. set->ssl.verifypeer = TRUE;
  462. set->ssl.verifyhost = TRUE;
  463. #ifdef USE_TLS_SRP
  464. set->ssl.authtype = CURL_TLSAUTH_NONE;
  465. #endif
  466. set->ssh_auth_types = CURLSSH_AUTH_DEFAULT; /* defaults to any auth
  467. type */
  468. set->ssl.sessionid = TRUE; /* session ID caching enabled by default */
  469. set->new_file_perms = 0644; /* Default permissions */
  470. set->new_directory_perms = 0755; /* Default permissions */
  471. /* for the *protocols fields we don't use the CURLPROTO_ALL convenience
  472. define since we internally only use the lower 16 bits for the passed
  473. in bitmask to not conflict with the private bits */
  474. set->allowed_protocols = CURLPROTO_ALL;
  475. set->redir_protocols = CURLPROTO_ALL & /* All except FILE, SCP and SMB */
  476. ~(CURLPROTO_FILE | CURLPROTO_SCP | CURLPROTO_SMB |
  477. CURLPROTO_SMBS);
  478. #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
  479. /*
  480. * disallow unprotected protection negotiation NEC reference implementation
  481. * seem not to follow rfc1961 section 4.3/4.4
  482. */
  483. set->socks5_gssapi_nec = FALSE;
  484. /* set default GSS-API service name */
  485. result = setstropt(&set->str[STRING_SOCKS5_GSSAPI_SERVICE],
  486. (char *) CURL_DEFAULT_SOCKS5_GSSAPI_SERVICE);
  487. if(result)
  488. return result;
  489. #endif
  490. /* This is our preferred CA cert bundle/path since install time */
  491. #if defined(CURL_CA_BUNDLE)
  492. result = setstropt(&set->str[STRING_SSL_CAFILE], (char *) CURL_CA_BUNDLE);
  493. #elif defined(CURL_CA_PATH)
  494. result = setstropt(&set->str[STRING_SSL_CAPATH], (char *) CURL_CA_PATH);
  495. #endif
  496. set->wildcardmatch = FALSE;
  497. set->chunk_bgn = ZERO_NULL;
  498. set->chunk_end = ZERO_NULL;
  499. /* tcp keepalives are disabled by default, but provide reasonable values for
  500. * the interval and idle times.
  501. */
  502. set->tcp_keepalive = FALSE;
  503. set->tcp_keepintvl = 60;
  504. set->tcp_keepidle = 60;
  505. set->ssl_enable_npn = TRUE;
  506. set->ssl_enable_alpn = TRUE;
  507. set->expect_100_timeout = 1000L; /* Wait for a second by default. */
  508. return result;
  509. }
  510. /**
  511. * Curl_open()
  512. *
  513. * @param curl is a pointer to a sessionhandle pointer that gets set by this
  514. * function.
  515. * @return CURLcode
  516. */
  517. CURLcode Curl_open(struct SessionHandle **curl)
  518. {
  519. CURLcode result;
  520. struct SessionHandle *data;
  521. /* Very simple start-up: alloc the struct, init it with zeroes and return */
  522. data = calloc(1, sizeof(struct SessionHandle));
  523. if(!data) {
  524. /* this is a very serious error */
  525. DEBUGF(fprintf(stderr, "Error: calloc of SessionHandle failed\n"));
  526. return CURLE_OUT_OF_MEMORY;
  527. }
  528. data->magic = CURLEASY_MAGIC_NUMBER;
  529. result = Curl_resolver_init(&data->state.resolver);
  530. if(result) {
  531. DEBUGF(fprintf(stderr, "Error: resolver_init failed\n"));
  532. free(data);
  533. return result;
  534. }
  535. /* We do some initial setup here, all those fields that can't be just 0 */
  536. data->state.headerbuff = malloc(HEADERSIZE);
  537. if(!data->state.headerbuff) {
  538. DEBUGF(fprintf(stderr, "Error: malloc of headerbuff failed\n"));
  539. result = CURLE_OUT_OF_MEMORY;
  540. }
  541. else {
  542. result = Curl_init_userdefined(&data->set);
  543. data->state.headersize=HEADERSIZE;
  544. Curl_convert_init(data);
  545. /* most recent connection is not yet defined */
  546. data->state.lastconnect = NULL;
  547. data->progress.flags |= PGRS_HIDE;
  548. data->state.current_speed = -1; /* init to negative == impossible */
  549. data->wildcard.state = CURLWC_INIT;
  550. data->wildcard.filelist = NULL;
  551. data->set.fnmatch = ZERO_NULL;
  552. data->set.maxconnects = DEFAULT_CONNCACHE_SIZE; /* for easy handles */
  553. }
  554. if(result) {
  555. Curl_resolver_cleanup(data->state.resolver);
  556. if(data->state.headerbuff)
  557. free(data->state.headerbuff);
  558. Curl_freeset(data);
  559. free(data);
  560. data = NULL;
  561. }
  562. else
  563. *curl = data;
  564. return result;
  565. }
  566. CURLcode Curl_setopt(struct SessionHandle *data, CURLoption option,
  567. va_list param)
  568. {
  569. char *argptr;
  570. CURLcode result = CURLE_OK;
  571. long arg;
  572. #ifndef CURL_DISABLE_HTTP
  573. curl_off_t bigsize;
  574. #endif
  575. switch(option) {
  576. case CURLOPT_DNS_CACHE_TIMEOUT:
  577. data->set.dns_cache_timeout = va_arg(param, long);
  578. break;
  579. case CURLOPT_DNS_USE_GLOBAL_CACHE:
  580. /* remember we want this enabled */
  581. arg = va_arg(param, long);
  582. data->set.global_dns_cache = (0 != arg)?TRUE:FALSE;
  583. break;
  584. case CURLOPT_SSL_CIPHER_LIST:
  585. /* set a list of cipher we want to use in the SSL connection */
  586. result = setstropt(&data->set.str[STRING_SSL_CIPHER_LIST],
  587. va_arg(param, char *));
  588. break;
  589. case CURLOPT_RANDOM_FILE:
  590. /*
  591. * This is the path name to a file that contains random data to seed
  592. * the random SSL stuff with. The file is only used for reading.
  593. */
  594. result = setstropt(&data->set.str[STRING_SSL_RANDOM_FILE],
  595. va_arg(param, char *));
  596. break;
  597. case CURLOPT_EGDSOCKET:
  598. /*
  599. * The Entropy Gathering Daemon socket pathname
  600. */
  601. result = setstropt(&data->set.str[STRING_SSL_EGDSOCKET],
  602. va_arg(param, char *));
  603. break;
  604. case CURLOPT_MAXCONNECTS:
  605. /*
  606. * Set the absolute number of maximum simultaneous alive connection that
  607. * libcurl is allowed to have.
  608. */
  609. data->set.maxconnects = va_arg(param, long);
  610. break;
  611. case CURLOPT_FORBID_REUSE:
  612. /*
  613. * When this transfer is done, it must not be left to be reused by a
  614. * subsequent transfer but shall be closed immediately.
  615. */
  616. data->set.reuse_forbid = (0 != va_arg(param, long))?TRUE:FALSE;
  617. break;
  618. case CURLOPT_FRESH_CONNECT:
  619. /*
  620. * This transfer shall not use a previously cached connection but
  621. * should be made with a fresh new connect!
  622. */
  623. data->set.reuse_fresh = (0 != va_arg(param, long))?TRUE:FALSE;
  624. break;
  625. case CURLOPT_VERBOSE:
  626. /*
  627. * Verbose means infof() calls that give a lot of information about
  628. * the connection and transfer procedures as well as internal choices.
  629. */
  630. data->set.verbose = (0 != va_arg(param, long))?TRUE:FALSE;
  631. break;
  632. case CURLOPT_HEADER:
  633. /*
  634. * Set to include the header in the general data output stream.
  635. */
  636. data->set.include_header = (0 != va_arg(param, long))?TRUE:FALSE;
  637. break;
  638. case CURLOPT_NOPROGRESS:
  639. /*
  640. * Shut off the internal supported progress meter
  641. */
  642. data->set.hide_progress = (0 != va_arg(param, long))?TRUE:FALSE;
  643. if(data->set.hide_progress)
  644. data->progress.flags |= PGRS_HIDE;
  645. else
  646. data->progress.flags &= ~PGRS_HIDE;
  647. break;
  648. case CURLOPT_NOBODY:
  649. /*
  650. * Do not include the body part in the output data stream.
  651. */
  652. data->set.opt_no_body = (0 != va_arg(param, long))?TRUE:FALSE;
  653. break;
  654. case CURLOPT_FAILONERROR:
  655. /*
  656. * Don't output the >=400 error code HTML-page, but instead only
  657. * return error.
  658. */
  659. data->set.http_fail_on_error = (0 != va_arg(param, long))?TRUE:FALSE;
  660. break;
  661. case CURLOPT_UPLOAD:
  662. case CURLOPT_PUT:
  663. /*
  664. * We want to sent data to the remote host. If this is HTTP, that equals
  665. * using the PUT request.
  666. */
  667. data->set.upload = (0 != va_arg(param, long))?TRUE:FALSE;
  668. if(data->set.upload) {
  669. /* If this is HTTP, PUT is what's needed to "upload" */
  670. data->set.httpreq = HTTPREQ_PUT;
  671. data->set.opt_no_body = FALSE; /* this is implied */
  672. }
  673. else
  674. /* In HTTP, the opposite of upload is GET (unless NOBODY is true as
  675. then this can be changed to HEAD later on) */
  676. data->set.httpreq = HTTPREQ_GET;
  677. break;
  678. case CURLOPT_FILETIME:
  679. /*
  680. * Try to get the file time of the remote document. The time will
  681. * later (possibly) become available using curl_easy_getinfo().
  682. */
  683. data->set.get_filetime = (0 != va_arg(param, long))?TRUE:FALSE;
  684. break;
  685. case CURLOPT_FTP_CREATE_MISSING_DIRS:
  686. /*
  687. * An FTP option that modifies an upload to create missing directories on
  688. * the server.
  689. */
  690. switch(va_arg(param, long)) {
  691. case 0:
  692. data->set.ftp_create_missing_dirs = 0;
  693. break;
  694. case 1:
  695. data->set.ftp_create_missing_dirs = 1;
  696. break;
  697. case 2:
  698. data->set.ftp_create_missing_dirs = 2;
  699. break;
  700. default:
  701. /* reserve other values for future use */
  702. result = CURLE_UNKNOWN_OPTION;
  703. break;
  704. }
  705. break;
  706. case CURLOPT_SERVER_RESPONSE_TIMEOUT:
  707. /*
  708. * Option that specifies how quickly an server response must be obtained
  709. * before it is considered failure. For pingpong protocols.
  710. */
  711. data->set.server_response_timeout = va_arg( param , long ) * 1000;
  712. break;
  713. case CURLOPT_TFTP_BLKSIZE:
  714. /*
  715. * TFTP option that specifies the block size to use for data transmission
  716. */
  717. data->set.tftp_blksize = va_arg(param, long);
  718. break;
  719. case CURLOPT_DIRLISTONLY:
  720. /*
  721. * An option that changes the command to one that asks for a list
  722. * only, no file info details.
  723. */
  724. data->set.ftp_list_only = (0 != va_arg(param, long))?TRUE:FALSE;
  725. break;
  726. case CURLOPT_APPEND:
  727. /*
  728. * We want to upload and append to an existing file.
  729. */
  730. data->set.ftp_append = (0 != va_arg(param, long))?TRUE:FALSE;
  731. break;
  732. case CURLOPT_FTP_FILEMETHOD:
  733. /*
  734. * How do access files over FTP.
  735. */
  736. data->set.ftp_filemethod = (curl_ftpfile)va_arg(param, long);
  737. break;
  738. case CURLOPT_NETRC:
  739. /*
  740. * Parse the $HOME/.netrc file
  741. */
  742. data->set.use_netrc = (enum CURL_NETRC_OPTION)va_arg(param, long);
  743. break;
  744. case CURLOPT_NETRC_FILE:
  745. /*
  746. * Use this file instead of the $HOME/.netrc file
  747. */
  748. result = setstropt(&data->set.str[STRING_NETRC_FILE],
  749. va_arg(param, char *));
  750. break;
  751. case CURLOPT_TRANSFERTEXT:
  752. /*
  753. * This option was previously named 'FTPASCII'. Renamed to work with
  754. * more protocols than merely FTP.
  755. *
  756. * Transfer using ASCII (instead of BINARY).
  757. */
  758. data->set.prefer_ascii = (0 != va_arg(param, long))?TRUE:FALSE;
  759. break;
  760. case CURLOPT_TIMECONDITION:
  761. /*
  762. * Set HTTP time condition. This must be one of the defines in the
  763. * curl/curl.h header file.
  764. */
  765. data->set.timecondition = (curl_TimeCond)va_arg(param, long);
  766. break;
  767. case CURLOPT_TIMEVALUE:
  768. /*
  769. * This is the value to compare with the remote document with the
  770. * method set with CURLOPT_TIMECONDITION
  771. */
  772. data->set.timevalue = (time_t)va_arg(param, long);
  773. break;
  774. case CURLOPT_SSLVERSION:
  775. /*
  776. * Set explicit SSL version to try to connect with, as some SSL
  777. * implementations are lame.
  778. */
  779. data->set.ssl.version = va_arg(param, long);
  780. break;
  781. #ifndef CURL_DISABLE_HTTP
  782. case CURLOPT_AUTOREFERER:
  783. /*
  784. * Switch on automatic referer that gets set if curl follows locations.
  785. */
  786. data->set.http_auto_referer = (0 != va_arg(param, long))?TRUE:FALSE;
  787. break;
  788. case CURLOPT_ACCEPT_ENCODING:
  789. /*
  790. * String to use at the value of Accept-Encoding header.
  791. *
  792. * If the encoding is set to "" we use an Accept-Encoding header that
  793. * encompasses all the encodings we support.
  794. * If the encoding is set to NULL we don't send an Accept-Encoding header
  795. * and ignore an received Content-Encoding header.
  796. *
  797. */
  798. argptr = va_arg(param, char *);
  799. result = setstropt(&data->set.str[STRING_ENCODING],
  800. (argptr && !*argptr)?
  801. (char *) ALL_CONTENT_ENCODINGS: argptr);
  802. break;
  803. case CURLOPT_TRANSFER_ENCODING:
  804. data->set.http_transfer_encoding = (0 != va_arg(param, long))?TRUE:FALSE;
  805. break;
  806. case CURLOPT_FOLLOWLOCATION:
  807. /*
  808. * Follow Location: header hints on a HTTP-server.
  809. */
  810. data->set.http_follow_location = (0 != va_arg(param, long))?TRUE:FALSE;
  811. break;
  812. case CURLOPT_UNRESTRICTED_AUTH:
  813. /*
  814. * Send authentication (user+password) when following locations, even when
  815. * hostname changed.
  816. */
  817. data->set.http_disable_hostname_check_before_authentication =
  818. (0 != va_arg(param, long))?TRUE:FALSE;
  819. break;
  820. case CURLOPT_MAXREDIRS:
  821. /*
  822. * The maximum amount of hops you allow curl to follow Location:
  823. * headers. This should mostly be used to detect never-ending loops.
  824. */
  825. data->set.maxredirs = va_arg(param, long);
  826. break;
  827. case CURLOPT_POSTREDIR:
  828. {
  829. /*
  830. * Set the behaviour of POST when redirecting
  831. * CURL_REDIR_GET_ALL - POST is changed to GET after 301 and 302
  832. * CURL_REDIR_POST_301 - POST is kept as POST after 301
  833. * CURL_REDIR_POST_302 - POST is kept as POST after 302
  834. * CURL_REDIR_POST_303 - POST is kept as POST after 303
  835. * CURL_REDIR_POST_ALL - POST is kept as POST after 301, 302 and 303
  836. * other - POST is kept as POST after 301 and 302
  837. */
  838. int postRedir = curlx_sltosi(va_arg(param, long));
  839. data->set.keep_post = postRedir & CURL_REDIR_POST_ALL;
  840. }
  841. break;
  842. case CURLOPT_POST:
  843. /* Does this option serve a purpose anymore? Yes it does, when
  844. CURLOPT_POSTFIELDS isn't used and the POST data is read off the
  845. callback! */
  846. if(va_arg(param, long)) {
  847. data->set.httpreq = HTTPREQ_POST;
  848. data->set.opt_no_body = FALSE; /* this is implied */
  849. }
  850. else
  851. data->set.httpreq = HTTPREQ_GET;
  852. break;
  853. case CURLOPT_COPYPOSTFIELDS:
  854. /*
  855. * A string with POST data. Makes curl HTTP POST. Even if it is NULL.
  856. * If needed, CURLOPT_POSTFIELDSIZE must have been set prior to
  857. * CURLOPT_COPYPOSTFIELDS and not altered later.
  858. */
  859. argptr = va_arg(param, char *);
  860. if(!argptr || data->set.postfieldsize == -1)
  861. result = setstropt(&data->set.str[STRING_COPYPOSTFIELDS], argptr);
  862. else {
  863. /*
  864. * Check that requested length does not overflow the size_t type.
  865. */
  866. if((data->set.postfieldsize < 0) ||
  867. ((sizeof(curl_off_t) != sizeof(size_t)) &&
  868. (data->set.postfieldsize > (curl_off_t)((size_t)-1))))
  869. result = CURLE_OUT_OF_MEMORY;
  870. else {
  871. char * p;
  872. (void) setstropt(&data->set.str[STRING_COPYPOSTFIELDS], NULL);
  873. /* Allocate even when size == 0. This satisfies the need of possible
  874. later address compare to detect the COPYPOSTFIELDS mode, and
  875. to mark that postfields is used rather than read function or
  876. form data.
  877. */
  878. p = malloc((size_t)(data->set.postfieldsize?
  879. data->set.postfieldsize:1));
  880. if(!p)
  881. result = CURLE_OUT_OF_MEMORY;
  882. else {
  883. if(data->set.postfieldsize)
  884. memcpy(p, argptr, (size_t)data->set.postfieldsize);
  885. data->set.str[STRING_COPYPOSTFIELDS] = p;
  886. }
  887. }
  888. }
  889. data->set.postfields = data->set.str[STRING_COPYPOSTFIELDS];
  890. data->set.httpreq = HTTPREQ_POST;
  891. break;
  892. case CURLOPT_POSTFIELDS:
  893. /*
  894. * Like above, but use static data instead of copying it.
  895. */
  896. data->set.postfields = va_arg(param, void *);
  897. /* Release old copied data. */
  898. (void) setstropt(&data->set.str[STRING_COPYPOSTFIELDS], NULL);
  899. data->set.httpreq = HTTPREQ_POST;
  900. break;
  901. case CURLOPT_POSTFIELDSIZE:
  902. /*
  903. * The size of the POSTFIELD data to prevent libcurl to do strlen() to
  904. * figure it out. Enables binary posts.
  905. */
  906. bigsize = va_arg(param, long);
  907. if(data->set.postfieldsize < bigsize &&
  908. data->set.postfields == data->set.str[STRING_COPYPOSTFIELDS]) {
  909. /* Previous CURLOPT_COPYPOSTFIELDS is no longer valid. */
  910. (void) setstropt(&data->set.str[STRING_COPYPOSTFIELDS], NULL);
  911. data->set.postfields = NULL;
  912. }
  913. data->set.postfieldsize = bigsize;
  914. break;
  915. case CURLOPT_POSTFIELDSIZE_LARGE:
  916. /*
  917. * The size of the POSTFIELD data to prevent libcurl to do strlen() to
  918. * figure it out. Enables binary posts.
  919. */
  920. bigsize = va_arg(param, curl_off_t);
  921. if(data->set.postfieldsize < bigsize &&
  922. data->set.postfields == data->set.str[STRING_COPYPOSTFIELDS]) {
  923. /* Previous CURLOPT_COPYPOSTFIELDS is no longer valid. */
  924. (void) setstropt(&data->set.str[STRING_COPYPOSTFIELDS], NULL);
  925. data->set.postfields = NULL;
  926. }
  927. data->set.postfieldsize = bigsize;
  928. break;
  929. case CURLOPT_HTTPPOST:
  930. /*
  931. * Set to make us do HTTP POST
  932. */
  933. data->set.httppost = va_arg(param, struct curl_httppost *);
  934. data->set.httpreq = HTTPREQ_POST_FORM;
  935. data->set.opt_no_body = FALSE; /* this is implied */
  936. break;
  937. case CURLOPT_REFERER:
  938. /*
  939. * String to set in the HTTP Referer: field.
  940. */
  941. if(data->change.referer_alloc) {
  942. Curl_safefree(data->change.referer);
  943. data->change.referer_alloc = FALSE;
  944. }
  945. result = setstropt(&data->set.str[STRING_SET_REFERER],
  946. va_arg(param, char *));
  947. data->change.referer = data->set.str[STRING_SET_REFERER];
  948. break;
  949. case CURLOPT_USERAGENT:
  950. /*
  951. * String to use in the HTTP User-Agent field
  952. */
  953. result = setstropt(&data->set.str[STRING_USERAGENT],
  954. va_arg(param, char *));
  955. break;
  956. case CURLOPT_HTTPHEADER:
  957. /*
  958. * Set a list with HTTP headers to use (or replace internals with)
  959. */
  960. data->set.headers = va_arg(param, struct curl_slist *);
  961. break;
  962. case CURLOPT_PROXYHEADER:
  963. /*
  964. * Set a list with proxy headers to use (or replace internals with)
  965. *
  966. * Since CURLOPT_HTTPHEADER was the only way to set HTTP headers for a
  967. * long time we remain doing it this way until CURLOPT_PROXYHEADER is
  968. * used. As soon as this option has been used, if set to anything but
  969. * NULL, custom headers for proxies are only picked from this list.
  970. *
  971. * Set this option to NULL to restore the previous behavior.
  972. */
  973. data->set.proxyheaders = va_arg(param, struct curl_slist *);
  974. break;
  975. case CURLOPT_HEADEROPT:
  976. /*
  977. * Set header option.
  978. */
  979. arg = va_arg(param, long);
  980. data->set.sep_headers = (arg & CURLHEADER_SEPARATE)? TRUE: FALSE;
  981. break;
  982. case CURLOPT_HTTP200ALIASES:
  983. /*
  984. * Set a list of aliases for HTTP 200 in response header
  985. */
  986. data->set.http200aliases = va_arg(param, struct curl_slist *);
  987. break;
  988. #if !defined(CURL_DISABLE_COOKIES)
  989. case CURLOPT_COOKIE:
  990. /*
  991. * Cookie string to send to the remote server in the request.
  992. */
  993. result = setstropt(&data->set.str[STRING_COOKIE],
  994. va_arg(param, char *));
  995. break;
  996. case CURLOPT_COOKIEFILE:
  997. /*
  998. * Set cookie file to read and parse. Can be used multiple times.
  999. */
  1000. argptr = (char *)va_arg(param, void *);
  1001. if(argptr) {
  1002. struct curl_slist *cl;
  1003. /* append the cookie file name to the list of file names, and deal with
  1004. them later */
  1005. cl = curl_slist_append(data->change.cookielist, argptr);
  1006. if(!cl) {
  1007. curl_slist_free_all(data->change.cookielist);
  1008. data->change.cookielist = NULL;
  1009. return CURLE_OUT_OF_MEMORY;
  1010. }
  1011. data->change.cookielist = cl; /* store the list for later use */
  1012. }
  1013. break;
  1014. case CURLOPT_COOKIEJAR:
  1015. /*
  1016. * Set cookie file name to dump all cookies to when we're done.
  1017. */
  1018. {
  1019. struct CookieInfo *newcookies;
  1020. result = setstropt(&data->set.str[STRING_COOKIEJAR],
  1021. va_arg(param, char *));
  1022. /*
  1023. * Activate the cookie parser. This may or may not already
  1024. * have been made.
  1025. */
  1026. newcookies = Curl_cookie_init(data, NULL, data->cookies,
  1027. data->set.cookiesession);
  1028. if(!newcookies)
  1029. result = CURLE_OUT_OF_MEMORY;
  1030. data->cookies = newcookies;
  1031. }
  1032. break;
  1033. case CURLOPT_COOKIESESSION:
  1034. /*
  1035. * Set this option to TRUE to start a new "cookie session". It will
  1036. * prevent the forthcoming read-cookies-from-file actions to accept
  1037. * cookies that are marked as being session cookies, as they belong to a
  1038. * previous session.
  1039. *
  1040. * In the original Netscape cookie spec, "session cookies" are cookies
  1041. * with no expire date set. RFC2109 describes the same action if no
  1042. * 'Max-Age' is set and RFC2965 includes the RFC2109 description and adds
  1043. * a 'Discard' action that can enforce the discard even for cookies that
  1044. * have a Max-Age.
  1045. *
  1046. * We run mostly with the original cookie spec, as hardly anyone implements
  1047. * anything else.
  1048. */
  1049. data->set.cookiesession = (0 != va_arg(param, long))?TRUE:FALSE;
  1050. break;
  1051. case CURLOPT_COOKIELIST:
  1052. argptr = va_arg(param, char *);
  1053. if(argptr == NULL)
  1054. break;
  1055. if(Curl_raw_equal(argptr, "ALL")) {
  1056. /* clear all cookies */
  1057. Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
  1058. Curl_cookie_clearall(data->cookies);
  1059. Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
  1060. }
  1061. else if(Curl_raw_equal(argptr, "SESS")) {
  1062. /* clear session cookies */
  1063. Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
  1064. Curl_cookie_clearsess(data->cookies);
  1065. Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
  1066. }
  1067. else if(Curl_raw_equal(argptr, "FLUSH")) {
  1068. /* flush cookies to file, takes care of the locking */
  1069. Curl_flush_cookies(data, 0);
  1070. }
  1071. else if(Curl_raw_equal(argptr, "RELOAD")) {
  1072. /* reload cookies from file */
  1073. Curl_cookie_loadfiles(data);
  1074. break;
  1075. }
  1076. else {
  1077. if(!data->cookies)
  1078. /* if cookie engine was not running, activate it */
  1079. data->cookies = Curl_cookie_init(data, NULL, NULL, TRUE);
  1080. argptr = strdup(argptr);
  1081. if(!argptr || !data->cookies) {
  1082. result = CURLE_OUT_OF_MEMORY;
  1083. Curl_safefree(argptr);
  1084. }
  1085. else {
  1086. Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
  1087. if(checkprefix("Set-Cookie:", argptr))
  1088. /* HTTP Header format line */
  1089. Curl_cookie_add(data, data->cookies, TRUE, argptr + 11, NULL, NULL);
  1090. else
  1091. /* Netscape format line */
  1092. Curl_cookie_add(data, data->cookies, FALSE, argptr, NULL, NULL);
  1093. Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
  1094. free(argptr);
  1095. }
  1096. }
  1097. break;
  1098. #endif /* CURL_DISABLE_COOKIES */
  1099. case CURLOPT_HTTPGET:
  1100. /*
  1101. * Set to force us do HTTP GET
  1102. */
  1103. if(va_arg(param, long)) {
  1104. data->set.httpreq = HTTPREQ_GET;
  1105. data->set.upload = FALSE; /* switch off upload */
  1106. data->set.opt_no_body = FALSE; /* this is implied */
  1107. }
  1108. break;
  1109. case CURLOPT_HTTP_VERSION:
  1110. /*
  1111. * This sets a requested HTTP version to be used. The value is one of
  1112. * the listed enums in curl/curl.h.
  1113. */
  1114. arg = va_arg(param, long);
  1115. #ifndef USE_NGHTTP2
  1116. if(arg == CURL_HTTP_VERSION_2_0)
  1117. return CURLE_UNSUPPORTED_PROTOCOL;
  1118. #endif
  1119. data->set.httpversion = arg;
  1120. break;
  1121. case CURLOPT_HTTPAUTH:
  1122. /*
  1123. * Set HTTP Authentication type BITMASK.
  1124. */
  1125. {
  1126. int bitcheck;
  1127. bool authbits;
  1128. unsigned long auth = va_arg(param, unsigned long);
  1129. if(auth == CURLAUTH_NONE) {
  1130. data->set.httpauth = auth;
  1131. break;
  1132. }
  1133. /* the DIGEST_IE bit is only used to set a special marker, for all the
  1134. rest we need to handle it as normal DIGEST */
  1135. data->state.authhost.iestyle = (auth & CURLAUTH_DIGEST_IE)?TRUE:FALSE;
  1136. if(auth & CURLAUTH_DIGEST_IE) {
  1137. auth |= CURLAUTH_DIGEST; /* set standard digest bit */
  1138. auth &= ~CURLAUTH_DIGEST_IE; /* unset ie digest bit */
  1139. }
  1140. /* switch off bits we can't support */
  1141. #ifndef USE_NTLM
  1142. auth &= ~CURLAUTH_NTLM; /* no NTLM support */
  1143. auth &= ~CURLAUTH_NTLM_WB; /* no NTLM_WB support */
  1144. #elif !defined(NTLM_WB_ENABLED)
  1145. auth &= ~CURLAUTH_NTLM_WB; /* no NTLM_WB support */
  1146. #endif
  1147. #ifndef USE_SPNEGO
  1148. auth &= ~CURLAUTH_NEGOTIATE; /* no Negotiate (SPNEGO) auth without
  1149. GSS-API or SSPI */
  1150. #endif
  1151. /* check if any auth bit lower than CURLAUTH_ONLY is still set */
  1152. bitcheck = 0;
  1153. authbits = FALSE;
  1154. while(bitcheck < 31) {
  1155. if(auth & (1UL << bitcheck++)) {
  1156. authbits = TRUE;
  1157. break;
  1158. }
  1159. }
  1160. if(!authbits)
  1161. return CURLE_NOT_BUILT_IN; /* no supported types left! */
  1162. data->set.httpauth = auth;
  1163. }
  1164. break;
  1165. case CURLOPT_EXPECT_100_TIMEOUT_MS:
  1166. /*
  1167. * Time to wait for a response to a HTTP request containing an
  1168. * Expect: 100-continue header before sending the data anyway.
  1169. */
  1170. data->set.expect_100_timeout = va_arg(param, long);
  1171. break;
  1172. #endif /* CURL_DISABLE_HTTP */
  1173. case CURLOPT_CUSTOMREQUEST:
  1174. /*
  1175. * Set a custom string to use as request
  1176. */
  1177. result = setstropt(&data->set.str[STRING_CUSTOMREQUEST],
  1178. va_arg(param, char *));
  1179. /* we don't set
  1180. data->set.httpreq = HTTPREQ_CUSTOM;
  1181. here, we continue as if we were using the already set type
  1182. and this just changes the actual request keyword */
  1183. break;
  1184. #ifndef CURL_DISABLE_PROXY
  1185. case CURLOPT_HTTPPROXYTUNNEL:
  1186. /*
  1187. * Tunnel operations through the proxy instead of normal proxy use
  1188. */
  1189. data->set.tunnel_thru_httpproxy = (0 != va_arg(param, long))?TRUE:FALSE;
  1190. break;
  1191. case CURLOPT_PROXYPORT:
  1192. /*
  1193. * Explicitly set HTTP proxy port number.
  1194. */
  1195. data->set.proxyport = va_arg(param, long);
  1196. break;
  1197. case CURLOPT_PROXYAUTH:
  1198. /*
  1199. * Set HTTP Authentication type BITMASK.
  1200. */
  1201. {
  1202. int bitcheck;
  1203. bool authbits;
  1204. unsigned long auth = va_arg(param, unsigned long);
  1205. if(auth == CURLAUTH_NONE) {
  1206. data->set.proxyauth = auth;
  1207. break;
  1208. }
  1209. /* the DIGEST_IE bit is only used to set a special marker, for all the
  1210. rest we need to handle it as normal DIGEST */
  1211. data->state.authproxy.iestyle = (auth & CURLAUTH_DIGEST_IE)?TRUE:FALSE;
  1212. if(auth & CURLAUTH_DIGEST_IE) {
  1213. auth |= CURLAUTH_DIGEST; /* set standard digest bit */
  1214. auth &= ~CURLAUTH_DIGEST_IE; /* unset ie digest bit */
  1215. }
  1216. /* switch off bits we can't support */
  1217. #ifndef USE_NTLM
  1218. auth &= ~CURLAUTH_NTLM; /* no NTLM support */
  1219. auth &= ~CURLAUTH_NTLM_WB; /* no NTLM_WB support */
  1220. #elif !defined(NTLM_WB_ENABLED)
  1221. auth &= ~CURLAUTH_NTLM_WB; /* no NTLM_WB support */
  1222. #endif
  1223. #ifndef USE_SPNEGO
  1224. auth &= ~CURLAUTH_NEGOTIATE; /* no Negotiate (SPNEGO) auth without
  1225. GSS-API or SSPI */
  1226. #endif
  1227. /* check if any auth bit lower than CURLAUTH_ONLY is still set */
  1228. bitcheck = 0;
  1229. authbits = FALSE;
  1230. while(bitcheck < 31) {
  1231. if(auth & (1UL << bitcheck++)) {
  1232. authbits = TRUE;
  1233. break;
  1234. }
  1235. }
  1236. if(!authbits)
  1237. return CURLE_NOT_BUILT_IN; /* no supported types left! */
  1238. data->set.proxyauth = auth;
  1239. }
  1240. break;
  1241. case CURLOPT_PROXY:
  1242. /*
  1243. * Set proxy server:port to use as HTTP proxy.
  1244. *
  1245. * If the proxy is set to "" we explicitly say that we don't want to use a
  1246. * proxy (even though there might be environment variables saying so).
  1247. *
  1248. * Setting it to NULL, means no proxy but allows the environment variables
  1249. * to decide for us.
  1250. */
  1251. result = setstropt(&data->set.str[STRING_PROXY],
  1252. va_arg(param, char *));
  1253. break;
  1254. case CURLOPT_PROXYTYPE:
  1255. /*
  1256. * Set proxy type. HTTP/HTTP_1_0/SOCKS4/SOCKS4a/SOCKS5/SOCKS5_HOSTNAME
  1257. */
  1258. data->set.proxytype = (curl_proxytype)va_arg(param, long);
  1259. break;
  1260. case CURLOPT_PROXY_TRANSFER_MODE:
  1261. /*
  1262. * set transfer mode (;type=<a|i>) when doing FTP via an HTTP proxy
  1263. */
  1264. switch (va_arg(param, long)) {
  1265. case 0:
  1266. data->set.proxy_transfer_mode = FALSE;
  1267. break;
  1268. case 1:
  1269. data->set.proxy_transfer_mode = TRUE;
  1270. break;
  1271. default:
  1272. /* reserve other values for future use */
  1273. result = CURLE_UNKNOWN_OPTION;
  1274. break;
  1275. }
  1276. break;
  1277. #endif /* CURL_DISABLE_PROXY */
  1278. #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
  1279. case CURLOPT_SOCKS5_GSSAPI_SERVICE:
  1280. /*
  1281. * Set GSS-API service name
  1282. */
  1283. result = setstropt(&data->set.str[STRING_SOCKS5_GSSAPI_SERVICE],
  1284. va_arg(param, char *));
  1285. break;
  1286. case CURLOPT_SOCKS5_GSSAPI_NEC:
  1287. /*
  1288. * set flag for nec socks5 support
  1289. */
  1290. data->set.socks5_gssapi_nec = (0 != va_arg(param, long))?TRUE:FALSE;
  1291. break;
  1292. #endif
  1293. case CURLOPT_HEADERDATA:
  1294. /*
  1295. * Custom pointer to pass the header write callback function
  1296. */
  1297. data->set.writeheader = (void *)va_arg(param, void *);
  1298. break;
  1299. case CURLOPT_ERRORBUFFER:
  1300. /*
  1301. * Error buffer provided by the caller to get the human readable
  1302. * error string in.
  1303. */
  1304. data->set.errorbuffer = va_arg(param, char *);
  1305. break;
  1306. case CURLOPT_WRITEDATA:
  1307. /*
  1308. * FILE pointer to write to. Or possibly
  1309. * used as argument to the write callback.
  1310. */
  1311. data->set.out = va_arg(param, void *);
  1312. break;
  1313. case CURLOPT_FTPPORT:
  1314. /*
  1315. * Use FTP PORT, this also specifies which IP address to use
  1316. */
  1317. result = setstropt(&data->set.str[STRING_FTPPORT],
  1318. va_arg(param, char *));
  1319. data->set.ftp_use_port = (NULL != data->set.str[STRING_FTPPORT]) ?
  1320. TRUE:FALSE;
  1321. break;
  1322. case CURLOPT_FTP_USE_EPRT:
  1323. data->set.ftp_use_eprt = (0 != va_arg(param, long))?TRUE:FALSE;
  1324. break;
  1325. case CURLOPT_FTP_USE_EPSV:
  1326. data->set.ftp_use_epsv = (0 != va_arg(param, long))?TRUE:FALSE;
  1327. break;
  1328. case CURLOPT_FTP_USE_PRET:
  1329. data->set.ftp_use_pret = (0 != va_arg(param, long))?TRUE:FALSE;
  1330. break;
  1331. case CURLOPT_FTP_SSL_CCC:
  1332. data->set.ftp_ccc = (curl_ftpccc)va_arg(param, long);
  1333. break;
  1334. case CURLOPT_FTP_SKIP_PASV_IP:
  1335. /*
  1336. * Enable or disable FTP_SKIP_PASV_IP, which will disable/enable the
  1337. * bypass of the IP address in PASV responses.
  1338. */
  1339. data->set.ftp_skip_ip = (0 != va_arg(param, long))?TRUE:FALSE;
  1340. break;
  1341. case CURLOPT_READDATA:
  1342. /*
  1343. * FILE pointer to read the file to be uploaded from. Or possibly
  1344. * used as argument to the read callback.
  1345. */
  1346. data->set.in = va_arg(param, void *);
  1347. break;
  1348. case CURLOPT_INFILESIZE:
  1349. /*
  1350. * If known, this should inform curl about the file size of the
  1351. * to-be-uploaded file.
  1352. */
  1353. data->set.filesize = va_arg(param, long);
  1354. break;
  1355. case CURLOPT_INFILESIZE_LARGE:
  1356. /*
  1357. * If known, this should inform curl about the file size of the
  1358. * to-be-uploaded file.
  1359. */
  1360. data->set.filesize = va_arg(param, curl_off_t);
  1361. break;
  1362. case CURLOPT_LOW_SPEED_LIMIT:
  1363. /*
  1364. * The low speed limit that if transfers are below this for
  1365. * CURLOPT_LOW_SPEED_TIME, the transfer is aborted.
  1366. */
  1367. data->set.low_speed_limit=va_arg(param, long);
  1368. break;
  1369. case CURLOPT_MAX_SEND_SPEED_LARGE:
  1370. /*
  1371. * When transfer uploads are faster then CURLOPT_MAX_SEND_SPEED_LARGE
  1372. * bytes per second the transfer is throttled..
  1373. */
  1374. data->set.max_send_speed=va_arg(param, curl_off_t);
  1375. break;
  1376. case CURLOPT_MAX_RECV_SPEED_LARGE:
  1377. /*
  1378. * When receiving data faster than CURLOPT_MAX_RECV_SPEED_LARGE bytes per
  1379. * second the transfer is throttled..
  1380. */
  1381. data->set.max_recv_speed=va_arg(param, curl_off_t);
  1382. break;
  1383. case CURLOPT_LOW_SPEED_TIME:
  1384. /*
  1385. * The low speed time that if transfers are below the set
  1386. * CURLOPT_LOW_SPEED_LIMIT during this time, the transfer is aborted.
  1387. */
  1388. data->set.low_speed_time=va_arg(param, long);
  1389. break;
  1390. case CURLOPT_URL:
  1391. /*
  1392. * The URL to fetch.
  1393. */
  1394. if(data->change.url_alloc) {
  1395. /* the already set URL is allocated, free it first! */
  1396. Curl_safefree(data->change.url);
  1397. data->change.url_alloc = FALSE;
  1398. }
  1399. result = setstropt(&data->set.str[STRING_SET_URL],
  1400. va_arg(param, char *));
  1401. data->change.url = data->set.str[STRING_SET_URL];
  1402. break;
  1403. case CURLOPT_PORT:
  1404. /*
  1405. * The port number to use when getting the URL
  1406. */
  1407. data->set.use_port = va_arg(param, long);
  1408. break;
  1409. case CURLOPT_TIMEOUT:
  1410. /*
  1411. * The maximum time you allow curl to use for a single transfer
  1412. * operation.
  1413. */
  1414. data->set.timeout = va_arg(param, long) * 1000L;
  1415. break;
  1416. case CURLOPT_TIMEOUT_MS:
  1417. data->set.timeout = va_arg(param, long);
  1418. break;
  1419. case CURLOPT_CONNECTTIMEOUT:
  1420. /*
  1421. * The maximum time you allow curl to use to connect.
  1422. */
  1423. data->set.connecttimeout = va_arg(param, long) * 1000L;
  1424. break;
  1425. case CURLOPT_CONNECTTIMEOUT_MS:
  1426. data->set.connecttimeout = va_arg(param, long);
  1427. break;
  1428. case CURLOPT_ACCEPTTIMEOUT_MS:
  1429. /*
  1430. * The maximum time you allow curl to wait for server connect
  1431. */
  1432. data->set.accepttimeout = va_arg(param, long);
  1433. break;
  1434. case CURLOPT_USERPWD:
  1435. /*
  1436. * user:password to use in the operation
  1437. */
  1438. result = setstropt_userpwd(va_arg(param, char *),
  1439. &data->set.str[STRING_USERNAME],
  1440. &data->set.str[STRING_PASSWORD]);
  1441. break;
  1442. case CURLOPT_USERNAME:
  1443. /*
  1444. * authentication user name to use in the operation
  1445. */
  1446. result = setstropt(&data->set.str[STRING_USERNAME],
  1447. va_arg(param, char *));
  1448. break;
  1449. case CURLOPT_PASSWORD:
  1450. /*
  1451. * authentication password to use in the operation
  1452. */
  1453. result = setstropt(&data->set.str[STRING_PASSWORD],
  1454. va_arg(param, char *));
  1455. break;
  1456. case CURLOPT_LOGIN_OPTIONS:
  1457. /*
  1458. * authentication options to use in the operation
  1459. */
  1460. result = setstropt(&data->set.str[STRING_OPTIONS],
  1461. va_arg(param, char *));
  1462. break;
  1463. case CURLOPT_XOAUTH2_BEARER:
  1464. /*
  1465. * XOAUTH2 bearer token to use in the operation
  1466. */
  1467. result = setstropt(&data->set.str[STRING_BEARER],
  1468. va_arg(param, char *));
  1469. break;
  1470. case CURLOPT_POSTQUOTE:
  1471. /*
  1472. * List of RAW FTP commands to use after a transfer
  1473. */
  1474. data->set.postquote = va_arg(param, struct curl_slist *);
  1475. break;
  1476. case CURLOPT_PREQUOTE:
  1477. /*
  1478. * List of RAW FTP commands to use prior to RETR (Wesley Laxton)
  1479. */
  1480. data->set.prequote = va_arg(param, struct curl_slist *);
  1481. break;
  1482. case CURLOPT_QUOTE:
  1483. /*
  1484. * List of RAW FTP commands to use before a transfer
  1485. */
  1486. data->set.quote = va_arg(param, struct curl_slist *);
  1487. break;
  1488. case CURLOPT_RESOLVE:
  1489. /*
  1490. * List of NAME:[address] names to populate the DNS cache with
  1491. * Prefix the NAME with dash (-) to _remove_ the name from the cache.
  1492. *
  1493. * Names added with this API will remain in the cache until explicitly
  1494. * removed or the handle is cleaned up.
  1495. *
  1496. * This API can remove any name from the DNS cache, but only entries
  1497. * that aren't actually in use right now will be pruned immediately.
  1498. */
  1499. data->set.resolve = va_arg(param, struct curl_slist *);
  1500. data->change.resolve = data->set.resolve;
  1501. break;
  1502. case CURLOPT_PROGRESSFUNCTION:
  1503. /*
  1504. * Progress callback function
  1505. */
  1506. data->set.fprogress = va_arg(param, curl_progress_callback);
  1507. if(data->set.fprogress)
  1508. data->progress.callback = TRUE; /* no longer internal */
  1509. else
  1510. data->progress.callback = FALSE; /* NULL enforces internal */
  1511. break;
  1512. case CURLOPT_XFERINFOFUNCTION:
  1513. /*
  1514. * Transfer info callback function
  1515. */
  1516. data->set.fxferinfo = va_arg(param, curl_xferinfo_callback);
  1517. if(data->set.fxferinfo)
  1518. data->progress.callback = TRUE; /* no longer internal */
  1519. else
  1520. data->progress.callback = FALSE; /* NULL enforces internal */
  1521. break;
  1522. case CURLOPT_PROGRESSDATA:
  1523. /*
  1524. * Custom client data to pass to the progress callback
  1525. */
  1526. data->set.progress_client = va_arg(param, void *);
  1527. break;
  1528. #ifndef CURL_DISABLE_PROXY
  1529. case CURLOPT_PROXYUSERPWD:
  1530. /*
  1531. * user:password needed to use the proxy
  1532. */
  1533. result = setstropt_userpwd(va_arg(param, char *),
  1534. &data->set.str[STRING_PROXYUSERNAME],
  1535. &data->set.str[STRING_PROXYPASSWORD]);
  1536. break;
  1537. case CURLOPT_PROXYUSERNAME:
  1538. /*
  1539. * authentication user name to use in the operation
  1540. */
  1541. result = setstropt(&data->set.str[STRING_PROXYUSERNAME],
  1542. va_arg(param, char *));
  1543. break;
  1544. case CURLOPT_PROXYPASSWORD:
  1545. /*
  1546. * authentication password to use in the operation
  1547. */
  1548. result = setstropt(&data->set.str[STRING_PROXYPASSWORD],
  1549. va_arg(param, char *));
  1550. break;
  1551. case CURLOPT_NOPROXY:
  1552. /*
  1553. * proxy exception list
  1554. */
  1555. result = setstropt(&data->set.str[STRING_NOPROXY],
  1556. va_arg(param, char *));
  1557. break;
  1558. #endif
  1559. case CURLOPT_RANGE:
  1560. /*
  1561. * What range of the file you want to transfer
  1562. */
  1563. result = setstropt(&data->set.str[STRING_SET_RANGE],
  1564. va_arg(param, char *));
  1565. break;
  1566. case CURLOPT_RESUME_FROM:
  1567. /*
  1568. * Resume transfer at the give file position
  1569. */
  1570. data->set.set_resume_from = va_arg(param, long);
  1571. break;
  1572. case CURLOPT_RESUME_FROM_LARGE:
  1573. /*
  1574. * Resume transfer at the give file position
  1575. */
  1576. data->set.set_resume_from = va_arg(param, curl_off_t);
  1577. break;
  1578. case CURLOPT_DEBUGFUNCTION:
  1579. /*
  1580. * stderr write callback.
  1581. */
  1582. data->set.fdebug = va_arg(param, curl_debug_callback);
  1583. /*
  1584. * if the callback provided is NULL, it'll use the default callback
  1585. */
  1586. break;
  1587. case CURLOPT_DEBUGDATA:
  1588. /*
  1589. * Set to a void * that should receive all error writes. This
  1590. * defaults to CURLOPT_STDERR for normal operations.
  1591. */
  1592. data->set.debugdata = va_arg(param, void *);
  1593. break;
  1594. case CURLOPT_STDERR:
  1595. /*
  1596. * Set to a FILE * that should receive all error writes. This
  1597. * defaults to stderr for normal operations.
  1598. */
  1599. data->set.err = va_arg(param, FILE *);
  1600. if(!data->set.err)
  1601. data->set.err = stderr;
  1602. break;
  1603. case CURLOPT_HEADERFUNCTION:
  1604. /*
  1605. * Set header write callback
  1606. */
  1607. data->set.fwrite_header = va_arg(param, curl_write_callback);
  1608. break;
  1609. case CURLOPT_WRITEFUNCTION:
  1610. /*
  1611. * Set data write callback
  1612. */
  1613. data->set.fwrite_func = va_arg(param, curl_write_callback);
  1614. if(!data->set.fwrite_func) {
  1615. data->set.is_fwrite_set = 0;
  1616. /* When set to NULL, reset to our internal default function */
  1617. data->set.fwrite_func = (curl_write_callback)fwrite;
  1618. }
  1619. else
  1620. data->set.is_fwrite_set = 1;
  1621. break;
  1622. case CURLOPT_READFUNCTION:
  1623. /*
  1624. * Read data callback
  1625. */
  1626. data->set.fread_func = va_arg(param, curl_read_callback);
  1627. if(!data->set.fread_func) {
  1628. data->set.is_fread_set = 0;
  1629. /* When set to NULL, reset to our internal default function */
  1630. data->set.fread_func = (curl_read_callback)fread;
  1631. }
  1632. else
  1633. data->set.is_fread_set = 1;
  1634. break;
  1635. case CURLOPT_SEEKFUNCTION:
  1636. /*
  1637. * Seek callback. Might be NULL.
  1638. */
  1639. data->set.seek_func = va_arg(param, curl_seek_callback);
  1640. break;
  1641. case CURLOPT_SEEKDATA:
  1642. /*
  1643. * Seek control callback. Might be NULL.
  1644. */
  1645. data->set.seek_client = va_arg(param, void *);
  1646. break;
  1647. case CURLOPT_CONV_FROM_NETWORK_FUNCTION:
  1648. /*
  1649. * "Convert from network encoding" callback
  1650. */
  1651. data->set.convfromnetwork = va_arg(param, curl_conv_callback);
  1652. break;
  1653. case CURLOPT_CONV_TO_NETWORK_FUNCTION:
  1654. /*
  1655. * "Convert to network encoding" callback
  1656. */
  1657. data->set.convtonetwork = va_arg(param, curl_conv_callback);
  1658. break;
  1659. case CURLOPT_CONV_FROM_UTF8_FUNCTION:
  1660. /*
  1661. * "Convert from UTF-8 encoding" callback
  1662. */
  1663. data->set.convfromutf8 = va_arg(param, curl_conv_callback);
  1664. break;
  1665. case CURLOPT_IOCTLFUNCTION:
  1666. /*
  1667. * I/O control callback. Might be NULL.
  1668. */
  1669. data->set.ioctl_func = va_arg(param, curl_ioctl_callback);
  1670. break;
  1671. case CURLOPT_IOCTLDATA:
  1672. /*
  1673. * I/O control data pointer. Might be NULL.
  1674. */
  1675. data->set.ioctl_client = va_arg(param, void *);
  1676. break;
  1677. case CURLOPT_SSLCERT:
  1678. /*
  1679. * String that holds file name of the SSL certificate to use
  1680. */
  1681. result = setstropt(&data->set.str[STRING_CERT],
  1682. va_arg(param, char *));
  1683. break;
  1684. case CURLOPT_SSLCERTTYPE:
  1685. /*
  1686. * String that holds file type of the SSL certificate to use
  1687. */
  1688. result = setstropt(&data->set.str[STRING_CERT_TYPE],
  1689. va_arg(param, char *));
  1690. break;
  1691. case CURLOPT_SSLKEY:
  1692. /*
  1693. * String that holds file name of the SSL key to use
  1694. */
  1695. result = setstropt(&data->set.str[STRING_KEY],
  1696. va_arg(param, char *));
  1697. break;
  1698. case CURLOPT_SSLKEYTYPE:
  1699. /*
  1700. * String that holds file type of the SSL key to use
  1701. */
  1702. result = setstropt(&data->set.str[STRING_KEY_TYPE],
  1703. va_arg(param, char *));
  1704. break;
  1705. case CURLOPT_KEYPASSWD:
  1706. /*
  1707. * String that holds the SSL or SSH private key password.
  1708. */
  1709. result = setstropt(&data->set.str[STRING_KEY_PASSWD],
  1710. va_arg(param, char *));
  1711. break;
  1712. case CURLOPT_SSLENGINE:
  1713. /*
  1714. * String that holds the SSL crypto engine.
  1715. */
  1716. argptr = va_arg(param, char *);
  1717. if(argptr && argptr[0])
  1718. result = Curl_ssl_set_engine(data, argptr);
  1719. break;
  1720. case CURLOPT_SSLENGINE_DEFAULT:
  1721. /*
  1722. * flag to set engine as default.
  1723. */
  1724. result = Curl_ssl_set_engine_default(data);
  1725. break;
  1726. case CURLOPT_CRLF:
  1727. /*
  1728. * Kludgy option to enable CRLF conversions. Subject for removal.
  1729. */
  1730. data->set.crlf = (0 != va_arg(param, long))?TRUE:FALSE;
  1731. break;
  1732. case CURLOPT_INTERFACE:
  1733. /*
  1734. * Set what interface or address/hostname to bind the socket to when
  1735. * performing an operation and thus what from-IP your connection will use.
  1736. */
  1737. result = setstropt(&data->set.str[STRING_DEVICE],
  1738. va_arg(param, char *));
  1739. break;
  1740. case CURLOPT_LOCALPORT:
  1741. /*
  1742. * Set what local port to bind the socket to when performing an operation.
  1743. */
  1744. data->set.localport = curlx_sltous(va_arg(param, long));
  1745. break;
  1746. case CURLOPT_LOCALPORTRANGE:
  1747. /*
  1748. * Set number of local ports to try, starting with CURLOPT_LOCALPORT.
  1749. */
  1750. data->set.localportrange = curlx_sltosi(va_arg(param, long));
  1751. break;
  1752. case CURLOPT_KRBLEVEL:
  1753. /*
  1754. * A string that defines the kerberos security level.
  1755. */
  1756. result = setstropt(&data->set.str[STRING_KRB_LEVEL],
  1757. va_arg(param, char *));
  1758. data->set.krb = (NULL != data->set.str[STRING_KRB_LEVEL])?TRUE:FALSE;
  1759. break;
  1760. case CURLOPT_GSSAPI_DELEGATION:
  1761. /*
  1762. * GSS-API credential delegation
  1763. */
  1764. data->set.gssapi_delegation = va_arg(param, long);
  1765. break;
  1766. case CURLOPT_SSL_VERIFYPEER:
  1767. /*
  1768. * Enable peer SSL verifying.
  1769. */
  1770. data->set.ssl.verifypeer = (0 != va_arg(param, long))?TRUE:FALSE;
  1771. break;
  1772. case CURLOPT_SSL_VERIFYHOST:
  1773. /*
  1774. * Enable verification of the host name in the peer certificate
  1775. */
  1776. arg = va_arg(param, long);
  1777. /* Obviously people are not reading documentation and too many thought
  1778. this argument took a boolean when it wasn't and misused it. We thus ban
  1779. 1 as a sensible input and we warn about its use. Then we only have the
  1780. 2 action internally stored as TRUE. */
  1781. if(1 == arg) {
  1782. failf(data, "CURLOPT_SSL_VERIFYHOST no longer supports 1 as value!");
  1783. return CURLE_BAD_FUNCTION_ARGUMENT;
  1784. }
  1785. data->set.ssl.verifyhost = (0 != arg)?TRUE:FALSE;
  1786. break;
  1787. case CURLOPT_SSL_VERIFYSTATUS:
  1788. /*
  1789. * Enable certificate status verifying.
  1790. */
  1791. if(!Curl_ssl_cert_status_request()) {
  1792. result = CURLE_NOT_BUILT_IN;
  1793. break;
  1794. }
  1795. data->set.ssl.verifystatus = (0 != va_arg(param, long))?TRUE:FALSE;
  1796. break;
  1797. case CURLOPT_SSL_CTX_FUNCTION:
  1798. #ifdef have_curlssl_ssl_ctx
  1799. /*
  1800. * Set a SSL_CTX callback
  1801. */
  1802. data->set.ssl.fsslctx = va_arg(param, curl_ssl_ctx_callback);
  1803. #else
  1804. result = CURLE_NOT_BUILT_IN;
  1805. #endif
  1806. break;
  1807. case CURLOPT_SSL_CTX_DATA:
  1808. #ifdef have_curlssl_ssl_ctx
  1809. /*
  1810. * Set a SSL_CTX callback parameter pointer
  1811. */
  1812. data->set.ssl.fsslctxp = va_arg(param, void *);
  1813. #else
  1814. result = CURLE_NOT_BUILT_IN;
  1815. #endif
  1816. break;
  1817. case CURLOPT_CERTINFO:
  1818. #ifdef have_curlssl_certinfo
  1819. data->set.ssl.certinfo = (0 != va_arg(param, long))?TRUE:FALSE;
  1820. #else
  1821. result = CURLE_NOT_BUILT_IN;
  1822. #endif
  1823. break;
  1824. case CURLOPT_PINNEDPUBLICKEY:
  1825. /*
  1826. * Set pinned public key for SSL connection.
  1827. * Specify file name of the public key in DER format.
  1828. */
  1829. result = setstropt(&data->set.str[STRING_SSL_PINNEDPUBLICKEY],
  1830. va_arg(param, char *));
  1831. break;
  1832. case CURLOPT_CAINFO:
  1833. /*
  1834. * Set CA info for SSL connection. Specify file name of the CA certificate
  1835. */
  1836. result = setstropt(&data->set.str[STRING_SSL_CAFILE],
  1837. va_arg(param, char *));
  1838. break;
  1839. case CURLOPT_CAPATH:
  1840. #ifdef have_curlssl_ca_path /* not supported by all backends */
  1841. /*
  1842. * Set CA path info for SSL connection. Specify directory name of the CA
  1843. * certificates which have been prepared using openssl c_rehash utility.
  1844. */
  1845. /* This does not work on windows. */
  1846. result = setstropt(&data->set.str[STRING_SSL_CAPATH],
  1847. va_arg(param, char *));
  1848. #else
  1849. result = CURLE_NOT_BUILT_IN;
  1850. #endif
  1851. break;
  1852. case CURLOPT_CRLFILE:
  1853. /*
  1854. * Set CRL file info for SSL connection. Specify file name of the CRL
  1855. * to check certificates revocation
  1856. */
  1857. result = setstropt(&data->set.str[STRING_SSL_CRLFILE],
  1858. va_arg(param, char *));
  1859. break;
  1860. case CURLOPT_ISSUERCERT:
  1861. /*
  1862. * Set Issuer certificate file
  1863. * to check certificates issuer
  1864. */
  1865. result = setstropt(&data->set.str[STRING_SSL_ISSUERCERT],
  1866. va_arg(param, char *));
  1867. break;
  1868. case CURLOPT_TELNETOPTIONS:
  1869. /*
  1870. * Set a linked list of telnet options
  1871. */
  1872. data->set.telnet_options = va_arg(param, struct curl_slist *);
  1873. break;
  1874. case CURLOPT_BUFFERSIZE:
  1875. /*
  1876. * The application kindly asks for a differently sized receive buffer.
  1877. * If it seems reasonable, we'll use it.
  1878. */
  1879. data->set.buffer_size = va_arg(param, long);
  1880. if((data->set.buffer_size> (BUFSIZE -1 )) ||
  1881. (data->set.buffer_size < 1))
  1882. data->set.buffer_size = 0; /* huge internal default */
  1883. break;
  1884. case CURLOPT_NOSIGNAL:
  1885. /*
  1886. * The application asks not to set any signal() or alarm() handlers,
  1887. * even when using a timeout.
  1888. */
  1889. data->set.no_signal = (0 != va_arg(param, long))?TRUE:FALSE;
  1890. break;
  1891. case CURLOPT_SHARE:
  1892. {
  1893. struct Curl_share *set;
  1894. set = va_arg(param, struct Curl_share *);
  1895. /* disconnect from old share, if any */
  1896. if(data->share) {
  1897. Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
  1898. if(data->dns.hostcachetype == HCACHE_SHARED) {
  1899. data->dns.hostcache = NULL;
  1900. data->dns.hostcachetype = HCACHE_NONE;
  1901. }
  1902. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
  1903. if(data->share->cookies == data->cookies)
  1904. data->cookies = NULL;
  1905. #endif
  1906. if(data->share->sslsession == data->state.session)
  1907. data->state.session = NULL;
  1908. data->share->dirty--;
  1909. Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
  1910. data->share = NULL;
  1911. }
  1912. /* use new share if it set */
  1913. data->share = set;
  1914. if(data->share) {
  1915. Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
  1916. data->share->dirty++;
  1917. if(data->share->hostcache) {
  1918. /* use shared host cache */
  1919. data->dns.hostcache = data->share->hostcache;
  1920. data->dns.hostcachetype = HCACHE_SHARED;
  1921. }
  1922. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
  1923. if(data->share->cookies) {
  1924. /* use shared cookie list, first free own one if any */
  1925. if(data->cookies)
  1926. Curl_cookie_cleanup(data->cookies);
  1927. /* enable cookies since we now use a share that uses cookies! */
  1928. data->cookies = data->share->cookies;
  1929. }
  1930. #endif /* CURL_DISABLE_HTTP */
  1931. if(data->share->sslsession) {
  1932. data->set.ssl.max_ssl_sessions = data->share->max_ssl_sessions;
  1933. data->state.session = data->share->sslsession;
  1934. }
  1935. Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
  1936. }
  1937. /* check for host cache not needed,
  1938. * it will be done by curl_easy_perform */
  1939. }
  1940. break;
  1941. case CURLOPT_PRIVATE:
  1942. /*
  1943. * Set private data pointer.
  1944. */
  1945. data->set.private_data = va_arg(param, void *);
  1946. break;
  1947. case CURLOPT_MAXFILESIZE:
  1948. /*
  1949. * Set the maximum size of a file to download.
  1950. */
  1951. data->set.max_filesize = va_arg(param, long);
  1952. break;
  1953. #ifdef USE_SSL
  1954. case CURLOPT_USE_SSL:
  1955. /*
  1956. * Make transfers attempt to use SSL/TLS.
  1957. */
  1958. data->set.use_ssl = (curl_usessl)va_arg(param, long);
  1959. break;
  1960. case CURLOPT_SSL_OPTIONS:
  1961. arg = va_arg(param, long);
  1962. data->set.ssl_enable_beast = arg&CURLSSLOPT_ALLOW_BEAST?TRUE:FALSE;
  1963. break;
  1964. #endif
  1965. case CURLOPT_FTPSSLAUTH:
  1966. /*
  1967. * Set a specific auth for FTP-SSL transfers.
  1968. */
  1969. data->set.ftpsslauth = (curl_ftpauth)va_arg(param, long);
  1970. break;
  1971. case CURLOPT_IPRESOLVE:
  1972. data->set.ipver = va_arg(param, long);
  1973. break;
  1974. case CURLOPT_MAXFILESIZE_LARGE:
  1975. /*
  1976. * Set the maximum size of a file to download.
  1977. */
  1978. data->set.max_filesize = va_arg(param, curl_off_t);
  1979. break;
  1980. case CURLOPT_TCP_NODELAY:
  1981. /*
  1982. * Enable or disable TCP_NODELAY, which will disable/enable the Nagle
  1983. * algorithm
  1984. */
  1985. data->set.tcp_nodelay = (0 != va_arg(param, long))?TRUE:FALSE;
  1986. break;
  1987. case CURLOPT_FTP_ACCOUNT:
  1988. result = setstropt(&data->set.str[STRING_FTP_ACCOUNT],
  1989. va_arg(param, char *));
  1990. break;
  1991. case CURLOPT_IGNORE_CONTENT_LENGTH:
  1992. data->set.ignorecl = (0 != va_arg(param, long))?TRUE:FALSE;
  1993. break;
  1994. case CURLOPT_CONNECT_ONLY:
  1995. /*
  1996. * No data transfer, set up connection and let application use the socket
  1997. */
  1998. data->set.connect_only = (0 != va_arg(param, long))?TRUE:FALSE;
  1999. break;
  2000. case CURLOPT_FTP_ALTERNATIVE_TO_USER:
  2001. result = setstropt(&data->set.str[STRING_FTP_ALTERNATIVE_TO_USER],
  2002. va_arg(param, char *));
  2003. break;
  2004. case CURLOPT_SOCKOPTFUNCTION:
  2005. /*
  2006. * socket callback function: called after socket() but before connect()
  2007. */
  2008. data->set.fsockopt = va_arg(param, curl_sockopt_callback);
  2009. break;
  2010. case CURLOPT_SOCKOPTDATA:
  2011. /*
  2012. * socket callback data pointer. Might be NULL.
  2013. */
  2014. data->set.sockopt_client = va_arg(param, void *);
  2015. break;
  2016. case CURLOPT_OPENSOCKETFUNCTION:
  2017. /*
  2018. * open/create socket callback function: called instead of socket(),
  2019. * before connect()
  2020. */
  2021. data->set.fopensocket = va_arg(param, curl_opensocket_callback);
  2022. break;
  2023. case CURLOPT_OPENSOCKETDATA:
  2024. /*
  2025. * socket callback data pointer. Might be NULL.
  2026. */
  2027. data->set.opensocket_client = va_arg(param, void *);
  2028. break;
  2029. case CURLOPT_CLOSESOCKETFUNCTION:
  2030. /*
  2031. * close socket callback function: called instead of close()
  2032. * when shutting down a connection
  2033. */
  2034. data->set.fclosesocket = va_arg(param, curl_closesocket_callback);
  2035. break;
  2036. case CURLOPT_CLOSESOCKETDATA:
  2037. /*
  2038. * socket callback data pointer. Might be NULL.
  2039. */
  2040. data->set.closesocket_client = va_arg(param, void *);
  2041. break;
  2042. case CURLOPT_SSL_SESSIONID_CACHE:
  2043. data->set.ssl.sessionid = (0 != va_arg(param, long))?TRUE:FALSE;
  2044. break;
  2045. #ifdef USE_LIBSSH2
  2046. /* we only include SSH options if explicitly built to support SSH */
  2047. case CURLOPT_SSH_AUTH_TYPES:
  2048. data->set.ssh_auth_types = va_arg(param, long);
  2049. break;
  2050. case CURLOPT_SSH_PUBLIC_KEYFILE:
  2051. /*
  2052. * Use this file instead of the $HOME/.ssh/id_dsa.pub file
  2053. */
  2054. result = setstropt(&data->set.str[STRING_SSH_PUBLIC_KEY],
  2055. va_arg(param, char *));
  2056. break;
  2057. case CURLOPT_SSH_PRIVATE_KEYFILE:
  2058. /*
  2059. * Use this file instead of the $HOME/.ssh/id_dsa file
  2060. */
  2061. result = setstropt(&data->set.str[STRING_SSH_PRIVATE_KEY],
  2062. va_arg(param, char *));
  2063. break;
  2064. case CURLOPT_SSH_HOST_PUBLIC_KEY_MD5:
  2065. /*
  2066. * Option to allow for the MD5 of the host public key to be checked
  2067. * for validation purposes.
  2068. */
  2069. result = setstropt(&data->set.str[STRING_SSH_HOST_PUBLIC_KEY_MD5],
  2070. va_arg(param, char *));
  2071. break;
  2072. #ifdef HAVE_LIBSSH2_KNOWNHOST_API
  2073. case CURLOPT_SSH_KNOWNHOSTS:
  2074. /*
  2075. * Store the file name to read known hosts from.
  2076. */
  2077. result = setstropt(&data->set.str[STRING_SSH_KNOWNHOSTS],
  2078. va_arg(param, char *));
  2079. break;
  2080. case CURLOPT_SSH_KEYFUNCTION:
  2081. /* setting to NULL is fine since the ssh.c functions themselves will
  2082. then rever to use the internal default */
  2083. data->set.ssh_keyfunc = va_arg(param, curl_sshkeycallback);
  2084. break;
  2085. case CURLOPT_SSH_KEYDATA:
  2086. /*
  2087. * Custom client data to pass to the SSH keyfunc callback
  2088. */
  2089. data->set.ssh_keyfunc_userp = va_arg(param, void *);
  2090. break;
  2091. #endif /* HAVE_LIBSSH2_KNOWNHOST_API */
  2092. #endif /* USE_LIBSSH2 */
  2093. case CURLOPT_HTTP_TRANSFER_DECODING:
  2094. /*
  2095. * disable libcurl transfer encoding is used
  2096. */
  2097. data->set.http_te_skip = (0 == va_arg(param, long))?TRUE:FALSE;
  2098. break;
  2099. case CURLOPT_HTTP_CONTENT_DECODING:
  2100. /*
  2101. * raw data passed to the application when content encoding is used
  2102. */
  2103. data->set.http_ce_skip = (0 == va_arg(param, long))?TRUE:FALSE;
  2104. break;
  2105. case CURLOPT_NEW_FILE_PERMS:
  2106. /*
  2107. * Uses these permissions instead of 0644
  2108. */
  2109. data->set.new_file_perms = va_arg(param, long);
  2110. break;
  2111. case CURLOPT_NEW_DIRECTORY_PERMS:
  2112. /*
  2113. * Uses these permissions instead of 0755
  2114. */
  2115. data->set.new_directory_perms = va_arg(param, long);
  2116. break;
  2117. case CURLOPT_ADDRESS_SCOPE:
  2118. /*
  2119. * We always get longs when passed plain numericals, but for this value we
  2120. * know that an unsigned int will always hold the value so we blindly
  2121. * typecast to this type
  2122. */
  2123. data->set.scope_id = curlx_sltoui(va_arg(param, long));
  2124. break;
  2125. case CURLOPT_PROTOCOLS:
  2126. /* set the bitmask for the protocols that are allowed to be used for the
  2127. transfer, which thus helps the app which takes URLs from users or other
  2128. external inputs and want to restrict what protocol(s) to deal
  2129. with. Defaults to CURLPROTO_ALL. */
  2130. data->set.allowed_protocols = va_arg(param, long);
  2131. break;
  2132. case CURLOPT_REDIR_PROTOCOLS:
  2133. /* set the bitmask for the protocols that libcurl is allowed to follow to,
  2134. as a subset of the CURLOPT_PROTOCOLS ones. That means the protocol needs
  2135. to be set in both bitmasks to be allowed to get redirected to. Defaults
  2136. to all protocols except FILE and SCP. */
  2137. data->set.redir_protocols = va_arg(param, long);
  2138. break;
  2139. case CURLOPT_MAIL_FROM:
  2140. /* Set the SMTP mail originator */
  2141. result = setstropt(&data->set.str[STRING_MAIL_FROM],
  2142. va_arg(param, char *));
  2143. break;
  2144. case CURLOPT_MAIL_AUTH:
  2145. /* Set the SMTP auth originator */
  2146. result = setstropt(&data->set.str[STRING_MAIL_AUTH],
  2147. va_arg(param, char *));
  2148. break;
  2149. case CURLOPT_MAIL_RCPT:
  2150. /* Set the list of mail recipients */
  2151. data->set.mail_rcpt = va_arg(param, struct curl_slist *);
  2152. break;
  2153. case CURLOPT_SASL_IR:
  2154. /* Enable/disable SASL initial response */
  2155. data->set.sasl_ir = (0 != va_arg(param, long)) ? TRUE : FALSE;
  2156. break;
  2157. case CURLOPT_RTSP_REQUEST:
  2158. {
  2159. /*
  2160. * Set the RTSP request method (OPTIONS, SETUP, PLAY, etc...)
  2161. * Would this be better if the RTSPREQ_* were just moved into here?
  2162. */
  2163. long curl_rtspreq = va_arg(param, long);
  2164. Curl_RtspReq rtspreq = RTSPREQ_NONE;
  2165. switch(curl_rtspreq) {
  2166. case CURL_RTSPREQ_OPTIONS:
  2167. rtspreq = RTSPREQ_OPTIONS;
  2168. break;
  2169. case CURL_RTSPREQ_DESCRIBE:
  2170. rtspreq = RTSPREQ_DESCRIBE;
  2171. break;
  2172. case CURL_RTSPREQ_ANNOUNCE:
  2173. rtspreq = RTSPREQ_ANNOUNCE;
  2174. break;
  2175. case CURL_RTSPREQ_SETUP:
  2176. rtspreq = RTSPREQ_SETUP;
  2177. break;
  2178. case CURL_RTSPREQ_PLAY:
  2179. rtspreq = RTSPREQ_PLAY;
  2180. break;
  2181. case CURL_RTSPREQ_PAUSE:
  2182. rtspreq = RTSPREQ_PAUSE;
  2183. break;
  2184. case CURL_RTSPREQ_TEARDOWN:
  2185. rtspreq = RTSPREQ_TEARDOWN;
  2186. break;
  2187. case CURL_RTSPREQ_GET_PARAMETER:
  2188. rtspreq = RTSPREQ_GET_PARAMETER;
  2189. break;
  2190. case CURL_RTSPREQ_SET_PARAMETER:
  2191. rtspreq = RTSPREQ_SET_PARAMETER;
  2192. break;
  2193. case CURL_RTSPREQ_RECORD:
  2194. rtspreq = RTSPREQ_RECORD;
  2195. break;
  2196. case CURL_RTSPREQ_RECEIVE:
  2197. rtspreq = RTSPREQ_RECEIVE;
  2198. break;
  2199. default:
  2200. rtspreq = RTSPREQ_NONE;
  2201. }
  2202. data->set.rtspreq = rtspreq;
  2203. break;
  2204. }
  2205. case CURLOPT_RTSP_SESSION_ID:
  2206. /*
  2207. * Set the RTSP Session ID manually. Useful if the application is
  2208. * resuming a previously established RTSP session
  2209. */
  2210. result = setstropt(&data->set.str[STRING_RTSP_SESSION_ID],
  2211. va_arg(param, char *));
  2212. break;
  2213. case CURLOPT_RTSP_STREAM_URI:
  2214. /*
  2215. * Set the Stream URI for the RTSP request. Unless the request is
  2216. * for generic server options, the application will need to set this.
  2217. */
  2218. result = setstropt(&data->set.str[STRING_RTSP_STREAM_URI],
  2219. va_arg(param, char *));
  2220. break;
  2221. case CURLOPT_RTSP_TRANSPORT:
  2222. /*
  2223. * The content of the Transport: header for the RTSP request
  2224. */
  2225. result = setstropt(&data->set.str[STRING_RTSP_TRANSPORT],
  2226. va_arg(param, char *));
  2227. break;
  2228. case CURLOPT_RTSP_CLIENT_CSEQ:
  2229. /*
  2230. * Set the CSEQ number to issue for the next RTSP request. Useful if the
  2231. * application is resuming a previously broken connection. The CSEQ
  2232. * will increment from this new number henceforth.
  2233. */
  2234. data->state.rtsp_next_client_CSeq = va_arg(param, long);
  2235. break;
  2236. case CURLOPT_RTSP_SERVER_CSEQ:
  2237. /* Same as the above, but for server-initiated requests */
  2238. data->state.rtsp_next_client_CSeq = va_arg(param, long);
  2239. break;
  2240. case CURLOPT_INTERLEAVEDATA:
  2241. data->set.rtp_out = va_arg(param, void *);
  2242. break;
  2243. case CURLOPT_INTERLEAVEFUNCTION:
  2244. /* Set the user defined RTP write function */
  2245. data->set.fwrite_rtp = va_arg(param, curl_write_callback);
  2246. break;
  2247. case CURLOPT_WILDCARDMATCH:
  2248. data->set.wildcardmatch = (0 != va_arg(param, long))?TRUE:FALSE;
  2249. break;
  2250. case CURLOPT_CHUNK_BGN_FUNCTION:
  2251. data->set.chunk_bgn = va_arg(param, curl_chunk_bgn_callback);
  2252. break;
  2253. case CURLOPT_CHUNK_END_FUNCTION:
  2254. data->set.chunk_end = va_arg(param, curl_chunk_end_callback);
  2255. break;
  2256. case CURLOPT_FNMATCH_FUNCTION:
  2257. data->set.fnmatch = va_arg(param, curl_fnmatch_callback);
  2258. break;
  2259. case CURLOPT_CHUNK_DATA:
  2260. data->wildcard.customptr = va_arg(param, void *);
  2261. break;
  2262. case CURLOPT_FNMATCH_DATA:
  2263. data->set.fnmatch_data = va_arg(param, void *);
  2264. break;
  2265. #ifdef USE_TLS_SRP
  2266. case CURLOPT_TLSAUTH_USERNAME:
  2267. result = setstropt(&data->set.str[STRING_TLSAUTH_USERNAME],
  2268. va_arg(param, char *));
  2269. if(data->set.str[STRING_TLSAUTH_USERNAME] && !data->set.ssl.authtype)
  2270. data->set.ssl.authtype = CURL_TLSAUTH_SRP; /* default to SRP */
  2271. break;
  2272. case CURLOPT_TLSAUTH_PASSWORD:
  2273. result = setstropt(&data->set.str[STRING_TLSAUTH_PASSWORD],
  2274. va_arg(param, char *));
  2275. if(data->set.str[STRING_TLSAUTH_USERNAME] && !data->set.ssl.authtype)
  2276. data->set.ssl.authtype = CURL_TLSAUTH_SRP; /* default to SRP */
  2277. break;
  2278. case CURLOPT_TLSAUTH_TYPE:
  2279. if(strnequal((char *)va_arg(param, char *), "SRP", strlen("SRP")))
  2280. data->set.ssl.authtype = CURL_TLSAUTH_SRP;
  2281. else
  2282. data->set.ssl.authtype = CURL_TLSAUTH_NONE;
  2283. break;
  2284. #endif
  2285. case CURLOPT_DNS_SERVERS:
  2286. result = Curl_set_dns_servers(data, va_arg(param, char *));
  2287. break;
  2288. case CURLOPT_DNS_INTERFACE:
  2289. result = Curl_set_dns_interface(data, va_arg(param, char *));
  2290. break;
  2291. case CURLOPT_DNS_LOCAL_IP4:
  2292. result = Curl_set_dns_local_ip4(data, va_arg(param, char *));
  2293. break;
  2294. case CURLOPT_DNS_LOCAL_IP6:
  2295. result = Curl_set_dns_local_ip6(data, va_arg(param, char *));
  2296. break;
  2297. case CURLOPT_TCP_KEEPALIVE:
  2298. data->set.tcp_keepalive = (0 != va_arg(param, long))?TRUE:FALSE;
  2299. break;
  2300. case CURLOPT_TCP_KEEPIDLE:
  2301. data->set.tcp_keepidle = va_arg(param, long);
  2302. break;
  2303. case CURLOPT_TCP_KEEPINTVL:
  2304. data->set.tcp_keepintvl = va_arg(param, long);
  2305. break;
  2306. case CURLOPT_SSL_ENABLE_NPN:
  2307. data->set.ssl_enable_npn = (0 != va_arg(param, long))?TRUE:FALSE;
  2308. break;
  2309. case CURLOPT_SSL_ENABLE_ALPN:
  2310. data->set.ssl_enable_alpn = (0 != va_arg(param, long))?TRUE:FALSE;
  2311. break;
  2312. #ifdef USE_UNIX_SOCKETS
  2313. case CURLOPT_UNIX_SOCKET_PATH:
  2314. result = setstropt(&data->set.str[STRING_UNIX_SOCKET_PATH],
  2315. va_arg(param, char *));
  2316. break;
  2317. #endif
  2318. default:
  2319. /* unknown tag and its companion, just ignore: */
  2320. result = CURLE_UNKNOWN_OPTION;
  2321. break;
  2322. }
  2323. return result;
  2324. }
  2325. static void conn_free(struct connectdata *conn)
  2326. {
  2327. if(!conn)
  2328. return;
  2329. /* possible left-overs from the async name resolvers */
  2330. Curl_resolver_cancel(conn);
  2331. /* close the SSL stuff before we close any sockets since they will/may
  2332. write to the sockets */
  2333. Curl_ssl_close(conn, FIRSTSOCKET);
  2334. Curl_ssl_close(conn, SECONDARYSOCKET);
  2335. /* close possibly still open sockets */
  2336. if(CURL_SOCKET_BAD != conn->sock[SECONDARYSOCKET])
  2337. Curl_closesocket(conn, conn->sock[SECONDARYSOCKET]);
  2338. if(CURL_SOCKET_BAD != conn->sock[FIRSTSOCKET])
  2339. Curl_closesocket(conn, conn->sock[FIRSTSOCKET]);
  2340. if(CURL_SOCKET_BAD != conn->tempsock[0])
  2341. Curl_closesocket(conn, conn->tempsock[0]);
  2342. if(CURL_SOCKET_BAD != conn->tempsock[1])
  2343. Curl_closesocket(conn, conn->tempsock[1]);
  2344. #if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM) && \
  2345. defined(NTLM_WB_ENABLED)
  2346. Curl_ntlm_wb_cleanup(conn);
  2347. #endif
  2348. Curl_safefree(conn->user);
  2349. Curl_safefree(conn->passwd);
  2350. Curl_safefree(conn->xoauth2_bearer);
  2351. Curl_safefree(conn->options);
  2352. Curl_safefree(conn->proxyuser);
  2353. Curl_safefree(conn->proxypasswd);
  2354. Curl_safefree(conn->allocptr.proxyuserpwd);
  2355. Curl_safefree(conn->allocptr.uagent);
  2356. Curl_safefree(conn->allocptr.userpwd);
  2357. Curl_safefree(conn->allocptr.accept_encoding);
  2358. Curl_safefree(conn->allocptr.te);
  2359. Curl_safefree(conn->allocptr.rangeline);
  2360. Curl_safefree(conn->allocptr.ref);
  2361. Curl_safefree(conn->allocptr.host);
  2362. Curl_safefree(conn->allocptr.cookiehost);
  2363. Curl_safefree(conn->allocptr.rtsp_transport);
  2364. Curl_safefree(conn->trailer);
  2365. Curl_safefree(conn->host.rawalloc); /* host name buffer */
  2366. Curl_safefree(conn->proxy.rawalloc); /* proxy name buffer */
  2367. Curl_safefree(conn->master_buffer);
  2368. Curl_llist_destroy(conn->send_pipe, NULL);
  2369. Curl_llist_destroy(conn->recv_pipe, NULL);
  2370. conn->send_pipe = NULL;
  2371. conn->recv_pipe = NULL;
  2372. Curl_safefree(conn->localdev);
  2373. Curl_free_ssl_config(&conn->ssl_config);
  2374. free(conn); /* free all the connection oriented data */
  2375. }
  2376. /*
  2377. * Disconnects the given connection. Note the connection may not be the
  2378. * primary connection, like when freeing room in the connection cache or
  2379. * killing of a dead old connection.
  2380. *
  2381. * This function MUST NOT reset state in the SessionHandle struct if that
  2382. * isn't strictly bound to the life-time of *this* particular connection.
  2383. *
  2384. */
  2385. CURLcode Curl_disconnect(struct connectdata *conn, bool dead_connection)
  2386. {
  2387. struct SessionHandle *data;
  2388. if(!conn)
  2389. return CURLE_OK; /* this is closed and fine already */
  2390. data = conn->data;
  2391. if(!data) {
  2392. DEBUGF(fprintf(stderr, "DISCONNECT without easy handle, ignoring\n"));
  2393. return CURLE_OK;
  2394. }
  2395. if(conn->dns_entry != NULL) {
  2396. Curl_resolv_unlock(data, conn->dns_entry);
  2397. conn->dns_entry = NULL;
  2398. }
  2399. Curl_hostcache_prune(data); /* kill old DNS cache entries */
  2400. #if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM)
  2401. /* Cleanup NTLM connection-related data */
  2402. Curl_http_ntlm_cleanup(conn);
  2403. #endif
  2404. if(conn->handler->disconnect)
  2405. /* This is set if protocol-specific cleanups should be made */
  2406. conn->handler->disconnect(conn, dead_connection);
  2407. /* unlink ourselves! */
  2408. infof(data, "Closing connection %ld\n", conn->connection_id);
  2409. Curl_conncache_remove_conn(data->state.conn_cache, conn);
  2410. #if defined(USE_LIBIDN)
  2411. if(conn->host.encalloc)
  2412. idn_free(conn->host.encalloc); /* encoded host name buffer, must be freed
  2413. with idn_free() since this was allocated
  2414. by libidn */
  2415. if(conn->proxy.encalloc)
  2416. idn_free(conn->proxy.encalloc); /* encoded proxy name buffer, must be
  2417. freed with idn_free() since this was
  2418. allocated by libidn */
  2419. #elif defined(USE_WIN32_IDN)
  2420. free(conn->host.encalloc); /* encoded host name buffer, must be freed with
  2421. idn_free() since this was allocated by
  2422. curl_win32_idn_to_ascii */
  2423. if(conn->proxy.encalloc)
  2424. free(conn->proxy.encalloc); /* encoded proxy name buffer, must be freed
  2425. with idn_free() since this was allocated by
  2426. curl_win32_idn_to_ascii */
  2427. #endif
  2428. Curl_ssl_close(conn, FIRSTSOCKET);
  2429. /* Indicate to all handles on the pipe that we're dead */
  2430. if(Curl_multi_pipeline_enabled(data->multi)) {
  2431. signalPipeClose(conn->send_pipe, TRUE);
  2432. signalPipeClose(conn->recv_pipe, TRUE);
  2433. }
  2434. conn_free(conn);
  2435. return CURLE_OK;
  2436. }
  2437. /*
  2438. * This function should return TRUE if the socket is to be assumed to
  2439. * be dead. Most commonly this happens when the server has closed the
  2440. * connection due to inactivity.
  2441. */
  2442. static bool SocketIsDead(curl_socket_t sock)
  2443. {
  2444. int sval;
  2445. bool ret_val = TRUE;
  2446. sval = Curl_socket_ready(sock, CURL_SOCKET_BAD, 0);
  2447. if(sval == 0)
  2448. /* timeout */
  2449. ret_val = FALSE;
  2450. return ret_val;
  2451. }
  2452. static bool IsPipeliningPossible(const struct SessionHandle *handle,
  2453. const struct connectdata *conn)
  2454. {
  2455. if((conn->handler->protocol & PROTO_FAMILY_HTTP) &&
  2456. Curl_multi_pipeline_enabled(handle->multi) &&
  2457. (handle->set.httpreq == HTTPREQ_GET ||
  2458. handle->set.httpreq == HTTPREQ_HEAD) &&
  2459. handle->set.httpversion != CURL_HTTP_VERSION_1_0)
  2460. return TRUE;
  2461. return FALSE;
  2462. }
  2463. bool Curl_isPipeliningEnabled(const struct SessionHandle *handle)
  2464. {
  2465. return Curl_multi_pipeline_enabled(handle->multi);
  2466. }
  2467. CURLcode Curl_addHandleToPipeline(struct SessionHandle *data,
  2468. struct curl_llist *pipeline)
  2469. {
  2470. if(!Curl_llist_insert_next(pipeline, pipeline->tail, data))
  2471. return CURLE_OUT_OF_MEMORY;
  2472. return CURLE_OK;
  2473. }
  2474. int Curl_removeHandleFromPipeline(struct SessionHandle *handle,
  2475. struct curl_llist *pipeline)
  2476. {
  2477. struct curl_llist_element *curr;
  2478. curr = pipeline->head;
  2479. while(curr) {
  2480. if(curr->ptr == handle) {
  2481. Curl_llist_remove(pipeline, curr, NULL);
  2482. return 1; /* we removed a handle */
  2483. }
  2484. curr = curr->next;
  2485. }
  2486. return 0;
  2487. }
  2488. #if 0 /* this code is saved here as it is useful for debugging purposes */
  2489. static void Curl_printPipeline(struct curl_llist *pipeline)
  2490. {
  2491. struct curl_llist_element *curr;
  2492. curr = pipeline->head;
  2493. while(curr) {
  2494. struct SessionHandle *data = (struct SessionHandle *) curr->ptr;
  2495. infof(data, "Handle in pipeline: %s\n", data->state.path);
  2496. curr = curr->next;
  2497. }
  2498. }
  2499. #endif
  2500. static struct SessionHandle* gethandleathead(struct curl_llist *pipeline)
  2501. {
  2502. struct curl_llist_element *curr = pipeline->head;
  2503. if(curr) {
  2504. return (struct SessionHandle *) curr->ptr;
  2505. }
  2506. return NULL;
  2507. }
  2508. /* remove the specified connection from all (possible) pipelines and related
  2509. queues */
  2510. void Curl_getoff_all_pipelines(struct SessionHandle *data,
  2511. struct connectdata *conn)
  2512. {
  2513. bool recv_head = (conn->readchannel_inuse &&
  2514. (gethandleathead(conn->recv_pipe) == data)) ? TRUE : FALSE;
  2515. bool send_head = (conn->writechannel_inuse &&
  2516. (gethandleathead(conn->send_pipe) == data)) ? TRUE : FALSE;
  2517. if(Curl_removeHandleFromPipeline(data, conn->recv_pipe) && recv_head)
  2518. conn->readchannel_inuse = FALSE;
  2519. if(Curl_removeHandleFromPipeline(data, conn->send_pipe) && send_head)
  2520. conn->writechannel_inuse = FALSE;
  2521. }
  2522. static void signalPipeClose(struct curl_llist *pipeline, bool pipe_broke)
  2523. {
  2524. struct curl_llist_element *curr;
  2525. if(!pipeline)
  2526. return;
  2527. curr = pipeline->head;
  2528. while(curr) {
  2529. struct curl_llist_element *next = curr->next;
  2530. struct SessionHandle *data = (struct SessionHandle *) curr->ptr;
  2531. #ifdef DEBUGBUILD /* debug-only code */
  2532. if(data->magic != CURLEASY_MAGIC_NUMBER) {
  2533. /* MAJOR BADNESS */
  2534. infof(data, "signalPipeClose() found BAAD easy handle\n");
  2535. }
  2536. #endif
  2537. if(pipe_broke)
  2538. data->state.pipe_broke = TRUE;
  2539. Curl_multi_handlePipeBreak(data);
  2540. Curl_llist_remove(pipeline, curr, NULL);
  2541. curr = next;
  2542. }
  2543. }
  2544. /*
  2545. * This function finds the connection in the connection
  2546. * cache that has been unused for the longest time.
  2547. *
  2548. * Returns the pointer to the oldest idle connection, or NULL if none was
  2549. * found.
  2550. */
  2551. static struct connectdata *
  2552. find_oldest_idle_connection(struct SessionHandle *data)
  2553. {
  2554. struct conncache *bc = data->state.conn_cache;
  2555. struct curl_hash_iterator iter;
  2556. struct curl_llist_element *curr;
  2557. struct curl_hash_element *he;
  2558. long highscore=-1;
  2559. long score;
  2560. struct timeval now;
  2561. struct connectdata *conn_candidate = NULL;
  2562. struct connectbundle *bundle;
  2563. now = Curl_tvnow();
  2564. Curl_hash_start_iterate(bc->hash, &iter);
  2565. he = Curl_hash_next_element(&iter);
  2566. while(he) {
  2567. struct connectdata *conn;
  2568. bundle = he->ptr;
  2569. curr = bundle->conn_list->head;
  2570. while(curr) {
  2571. conn = curr->ptr;
  2572. if(!conn->inuse) {
  2573. /* Set higher score for the age passed since the connection was used */
  2574. score = Curl_tvdiff(now, conn->now);
  2575. if(score > highscore) {
  2576. highscore = score;
  2577. conn_candidate = conn;
  2578. }
  2579. }
  2580. curr = curr->next;
  2581. }
  2582. he = Curl_hash_next_element(&iter);
  2583. }
  2584. return conn_candidate;
  2585. }
  2586. /*
  2587. * This function finds the connection in the connection
  2588. * bundle that has been unused for the longest time.
  2589. *
  2590. * Returns the pointer to the oldest idle connection, or NULL if none was
  2591. * found.
  2592. */
  2593. static struct connectdata *
  2594. find_oldest_idle_connection_in_bundle(struct SessionHandle *data,
  2595. struct connectbundle *bundle)
  2596. {
  2597. struct curl_llist_element *curr;
  2598. long highscore=-1;
  2599. long score;
  2600. struct timeval now;
  2601. struct connectdata *conn_candidate = NULL;
  2602. struct connectdata *conn;
  2603. (void)data;
  2604. now = Curl_tvnow();
  2605. curr = bundle->conn_list->head;
  2606. while(curr) {
  2607. conn = curr->ptr;
  2608. if(!conn->inuse) {
  2609. /* Set higher score for the age passed since the connection was used */
  2610. score = Curl_tvdiff(now, conn->now);
  2611. if(score > highscore) {
  2612. highscore = score;
  2613. conn_candidate = conn;
  2614. }
  2615. }
  2616. curr = curr->next;
  2617. }
  2618. return conn_candidate;
  2619. }
  2620. /*
  2621. * This function checks if given connection is dead and disconnects if so.
  2622. * (That also removes it from the connection cache.)
  2623. *
  2624. * Returns TRUE if the connection actually was dead and disconnected.
  2625. */
  2626. static bool disconnect_if_dead(struct connectdata *conn,
  2627. struct SessionHandle *data)
  2628. {
  2629. size_t pipeLen = conn->send_pipe->size + conn->recv_pipe->size;
  2630. if(!pipeLen && !conn->inuse) {
  2631. /* The check for a dead socket makes sense only if there are no
  2632. handles in pipeline and the connection isn't already marked in
  2633. use */
  2634. bool dead;
  2635. if(conn->handler->protocol & CURLPROTO_RTSP)
  2636. /* RTSP is a special case due to RTP interleaving */
  2637. dead = Curl_rtsp_connisdead(conn);
  2638. else
  2639. dead = SocketIsDead(conn->sock[FIRSTSOCKET]);
  2640. if(dead) {
  2641. conn->data = data;
  2642. infof(data, "Connection %ld seems to be dead!\n", conn->connection_id);
  2643. /* disconnect resources */
  2644. Curl_disconnect(conn, /* dead_connection */TRUE);
  2645. return TRUE;
  2646. }
  2647. }
  2648. return FALSE;
  2649. }
  2650. /*
  2651. * Wrapper to use disconnect_if_dead() function in Curl_conncache_foreach()
  2652. *
  2653. * Returns always 0.
  2654. */
  2655. static int call_disconnect_if_dead(struct connectdata *conn,
  2656. void *param)
  2657. {
  2658. struct SessionHandle* data = (struct SessionHandle*)param;
  2659. disconnect_if_dead(conn, data);
  2660. return 0; /* continue iteration */
  2661. }
  2662. /*
  2663. * This function scans the connection cache for half-open/dead connections,
  2664. * closes and removes them.
  2665. * The cleanup is done at most once per second.
  2666. */
  2667. static void prune_dead_connections(struct SessionHandle *data)
  2668. {
  2669. struct timeval now = Curl_tvnow();
  2670. long elapsed = Curl_tvdiff(now, data->state.conn_cache->last_cleanup);
  2671. if(elapsed >= 1000L) {
  2672. Curl_conncache_foreach(data->state.conn_cache, data,
  2673. call_disconnect_if_dead);
  2674. data->state.conn_cache->last_cleanup = now;
  2675. }
  2676. }
  2677. /*
  2678. * Given one filled in connection struct (named needle), this function should
  2679. * detect if there already is one that has all the significant details
  2680. * exactly the same and thus should be used instead.
  2681. *
  2682. * If there is a match, this function returns TRUE - and has marked the
  2683. * connection as 'in-use'. It must later be called with ConnectionDone() to
  2684. * return back to 'idle' (unused) state.
  2685. *
  2686. * The force_reuse flag is set if the connection must be used, even if
  2687. * the pipelining strategy wants to open a new connection instead of reusing.
  2688. */
  2689. static bool
  2690. ConnectionExists(struct SessionHandle *data,
  2691. struct connectdata *needle,
  2692. struct connectdata **usethis,
  2693. bool *force_reuse)
  2694. {
  2695. struct connectdata *check;
  2696. struct connectdata *chosen = 0;
  2697. bool canPipeline = IsPipeliningPossible(data, needle);
  2698. bool wantNTLMhttp = ((data->state.authhost.want & CURLAUTH_NTLM) ||
  2699. (data->state.authhost.want & CURLAUTH_NTLM_WB)) &&
  2700. (needle->handler->protocol & PROTO_FAMILY_HTTP) ? TRUE : FALSE;
  2701. struct connectbundle *bundle;
  2702. *force_reuse = FALSE;
  2703. /* We can't pipe if the site is blacklisted */
  2704. if(canPipeline && Curl_pipeline_site_blacklisted(data, needle)) {
  2705. canPipeline = FALSE;
  2706. }
  2707. /* Look up the bundle with all the connections to this
  2708. particular host */
  2709. bundle = Curl_conncache_find_bundle(data->state.conn_cache,
  2710. needle->host.name);
  2711. if(bundle) {
  2712. size_t max_pipe_len = Curl_multi_max_pipeline_length(data->multi);
  2713. size_t best_pipe_len = max_pipe_len;
  2714. struct curl_llist_element *curr;
  2715. infof(data, "Found bundle for host %s: %p\n",
  2716. needle->host.name, (void *)bundle);
  2717. /* We can't pipe if we don't know anything about the server */
  2718. if(canPipeline && !bundle->server_supports_pipelining) {
  2719. infof(data, "Server doesn't support pipelining\n");
  2720. canPipeline = FALSE;
  2721. }
  2722. curr = bundle->conn_list->head;
  2723. while(curr) {
  2724. bool match = FALSE;
  2725. #if defined(USE_NTLM)
  2726. bool credentialsMatch = FALSE;
  2727. #endif
  2728. size_t pipeLen;
  2729. /*
  2730. * Note that if we use a HTTP proxy, we check connections to that
  2731. * proxy and not to the actual remote server.
  2732. */
  2733. check = curr->ptr;
  2734. curr = curr->next;
  2735. if(disconnect_if_dead(check, data))
  2736. continue;
  2737. pipeLen = check->send_pipe->size + check->recv_pipe->size;
  2738. if(canPipeline) {
  2739. /* Make sure the pipe has only GET requests */
  2740. struct SessionHandle* sh = gethandleathead(check->send_pipe);
  2741. struct SessionHandle* rh = gethandleathead(check->recv_pipe);
  2742. if(sh) {
  2743. if(!IsPipeliningPossible(sh, check))
  2744. continue;
  2745. }
  2746. else if(rh) {
  2747. if(!IsPipeliningPossible(rh, check))
  2748. continue;
  2749. }
  2750. }
  2751. else {
  2752. if(pipeLen > 0) {
  2753. /* can only happen within multi handles, and means that another easy
  2754. handle is using this connection */
  2755. continue;
  2756. }
  2757. if(Curl_resolver_asynch()) {
  2758. /* ip_addr_str[0] is NUL only if the resolving of the name hasn't
  2759. completed yet and until then we don't re-use this connection */
  2760. if(!check->ip_addr_str[0]) {
  2761. infof(data,
  2762. "Connection #%ld is still name resolving, can't reuse\n",
  2763. check->connection_id);
  2764. continue;
  2765. }
  2766. }
  2767. if((check->sock[FIRSTSOCKET] == CURL_SOCKET_BAD) ||
  2768. check->bits.close) {
  2769. /* Don't pick a connection that hasn't connected yet or that is going
  2770. to get closed. */
  2771. infof(data, "Connection #%ld isn't open enough, can't reuse\n",
  2772. check->connection_id);
  2773. #ifdef DEBUGBUILD
  2774. if(check->recv_pipe->size > 0) {
  2775. infof(data,
  2776. "BAD! Unconnected #%ld has a non-empty recv pipeline!\n",
  2777. check->connection_id);
  2778. }
  2779. #endif
  2780. continue;
  2781. }
  2782. }
  2783. if((needle->handler->flags&PROTOPT_SSL) !=
  2784. (check->handler->flags&PROTOPT_SSL))
  2785. /* don't do mixed SSL and non-SSL connections */
  2786. if(!(needle->handler->protocol & check->handler->protocol))
  2787. /* except protocols that have been upgraded via TLS */
  2788. continue;
  2789. if(needle->handler->flags&PROTOPT_SSL) {
  2790. if((data->set.ssl.verifypeer != check->verifypeer) ||
  2791. (data->set.ssl.verifyhost != check->verifyhost))
  2792. continue;
  2793. }
  2794. if(needle->bits.proxy != check->bits.proxy)
  2795. /* don't do mixed proxy and non-proxy connections */
  2796. continue;
  2797. if(!canPipeline && check->inuse)
  2798. /* this request can't be pipelined but the checked connection is
  2799. already in use so we skip it */
  2800. continue;
  2801. if(needle->localdev || needle->localport) {
  2802. /* If we are bound to a specific local end (IP+port), we must not
  2803. re-use a random other one, although if we didn't ask for a
  2804. particular one we can reuse one that was bound.
  2805. This comparison is a bit rough and too strict. Since the input
  2806. parameters can be specified in numerous ways and still end up the
  2807. same it would take a lot of processing to make it really accurate.
  2808. Instead, this matching will assume that re-uses of bound connections
  2809. will most likely also re-use the exact same binding parameters and
  2810. missing out a few edge cases shouldn't hurt anyone very much.
  2811. */
  2812. if((check->localport != needle->localport) ||
  2813. (check->localportrange != needle->localportrange) ||
  2814. !check->localdev ||
  2815. !needle->localdev ||
  2816. strcmp(check->localdev, needle->localdev))
  2817. continue;
  2818. }
  2819. if((!(needle->handler->flags & PROTOPT_CREDSPERREQUEST)) ||
  2820. wantNTLMhttp) {
  2821. /* This protocol requires credentials per connection or is HTTP+NTLM,
  2822. so verify that we're using the same name and password as well */
  2823. if(!strequal(needle->user, check->user) ||
  2824. !strequal(needle->passwd, check->passwd)) {
  2825. /* one of them was different */
  2826. continue;
  2827. }
  2828. #if defined(USE_NTLM)
  2829. credentialsMatch = TRUE;
  2830. #endif
  2831. }
  2832. if(!needle->bits.httpproxy || needle->handler->flags&PROTOPT_SSL ||
  2833. (needle->bits.httpproxy && check->bits.httpproxy &&
  2834. needle->bits.tunnel_proxy && check->bits.tunnel_proxy &&
  2835. Curl_raw_equal(needle->proxy.name, check->proxy.name) &&
  2836. (needle->port == check->port))) {
  2837. /* The requested connection does not use a HTTP proxy or it uses SSL or
  2838. it is a non-SSL protocol tunneled over the same http proxy name and
  2839. port number or it is a non-SSL protocol which is allowed to be
  2840. upgraded via TLS */
  2841. if((Curl_raw_equal(needle->handler->scheme, check->handler->scheme) ||
  2842. needle->handler->protocol & check->handler->protocol) &&
  2843. Curl_raw_equal(needle->host.name, check->host.name) &&
  2844. needle->remote_port == check->remote_port) {
  2845. if(needle->handler->flags & PROTOPT_SSL) {
  2846. /* This is a SSL connection so verify that we're using the same
  2847. SSL options as well */
  2848. if(!Curl_ssl_config_matches(&needle->ssl_config,
  2849. &check->ssl_config)) {
  2850. DEBUGF(infof(data,
  2851. "Connection #%ld has different SSL parameters, "
  2852. "can't reuse\n",
  2853. check->connection_id));
  2854. continue;
  2855. }
  2856. else if(check->ssl[FIRSTSOCKET].state != ssl_connection_complete) {
  2857. DEBUGF(infof(data,
  2858. "Connection #%ld has not started SSL connect, "
  2859. "can't reuse\n",
  2860. check->connection_id));
  2861. continue;
  2862. }
  2863. }
  2864. match = TRUE;
  2865. }
  2866. }
  2867. else { /* The requested needle connection is using a proxy,
  2868. is the checked one using the same host, port and type? */
  2869. if(check->bits.proxy &&
  2870. (needle->proxytype == check->proxytype) &&
  2871. (needle->bits.tunnel_proxy == check->bits.tunnel_proxy) &&
  2872. Curl_raw_equal(needle->proxy.name, check->proxy.name) &&
  2873. needle->port == check->port) {
  2874. /* This is the same proxy connection, use it! */
  2875. match = TRUE;
  2876. }
  2877. }
  2878. if(match) {
  2879. #if defined(USE_NTLM)
  2880. /* If we are looking for an HTTP+NTLM connection, check if this is
  2881. already authenticating with the right credentials. If not, keep
  2882. looking so that we can reuse NTLM connections if
  2883. possible. (Especially we must not reuse the same connection if
  2884. partway through a handshake!) */
  2885. if(wantNTLMhttp) {
  2886. if(credentialsMatch && check->ntlm.state != NTLMSTATE_NONE) {
  2887. chosen = check;
  2888. /* We must use this connection, no other */
  2889. *force_reuse = TRUE;
  2890. break;
  2891. }
  2892. else if(credentialsMatch)
  2893. /* this is a backup choice */
  2894. chosen = check;
  2895. continue;
  2896. }
  2897. #endif
  2898. if(canPipeline) {
  2899. /* We can pipeline if we want to. Let's continue looking for
  2900. the optimal connection to use, i.e the shortest pipe that is not
  2901. blacklisted. */
  2902. if(pipeLen == 0) {
  2903. /* We have the optimal connection. Let's stop looking. */
  2904. chosen = check;
  2905. break;
  2906. }
  2907. /* We can't use the connection if the pipe is full */
  2908. if(pipeLen >= max_pipe_len)
  2909. continue;
  2910. /* We can't use the connection if the pipe is penalized */
  2911. if(Curl_pipeline_penalized(data, check))
  2912. continue;
  2913. if(pipeLen < best_pipe_len) {
  2914. /* This connection has a shorter pipe so far. We'll pick this
  2915. and continue searching */
  2916. chosen = check;
  2917. best_pipe_len = pipeLen;
  2918. continue;
  2919. }
  2920. }
  2921. else {
  2922. /* We have found a connection. Let's stop searching. */
  2923. chosen = check;
  2924. break;
  2925. }
  2926. }
  2927. }
  2928. }
  2929. if(chosen) {
  2930. *usethis = chosen;
  2931. return TRUE; /* yes, we found one to use! */
  2932. }
  2933. return FALSE; /* no matching connecting exists */
  2934. }
  2935. /* Mark the connection as 'idle', or close it if the cache is full.
  2936. Returns TRUE if the connection is kept, or FALSE if it was closed. */
  2937. static bool
  2938. ConnectionDone(struct SessionHandle *data, struct connectdata *conn)
  2939. {
  2940. /* data->multi->maxconnects can be negative, deal with it. */
  2941. size_t maxconnects =
  2942. (data->multi->maxconnects < 0) ? data->multi->num_easy * 4:
  2943. data->multi->maxconnects;
  2944. struct connectdata *conn_candidate = NULL;
  2945. /* Mark the current connection as 'unused' */
  2946. conn->inuse = FALSE;
  2947. if(maxconnects > 0 &&
  2948. data->state.conn_cache->num_connections > maxconnects) {
  2949. infof(data, "Connection cache is full, closing the oldest one.\n");
  2950. conn_candidate = find_oldest_idle_connection(data);
  2951. if(conn_candidate) {
  2952. /* Set the connection's owner correctly */
  2953. conn_candidate->data = data;
  2954. /* the winner gets the honour of being disconnected */
  2955. (void)Curl_disconnect(conn_candidate, /* dead_connection */ FALSE);
  2956. }
  2957. }
  2958. return (conn_candidate == conn) ? FALSE : TRUE;
  2959. }
  2960. /*
  2961. * The given input connection struct pointer is to be stored in the connection
  2962. * cache. If the cache is already full, least interesting existing connection
  2963. * (if any) gets closed.
  2964. *
  2965. * The given connection should be unique. That must've been checked prior to
  2966. * this call.
  2967. */
  2968. static CURLcode ConnectionStore(struct SessionHandle *data,
  2969. struct connectdata *conn)
  2970. {
  2971. return Curl_conncache_add_conn(data->state.conn_cache, conn);
  2972. }
  2973. /* after a TCP connection to the proxy has been verified, this function does
  2974. the next magic step.
  2975. Note: this function's sub-functions call failf()
  2976. */
  2977. CURLcode Curl_connected_proxy(struct connectdata *conn,
  2978. int sockindex)
  2979. {
  2980. if(!conn->bits.proxy || sockindex)
  2981. /* this magic only works for the primary socket as the secondary is used
  2982. for FTP only and it has FTP specific magic in ftp.c */
  2983. return CURLE_OK;
  2984. switch(conn->proxytype) {
  2985. #ifndef CURL_DISABLE_PROXY
  2986. case CURLPROXY_SOCKS5:
  2987. case CURLPROXY_SOCKS5_HOSTNAME:
  2988. return Curl_SOCKS5(conn->proxyuser, conn->proxypasswd,
  2989. conn->host.name, conn->remote_port,
  2990. FIRSTSOCKET, conn);
  2991. case CURLPROXY_SOCKS4:
  2992. return Curl_SOCKS4(conn->proxyuser, conn->host.name,
  2993. conn->remote_port, FIRSTSOCKET, conn, FALSE);
  2994. case CURLPROXY_SOCKS4A:
  2995. return Curl_SOCKS4(conn->proxyuser, conn->host.name,
  2996. conn->remote_port, FIRSTSOCKET, conn, TRUE);
  2997. #endif /* CURL_DISABLE_PROXY */
  2998. case CURLPROXY_HTTP:
  2999. case CURLPROXY_HTTP_1_0:
  3000. /* do nothing here. handled later. */
  3001. break;
  3002. default:
  3003. break;
  3004. } /* switch proxytype */
  3005. return CURLE_OK;
  3006. }
  3007. /*
  3008. * verboseconnect() displays verbose information after a connect
  3009. */
  3010. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  3011. void Curl_verboseconnect(struct connectdata *conn)
  3012. {
  3013. if(conn->data->set.verbose)
  3014. infof(conn->data, "Connected to %s (%s) port %ld (#%ld)\n",
  3015. conn->bits.proxy ? conn->proxy.dispname : conn->host.dispname,
  3016. conn->ip_addr_str, conn->port, conn->connection_id);
  3017. }
  3018. #endif
  3019. int Curl_protocol_getsock(struct connectdata *conn,
  3020. curl_socket_t *socks,
  3021. int numsocks)
  3022. {
  3023. if(conn->handler->proto_getsock)
  3024. return conn->handler->proto_getsock(conn, socks, numsocks);
  3025. return GETSOCK_BLANK;
  3026. }
  3027. int Curl_doing_getsock(struct connectdata *conn,
  3028. curl_socket_t *socks,
  3029. int numsocks)
  3030. {
  3031. if(conn && conn->handler->doing_getsock)
  3032. return conn->handler->doing_getsock(conn, socks, numsocks);
  3033. return GETSOCK_BLANK;
  3034. }
  3035. /*
  3036. * We are doing protocol-specific connecting and this is being called over and
  3037. * over from the multi interface until the connection phase is done on
  3038. * protocol layer.
  3039. */
  3040. CURLcode Curl_protocol_connecting(struct connectdata *conn,
  3041. bool *done)
  3042. {
  3043. CURLcode result=CURLE_OK;
  3044. if(conn && conn->handler->connecting) {
  3045. *done = FALSE;
  3046. result = conn->handler->connecting(conn, done);
  3047. }
  3048. else
  3049. *done = TRUE;
  3050. return result;
  3051. }
  3052. /*
  3053. * We are DOING this is being called over and over from the multi interface
  3054. * until the DOING phase is done on protocol layer.
  3055. */
  3056. CURLcode Curl_protocol_doing(struct connectdata *conn, bool *done)
  3057. {
  3058. CURLcode result=CURLE_OK;
  3059. if(conn && conn->handler->doing) {
  3060. *done = FALSE;
  3061. result = conn->handler->doing(conn, done);
  3062. }
  3063. else
  3064. *done = TRUE;
  3065. return result;
  3066. }
  3067. /*
  3068. * We have discovered that the TCP connection has been successful, we can now
  3069. * proceed with some action.
  3070. *
  3071. */
  3072. CURLcode Curl_protocol_connect(struct connectdata *conn,
  3073. bool *protocol_done)
  3074. {
  3075. CURLcode result=CURLE_OK;
  3076. *protocol_done = FALSE;
  3077. if(conn->bits.tcpconnect[FIRSTSOCKET] && conn->bits.protoconnstart) {
  3078. /* We already are connected, get back. This may happen when the connect
  3079. worked fine in the first call, like when we connect to a local server
  3080. or proxy. Note that we don't know if the protocol is actually done.
  3081. Unless this protocol doesn't have any protocol-connect callback, as
  3082. then we know we're done. */
  3083. if(!conn->handler->connecting)
  3084. *protocol_done = TRUE;
  3085. return CURLE_OK;
  3086. }
  3087. if(!conn->bits.protoconnstart) {
  3088. result = Curl_proxy_connect(conn);
  3089. if(result)
  3090. return result;
  3091. if(conn->bits.tunnel_proxy && conn->bits.httpproxy &&
  3092. (conn->tunnel_state[FIRSTSOCKET] != TUNNEL_COMPLETE))
  3093. /* when using an HTTP tunnel proxy, await complete tunnel establishment
  3094. before proceeding further. Return CURLE_OK so we'll be called again */
  3095. return CURLE_OK;
  3096. if(conn->handler->connect_it) {
  3097. /* is there a protocol-specific connect() procedure? */
  3098. /* Call the protocol-specific connect function */
  3099. result = conn->handler->connect_it(conn, protocol_done);
  3100. }
  3101. else
  3102. *protocol_done = TRUE;
  3103. /* it has started, possibly even completed but that knowledge isn't stored
  3104. in this bit! */
  3105. if(!result)
  3106. conn->bits.protoconnstart = TRUE;
  3107. }
  3108. return result; /* pass back status */
  3109. }
  3110. /*
  3111. * Helpers for IDNA convertions.
  3112. */
  3113. static bool is_ASCII_name(const char *hostname)
  3114. {
  3115. const unsigned char *ch = (const unsigned char*)hostname;
  3116. while(*ch) {
  3117. if(*ch++ & 0x80)
  3118. return FALSE;
  3119. }
  3120. return TRUE;
  3121. }
  3122. #ifdef USE_LIBIDN
  3123. /*
  3124. * Check if characters in hostname is allowed in Top Level Domain.
  3125. */
  3126. static bool tld_check_name(struct SessionHandle *data,
  3127. const char *ace_hostname)
  3128. {
  3129. size_t err_pos;
  3130. char *uc_name = NULL;
  3131. int rc;
  3132. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  3133. const char *tld_errmsg = "<no msg>";
  3134. #else
  3135. (void)data;
  3136. #endif
  3137. /* Convert (and downcase) ACE-name back into locale's character set */
  3138. rc = idna_to_unicode_lzlz(ace_hostname, &uc_name, 0);
  3139. if(rc != IDNA_SUCCESS)
  3140. return FALSE;
  3141. rc = tld_check_lz(uc_name, &err_pos, NULL);
  3142. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  3143. #ifdef HAVE_TLD_STRERROR
  3144. if(rc != TLD_SUCCESS)
  3145. tld_errmsg = tld_strerror((Tld_rc)rc);
  3146. #endif
  3147. if(rc == TLD_INVALID)
  3148. infof(data, "WARNING: %s; pos %u = `%c'/0x%02X\n",
  3149. tld_errmsg, err_pos, uc_name[err_pos],
  3150. uc_name[err_pos] & 255);
  3151. else if(rc != TLD_SUCCESS)
  3152. infof(data, "WARNING: TLD check for %s failed; %s\n",
  3153. uc_name, tld_errmsg);
  3154. #endif /* CURL_DISABLE_VERBOSE_STRINGS */
  3155. if(uc_name)
  3156. idn_free(uc_name);
  3157. if(rc != TLD_SUCCESS)
  3158. return FALSE;
  3159. return TRUE;
  3160. }
  3161. #endif
  3162. /*
  3163. * Perform any necessary IDN conversion of hostname
  3164. */
  3165. static void fix_hostname(struct SessionHandle *data,
  3166. struct connectdata *conn, struct hostname *host)
  3167. {
  3168. size_t len;
  3169. #ifndef USE_LIBIDN
  3170. (void)data;
  3171. (void)conn;
  3172. #elif defined(CURL_DISABLE_VERBOSE_STRINGS)
  3173. (void)conn;
  3174. #endif
  3175. /* set the name we use to display the host name */
  3176. host->dispname = host->name;
  3177. len = strlen(host->name);
  3178. if(host->name[len-1] == '.')
  3179. /* strip off a single trailing dot if present, primarily for SNI but
  3180. there's no use for it */
  3181. host->name[len-1]=0;
  3182. if(!is_ASCII_name(host->name)) {
  3183. #ifdef USE_LIBIDN
  3184. /*************************************************************
  3185. * Check name for non-ASCII and convert hostname to ACE form.
  3186. *************************************************************/
  3187. if(stringprep_check_version(LIBIDN_REQUIRED_VERSION)) {
  3188. char *ace_hostname = NULL;
  3189. int rc = idna_to_ascii_lz(host->name, &ace_hostname, 0);
  3190. infof (data, "Input domain encoded as `%s'\n",
  3191. stringprep_locale_charset ());
  3192. if(rc != IDNA_SUCCESS)
  3193. infof(data, "Failed to convert %s to ACE; %s\n",
  3194. host->name, Curl_idn_strerror(conn,rc));
  3195. else {
  3196. /* tld_check_name() displays a warning if the host name contains
  3197. "illegal" characters for this TLD */
  3198. (void)tld_check_name(data, ace_hostname);
  3199. host->encalloc = ace_hostname;
  3200. /* change the name pointer to point to the encoded hostname */
  3201. host->name = host->encalloc;
  3202. }
  3203. }
  3204. #elif defined(USE_WIN32_IDN)
  3205. /*************************************************************
  3206. * Check name for non-ASCII and convert hostname to ACE form.
  3207. *************************************************************/
  3208. char *ace_hostname = NULL;
  3209. int rc = curl_win32_idn_to_ascii(host->name, &ace_hostname);
  3210. if(rc == 0)
  3211. infof(data, "Failed to convert %s to ACE;\n",
  3212. host->name);
  3213. else {
  3214. host->encalloc = ace_hostname;
  3215. /* change the name pointer to point to the encoded hostname */
  3216. host->name = host->encalloc;
  3217. }
  3218. #else
  3219. infof(data, "IDN support not present, can't parse Unicode domains\n");
  3220. #endif
  3221. }
  3222. }
  3223. static void llist_dtor(void *user, void *element)
  3224. {
  3225. (void)user;
  3226. (void)element;
  3227. /* Do nothing */
  3228. }
  3229. /*
  3230. * Allocate and initialize a new connectdata object.
  3231. */
  3232. static struct connectdata *allocate_conn(struct SessionHandle *data)
  3233. {
  3234. struct connectdata *conn = calloc(1, sizeof(struct connectdata));
  3235. if(!conn)
  3236. return NULL;
  3237. conn->handler = &Curl_handler_dummy; /* Be sure we have a handler defined
  3238. already from start to avoid NULL
  3239. situations and checks */
  3240. /* and we setup a few fields in case we end up actually using this struct */
  3241. conn->sock[FIRSTSOCKET] = CURL_SOCKET_BAD; /* no file descriptor */
  3242. conn->sock[SECONDARYSOCKET] = CURL_SOCKET_BAD; /* no file descriptor */
  3243. conn->tempsock[0] = CURL_SOCKET_BAD; /* no file descriptor */
  3244. conn->tempsock[1] = CURL_SOCKET_BAD; /* no file descriptor */
  3245. conn->connection_id = -1; /* no ID */
  3246. conn->port = -1; /* unknown at this point */
  3247. conn->remote_port = -1; /* unknown */
  3248. /* Default protocol-independent behavior doesn't support persistent
  3249. connections, so we set this to force-close. Protocols that support
  3250. this need to set this to FALSE in their "curl_do" functions. */
  3251. connclose(conn, "Default to force-close");
  3252. /* Store creation time to help future close decision making */
  3253. conn->created = Curl_tvnow();
  3254. conn->data = data; /* Setup the association between this connection
  3255. and the SessionHandle */
  3256. conn->proxytype = data->set.proxytype; /* type */
  3257. #ifdef CURL_DISABLE_PROXY
  3258. conn->bits.proxy = FALSE;
  3259. conn->bits.httpproxy = FALSE;
  3260. conn->bits.proxy_user_passwd = FALSE;
  3261. conn->bits.tunnel_proxy = FALSE;
  3262. #else /* CURL_DISABLE_PROXY */
  3263. /* note that these two proxy bits are now just on what looks to be
  3264. requested, they may be altered down the road */
  3265. conn->bits.proxy = (data->set.str[STRING_PROXY] &&
  3266. *data->set.str[STRING_PROXY])?TRUE:FALSE;
  3267. conn->bits.httpproxy = (conn->bits.proxy &&
  3268. (conn->proxytype == CURLPROXY_HTTP ||
  3269. conn->proxytype == CURLPROXY_HTTP_1_0))?TRUE:FALSE;
  3270. conn->bits.proxy_user_passwd =
  3271. (NULL != data->set.str[STRING_PROXYUSERNAME])?TRUE:FALSE;
  3272. conn->bits.tunnel_proxy = data->set.tunnel_thru_httpproxy;
  3273. #endif /* CURL_DISABLE_PROXY */
  3274. conn->bits.user_passwd = (NULL != data->set.str[STRING_USERNAME])?TRUE:FALSE;
  3275. conn->bits.ftp_use_epsv = data->set.ftp_use_epsv;
  3276. conn->bits.ftp_use_eprt = data->set.ftp_use_eprt;
  3277. conn->verifypeer = data->set.ssl.verifypeer;
  3278. conn->verifyhost = data->set.ssl.verifyhost;
  3279. conn->ip_version = data->set.ipver;
  3280. #if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM) && \
  3281. defined(NTLM_WB_ENABLED)
  3282. conn->ntlm_auth_hlpr_socket = CURL_SOCKET_BAD;
  3283. conn->ntlm_auth_hlpr_pid = 0;
  3284. conn->challenge_header = NULL;
  3285. conn->response_header = NULL;
  3286. #endif
  3287. if(Curl_multi_pipeline_enabled(data->multi) &&
  3288. !conn->master_buffer) {
  3289. /* Allocate master_buffer to be used for pipelining */
  3290. conn->master_buffer = calloc(BUFSIZE, sizeof (char));
  3291. if(!conn->master_buffer)
  3292. goto error;
  3293. }
  3294. /* Initialize the pipeline lists */
  3295. conn->send_pipe = Curl_llist_alloc((curl_llist_dtor) llist_dtor);
  3296. conn->recv_pipe = Curl_llist_alloc((curl_llist_dtor) llist_dtor);
  3297. if(!conn->send_pipe || !conn->recv_pipe)
  3298. goto error;
  3299. #ifdef HAVE_GSSAPI
  3300. conn->data_prot = PROT_CLEAR;
  3301. #endif
  3302. /* Store the local bind parameters that will be used for this connection */
  3303. if(data->set.str[STRING_DEVICE]) {
  3304. conn->localdev = strdup(data->set.str[STRING_DEVICE]);
  3305. if(!conn->localdev)
  3306. goto error;
  3307. }
  3308. conn->localportrange = data->set.localportrange;
  3309. conn->localport = data->set.localport;
  3310. /* the close socket stuff needs to be copied to the connection struct as
  3311. it may live on without (this specific) SessionHandle */
  3312. conn->fclosesocket = data->set.fclosesocket;
  3313. conn->closesocket_client = data->set.closesocket_client;
  3314. return conn;
  3315. error:
  3316. Curl_llist_destroy(conn->send_pipe, NULL);
  3317. Curl_llist_destroy(conn->recv_pipe, NULL);
  3318. conn->send_pipe = NULL;
  3319. conn->recv_pipe = NULL;
  3320. Curl_safefree(conn->master_buffer);
  3321. Curl_safefree(conn->localdev);
  3322. Curl_safefree(conn);
  3323. return NULL;
  3324. }
  3325. static CURLcode findprotocol(struct SessionHandle *data,
  3326. struct connectdata *conn,
  3327. const char *protostr)
  3328. {
  3329. const struct Curl_handler * const *pp;
  3330. const struct Curl_handler *p;
  3331. /* Scan protocol handler table and match against 'protostr' to set a few
  3332. variables based on the URL. Now that the handler may be changed later
  3333. when the protocol specific setup function is called. */
  3334. for(pp = protocols; (p = *pp) != NULL; pp++) {
  3335. if(Curl_raw_equal(p->scheme, protostr)) {
  3336. /* Protocol found in table. Check if allowed */
  3337. if(!(data->set.allowed_protocols & p->protocol))
  3338. /* nope, get out */
  3339. break;
  3340. /* it is allowed for "normal" request, now do an extra check if this is
  3341. the result of a redirect */
  3342. if(data->state.this_is_a_follow &&
  3343. !(data->set.redir_protocols & p->protocol))
  3344. /* nope, get out */
  3345. break;
  3346. /* Perform setup complement if some. */
  3347. conn->handler = conn->given = p;
  3348. /* 'port' and 'remote_port' are set in setup_connection_internals() */
  3349. return CURLE_OK;
  3350. }
  3351. }
  3352. /* The protocol was not found in the table, but we don't have to assign it
  3353. to anything since it is already assigned to a dummy-struct in the
  3354. create_conn() function when the connectdata struct is allocated. */
  3355. failf(data, "Protocol \"%s\" not supported or disabled in " LIBCURL_NAME,
  3356. protostr);
  3357. return CURLE_UNSUPPORTED_PROTOCOL;
  3358. }
  3359. /*
  3360. * Parse URL and fill in the relevant members of the connection struct.
  3361. */
  3362. static CURLcode parseurlandfillconn(struct SessionHandle *data,
  3363. struct connectdata *conn,
  3364. bool *prot_missing,
  3365. char **userp, char **passwdp,
  3366. char **optionsp)
  3367. {
  3368. char *at;
  3369. char *fragment;
  3370. char *path = data->state.path;
  3371. char *query;
  3372. int rc;
  3373. char protobuf[16] = "";
  3374. const char *protop = "";
  3375. CURLcode result;
  3376. bool rebuild_url = FALSE;
  3377. *prot_missing = FALSE;
  3378. /* We might pass the entire URL into the request so we need to make sure
  3379. * there are no bad characters in there.*/
  3380. if(strpbrk(data->change.url, "\r\n")) {
  3381. failf(data, "Illegal characters found in URL");
  3382. return CURLE_URL_MALFORMAT;
  3383. }
  3384. /*************************************************************
  3385. * Parse the URL.
  3386. *
  3387. * We need to parse the url even when using the proxy, because we will need
  3388. * the hostname and port in case we are trying to SSL connect through the
  3389. * proxy -- and we don't know if we will need to use SSL until we parse the
  3390. * url ...
  3391. ************************************************************/
  3392. if((2 == sscanf(data->change.url, "%15[^:]:%[^\n]",
  3393. protobuf, path)) &&
  3394. Curl_raw_equal(protobuf, "file")) {
  3395. if(path[0] == '/' && path[1] == '/') {
  3396. /* Allow omitted hostname (e.g. file:/<path>). This is not strictly
  3397. * speaking a valid file: URL by RFC 1738, but treating file:/<path> as
  3398. * file://localhost/<path> is similar to how other schemes treat missing
  3399. * hostnames. See RFC 1808. */
  3400. /* This cannot be done with strcpy() in a portable manner, since the
  3401. memory areas overlap! */
  3402. memmove(path, path + 2, strlen(path + 2)+1);
  3403. }
  3404. /*
  3405. * we deal with file://<host>/<path> differently since it supports no
  3406. * hostname other than "localhost" and "127.0.0.1", which is unique among
  3407. * the URL protocols specified in RFC 1738
  3408. */
  3409. if(path[0] != '/') {
  3410. /* the URL included a host name, we ignore host names in file:// URLs
  3411. as the standards don't define what to do with them */
  3412. char *ptr=strchr(path, '/');
  3413. if(ptr) {
  3414. /* there was a slash present
  3415. RFC1738 (section 3.1, page 5) says:
  3416. The rest of the locator consists of data specific to the scheme,
  3417. and is known as the "url-path". It supplies the details of how the
  3418. specified resource can be accessed. Note that the "/" between the
  3419. host (or port) and the url-path is NOT part of the url-path.
  3420. As most agents use file://localhost/foo to get '/foo' although the
  3421. slash preceding foo is a separator and not a slash for the path,
  3422. a URL as file://localhost//foo must be valid as well, to refer to
  3423. the same file with an absolute path.
  3424. */
  3425. if(ptr[1] && ('/' == ptr[1]))
  3426. /* if there was two slashes, we skip the first one as that is then
  3427. used truly as a separator */
  3428. ptr++;
  3429. /* This cannot be made with strcpy, as the memory chunks overlap! */
  3430. memmove(path, ptr, strlen(ptr)+1);
  3431. }
  3432. }
  3433. protop = "file"; /* protocol string */
  3434. }
  3435. else {
  3436. /* clear path */
  3437. path[0]=0;
  3438. if(2 > sscanf(data->change.url,
  3439. "%15[^\n:]://%[^\n/?]%[^\n]",
  3440. protobuf,
  3441. conn->host.name, path)) {
  3442. /*
  3443. * The URL was badly formatted, let's try the browser-style _without_
  3444. * protocol specified like 'http://'.
  3445. */
  3446. rc = sscanf(data->change.url, "%[^\n/?]%[^\n]", conn->host.name, path);
  3447. if(1 > rc) {
  3448. /*
  3449. * We couldn't even get this format.
  3450. * djgpp 2.04 has a sscanf() bug where 'conn->host.name' is
  3451. * assigned, but the return value is EOF!
  3452. */
  3453. #if defined(__DJGPP__) && (DJGPP_MINOR == 4)
  3454. if(!(rc == -1 && *conn->host.name))
  3455. #endif
  3456. {
  3457. failf(data, "<url> malformed");
  3458. return CURLE_URL_MALFORMAT;
  3459. }
  3460. }
  3461. /*
  3462. * Since there was no protocol part specified, we guess what protocol it
  3463. * is based on the first letters of the server name.
  3464. */
  3465. /* Note: if you add a new protocol, please update the list in
  3466. * lib/version.c too! */
  3467. if(checkprefix("FTP.", conn->host.name))
  3468. protop = "ftp";
  3469. else if(checkprefix("DICT.", conn->host.name))
  3470. protop = "DICT";
  3471. else if(checkprefix("LDAP.", conn->host.name))
  3472. protop = "LDAP";
  3473. else if(checkprefix("IMAP.", conn->host.name))
  3474. protop = "IMAP";
  3475. else if(checkprefix("SMTP.", conn->host.name))
  3476. protop = "smtp";
  3477. else if(checkprefix("POP3.", conn->host.name))
  3478. protop = "pop3";
  3479. else {
  3480. protop = "http";
  3481. }
  3482. *prot_missing = TRUE; /* not given in URL */
  3483. }
  3484. else
  3485. protop = protobuf;
  3486. }
  3487. /* We search for '?' in the host name (but only on the right side of a
  3488. * @-letter to allow ?-letters in username and password) to handle things
  3489. * like http://example.com?param= (notice the missing '/').
  3490. */
  3491. at = strchr(conn->host.name, '@');
  3492. if(at)
  3493. query = strchr(at+1, '?');
  3494. else
  3495. query = strchr(conn->host.name, '?');
  3496. if(query) {
  3497. /* We must insert a slash before the '?'-letter in the URL. If the URL had
  3498. a slash after the '?', that is where the path currently begins and the
  3499. '?string' is still part of the host name.
  3500. We must move the trailing part from the host name and put it first in
  3501. the path. And have it all prefixed with a slash.
  3502. */
  3503. size_t hostlen = strlen(query);
  3504. size_t pathlen = strlen(path);
  3505. /* move the existing path plus the zero byte forward, to make room for
  3506. the host-name part */
  3507. memmove(path+hostlen+1, path, pathlen+1);
  3508. /* now copy the trailing host part in front of the existing path */
  3509. memcpy(path+1, query, hostlen);
  3510. path[0]='/'; /* prepend the missing slash */
  3511. rebuild_url = TRUE;
  3512. *query=0; /* now cut off the hostname at the ? */
  3513. }
  3514. else if(!path[0]) {
  3515. /* if there's no path set, use a single slash */
  3516. strcpy(path, "/");
  3517. rebuild_url = TRUE;
  3518. }
  3519. /* If the URL is malformatted (missing a '/' after hostname before path) we
  3520. * insert a slash here. The only letter except '/' we accept to start a path
  3521. * is '?'.
  3522. */
  3523. if(path[0] == '?') {
  3524. /* We need this function to deal with overlapping memory areas. We know
  3525. that the memory area 'path' points to is 'urllen' bytes big and that
  3526. is bigger than the path. Use +1 to move the zero byte too. */
  3527. memmove(&path[1], path, strlen(path)+1);
  3528. path[0] = '/';
  3529. rebuild_url = TRUE;
  3530. }
  3531. else {
  3532. /* sanitise paths and remove ../ and ./ sequences according to RFC3986 */
  3533. char *newp = Curl_dedotdotify(path);
  3534. if(!newp)
  3535. return CURLE_OUT_OF_MEMORY;
  3536. if(strcmp(newp, path)) {
  3537. rebuild_url = TRUE;
  3538. free(data->state.pathbuffer);
  3539. data->state.pathbuffer = newp;
  3540. data->state.path = newp;
  3541. path = newp;
  3542. }
  3543. else
  3544. free(newp);
  3545. }
  3546. /*
  3547. * "rebuild_url" means that one or more URL components have been modified so
  3548. * we need to generate an updated full version. We need the corrected URL
  3549. * when communicating over HTTP proxy and we don't know at this point if
  3550. * we're using a proxy or not.
  3551. */
  3552. if(rebuild_url) {
  3553. char *reurl;
  3554. size_t plen = strlen(path); /* new path, should be 1 byte longer than
  3555. the original */
  3556. size_t urllen = strlen(data->change.url); /* original URL length */
  3557. size_t prefixlen = strlen(conn->host.name);
  3558. if(!*prot_missing)
  3559. prefixlen += strlen(protop) + strlen("://");
  3560. reurl = malloc(urllen + 2); /* 2 for zerobyte + slash */
  3561. if(!reurl)
  3562. return CURLE_OUT_OF_MEMORY;
  3563. /* copy the prefix */
  3564. memcpy(reurl, data->change.url, prefixlen);
  3565. /* append the trailing piece + zerobyte */
  3566. memcpy(&reurl[prefixlen], path, plen + 1);
  3567. /* possible free the old one */
  3568. if(data->change.url_alloc) {
  3569. Curl_safefree(data->change.url);
  3570. data->change.url_alloc = FALSE;
  3571. }
  3572. infof(data, "Rebuilt URL to: %s\n", reurl);
  3573. data->change.url = reurl;
  3574. data->change.url_alloc = TRUE; /* free this later */
  3575. }
  3576. /*
  3577. * Parse the login details from the URL and strip them out of
  3578. * the host name
  3579. */
  3580. result = parse_url_login(data, conn, userp, passwdp, optionsp);
  3581. if(result)
  3582. return result;
  3583. if(conn->host.name[0] == '[') {
  3584. /* This looks like an IPv6 address literal. See if there is an address
  3585. scope if there is no location header */
  3586. char *percent = strchr(conn->host.name, '%');
  3587. if(percent) {
  3588. unsigned int identifier_offset = 3;
  3589. char *endp;
  3590. unsigned long scope;
  3591. if(strncmp("%25", percent, 3) != 0) {
  3592. infof(data,
  3593. "Please URL encode %% as %%25, see RFC 6874.\n");
  3594. identifier_offset = 1;
  3595. }
  3596. scope = strtoul(percent + identifier_offset, &endp, 10);
  3597. if(*endp == ']') {
  3598. /* The address scope was well formed. Knock it out of the
  3599. hostname. */
  3600. memmove(percent, endp, strlen(endp)+1);
  3601. conn->scope_id = (unsigned int)scope;
  3602. }
  3603. else {
  3604. /* Zone identifier is not numeric */
  3605. #if defined(HAVE_NET_IF_H) && defined(IFNAMSIZ) && defined(HAVE_IF_NAMETOINDEX)
  3606. char ifname[IFNAMSIZ + 2];
  3607. char *square_bracket;
  3608. unsigned int scopeidx = 0;
  3609. strncpy(ifname, percent + identifier_offset, IFNAMSIZ + 2);
  3610. /* Ensure nullbyte termination */
  3611. ifname[IFNAMSIZ + 1] = '\0';
  3612. square_bracket = strchr(ifname, ']');
  3613. if(square_bracket) {
  3614. /* Remove ']' */
  3615. *square_bracket = '\0';
  3616. scopeidx = if_nametoindex(ifname);
  3617. if(scopeidx == 0) {
  3618. infof(data, "Invalid network interface: %s; %s\n", ifname,
  3619. strerror(errno));
  3620. }
  3621. }
  3622. if(scopeidx > 0) {
  3623. char *p = percent + identifier_offset + strlen(ifname);
  3624. /* Remove zone identifier from hostname */
  3625. memmove(percent, p, strlen(p) + 1);
  3626. conn->scope_id = scopeidx;
  3627. }
  3628. else
  3629. #endif /* HAVE_NET_IF_H && IFNAMSIZ */
  3630. infof(data, "Invalid IPv6 address format\n");
  3631. }
  3632. }
  3633. }
  3634. if(data->set.scope_id)
  3635. /* Override any scope that was set above. */
  3636. conn->scope_id = data->set.scope_id;
  3637. /* Remove the fragment part of the path. Per RFC 2396, this is always the
  3638. last part of the URI. We are looking for the first '#' so that we deal
  3639. gracefully with non conformant URI such as http://example.com#foo#bar. */
  3640. fragment = strchr(path, '#');
  3641. if(fragment) {
  3642. *fragment = 0;
  3643. /* we know the path part ended with a fragment, so we know the full URL
  3644. string does too and we need to cut it off from there so it isn't used
  3645. over proxy */
  3646. fragment = strchr(data->change.url, '#');
  3647. if(fragment)
  3648. *fragment = 0;
  3649. }
  3650. /*
  3651. * So if the URL was A://B/C#D,
  3652. * protop is A
  3653. * conn->host.name is B
  3654. * data->state.path is /C
  3655. */
  3656. return findprotocol(data, conn, protop);
  3657. }
  3658. /*
  3659. * If we're doing a resumed transfer, we need to setup our stuff
  3660. * properly.
  3661. */
  3662. static CURLcode setup_range(struct SessionHandle *data)
  3663. {
  3664. struct UrlState *s = &data->state;
  3665. s->resume_from = data->set.set_resume_from;
  3666. if(s->resume_from || data->set.str[STRING_SET_RANGE]) {
  3667. if(s->rangestringalloc)
  3668. free(s->range);
  3669. if(s->resume_from)
  3670. s->range = aprintf("%" CURL_FORMAT_CURL_OFF_TU "-", s->resume_from);
  3671. else
  3672. s->range = strdup(data->set.str[STRING_SET_RANGE]);
  3673. s->rangestringalloc = (s->range)?TRUE:FALSE;
  3674. if(!s->range)
  3675. return CURLE_OUT_OF_MEMORY;
  3676. /* tell ourselves to fetch this range */
  3677. s->use_range = TRUE; /* enable range download */
  3678. }
  3679. else
  3680. s->use_range = FALSE; /* disable range download */
  3681. return CURLE_OK;
  3682. }
  3683. /*
  3684. * setup_connection_internals() -
  3685. *
  3686. * Setup connection internals specific to the requested protocol in the
  3687. * SessionHandle. This is inited and setup before the connection is made but
  3688. * is about the particular protocol that is to be used.
  3689. *
  3690. * This MUST get called after proxy magic has been figured out.
  3691. */
  3692. static CURLcode setup_connection_internals(struct connectdata *conn)
  3693. {
  3694. const struct Curl_handler * p;
  3695. CURLcode result;
  3696. struct SessionHandle *data = conn->data;
  3697. /* in some case in the multi state-machine, we go back to the CONNECT state
  3698. and then a second (or third or...) call to this function will be made
  3699. without doing a DISCONNECT or DONE in between (since the connection is
  3700. yet in place) and therefore this function needs to first make sure
  3701. there's no lingering previous data allocated. */
  3702. Curl_free_request_state(data);
  3703. memset(&data->req, 0, sizeof(struct SingleRequest));
  3704. data->req.maxdownload = -1;
  3705. conn->socktype = SOCK_STREAM; /* most of them are TCP streams */
  3706. /* Perform setup complement if some. */
  3707. p = conn->handler;
  3708. if(p->setup_connection) {
  3709. result = (*p->setup_connection)(conn);
  3710. if(result)
  3711. return result;
  3712. p = conn->handler; /* May have changed. */
  3713. }
  3714. if(conn->port < 0)
  3715. /* we check for -1 here since if proxy was detected already, this
  3716. was very likely already set to the proxy port */
  3717. conn->port = p->defport;
  3718. /* only if remote_port was not already parsed off the URL we use the
  3719. default port number */
  3720. if(conn->remote_port < 0)
  3721. conn->remote_port = (unsigned short)conn->given->defport;
  3722. return CURLE_OK;
  3723. }
  3724. /*
  3725. * Curl_free_request_state() should free temp data that was allocated in the
  3726. * SessionHandle for this single request.
  3727. */
  3728. void Curl_free_request_state(struct SessionHandle *data)
  3729. {
  3730. Curl_safefree(data->req.protop);
  3731. Curl_safefree(data->req.newurl);
  3732. }
  3733. #ifndef CURL_DISABLE_PROXY
  3734. /****************************************************************
  3735. * Checks if the host is in the noproxy list. returns true if it matches
  3736. * and therefore the proxy should NOT be used.
  3737. ****************************************************************/
  3738. static bool check_noproxy(const char* name, const char* no_proxy)
  3739. {
  3740. /* no_proxy=domain1.dom,host.domain2.dom
  3741. * (a comma-separated list of hosts which should
  3742. * not be proxied, or an asterisk to override
  3743. * all proxy variables)
  3744. */
  3745. size_t tok_start;
  3746. size_t tok_end;
  3747. const char* separator = ", ";
  3748. size_t no_proxy_len;
  3749. size_t namelen;
  3750. char *endptr;
  3751. if(no_proxy && no_proxy[0]) {
  3752. if(Curl_raw_equal("*", no_proxy)) {
  3753. return TRUE;
  3754. }
  3755. /* NO_PROXY was specified and it wasn't just an asterisk */
  3756. no_proxy_len = strlen(no_proxy);
  3757. endptr = strchr(name, ':');
  3758. if(endptr)
  3759. namelen = endptr - name;
  3760. else
  3761. namelen = strlen(name);
  3762. for(tok_start = 0; tok_start < no_proxy_len; tok_start = tok_end + 1) {
  3763. while(tok_start < no_proxy_len &&
  3764. strchr(separator, no_proxy[tok_start]) != NULL) {
  3765. /* Look for the beginning of the token. */
  3766. ++tok_start;
  3767. }
  3768. if(tok_start == no_proxy_len)
  3769. break; /* It was all trailing separator chars, no more tokens. */
  3770. for(tok_end = tok_start; tok_end < no_proxy_len &&
  3771. strchr(separator, no_proxy[tok_end]) == NULL; ++tok_end)
  3772. /* Look for the end of the token. */
  3773. ;
  3774. /* To match previous behaviour, where it was necessary to specify
  3775. * ".local.com" to prevent matching "notlocal.com", we will leave
  3776. * the '.' off.
  3777. */
  3778. if(no_proxy[tok_start] == '.')
  3779. ++tok_start;
  3780. if((tok_end - tok_start) <= namelen) {
  3781. /* Match the last part of the name to the domain we are checking. */
  3782. const char *checkn = name + namelen - (tok_end - tok_start);
  3783. if(Curl_raw_nequal(no_proxy + tok_start, checkn,
  3784. tok_end - tok_start)) {
  3785. if((tok_end - tok_start) == namelen || *(checkn - 1) == '.') {
  3786. /* We either have an exact match, or the previous character is a .
  3787. * so it is within the same domain, so no proxy for this host.
  3788. */
  3789. return TRUE;
  3790. }
  3791. }
  3792. } /* if((tok_end - tok_start) <= namelen) */
  3793. } /* for(tok_start = 0; tok_start < no_proxy_len;
  3794. tok_start = tok_end + 1) */
  3795. } /* NO_PROXY was specified and it wasn't just an asterisk */
  3796. return FALSE;
  3797. }
  3798. /****************************************************************
  3799. * Detect what (if any) proxy to use. Remember that this selects a host
  3800. * name and is not limited to HTTP proxies only.
  3801. * The returned pointer must be freed by the caller (unless NULL)
  3802. ****************************************************************/
  3803. static char *detect_proxy(struct connectdata *conn)
  3804. {
  3805. char *proxy = NULL;
  3806. #ifndef CURL_DISABLE_HTTP
  3807. /* If proxy was not specified, we check for default proxy environment
  3808. * variables, to enable i.e Lynx compliance:
  3809. *
  3810. * http_proxy=http://some.server.dom:port/
  3811. * https_proxy=http://some.server.dom:port/
  3812. * ftp_proxy=http://some.server.dom:port/
  3813. * no_proxy=domain1.dom,host.domain2.dom
  3814. * (a comma-separated list of hosts which should
  3815. * not be proxied, or an asterisk to override
  3816. * all proxy variables)
  3817. * all_proxy=http://some.server.dom:port/
  3818. * (seems to exist for the CERN www lib. Probably
  3819. * the first to check for.)
  3820. *
  3821. * For compatibility, the all-uppercase versions of these variables are
  3822. * checked if the lowercase versions don't exist.
  3823. */
  3824. char *no_proxy=NULL;
  3825. char proxy_env[128];
  3826. no_proxy=curl_getenv("no_proxy");
  3827. if(!no_proxy)
  3828. no_proxy=curl_getenv("NO_PROXY");
  3829. if(!check_noproxy(conn->host.name, no_proxy)) {
  3830. /* It was not listed as without proxy */
  3831. const char *protop = conn->handler->scheme;
  3832. char *envp = proxy_env;
  3833. char *prox;
  3834. /* Now, build <protocol>_proxy and check for such a one to use */
  3835. while(*protop)
  3836. *envp++ = (char)tolower((int)*protop++);
  3837. /* append _proxy */
  3838. strcpy(envp, "_proxy");
  3839. /* read the protocol proxy: */
  3840. prox=curl_getenv(proxy_env);
  3841. /*
  3842. * We don't try the uppercase version of HTTP_PROXY because of
  3843. * security reasons:
  3844. *
  3845. * When curl is used in a webserver application
  3846. * environment (cgi or php), this environment variable can
  3847. * be controlled by the web server user by setting the
  3848. * http header 'Proxy:' to some value.
  3849. *
  3850. * This can cause 'internal' http/ftp requests to be
  3851. * arbitrarily redirected by any external attacker.
  3852. */
  3853. if(!prox && !Curl_raw_equal("http_proxy", proxy_env)) {
  3854. /* There was no lowercase variable, try the uppercase version: */
  3855. Curl_strntoupper(proxy_env, proxy_env, sizeof(proxy_env));
  3856. prox=curl_getenv(proxy_env);
  3857. }
  3858. if(prox)
  3859. proxy = prox; /* use this */
  3860. else {
  3861. proxy = curl_getenv("all_proxy"); /* default proxy to use */
  3862. if(!proxy)
  3863. proxy=curl_getenv("ALL_PROXY");
  3864. }
  3865. } /* if(!check_noproxy(conn->host.name, no_proxy)) - it wasn't specified
  3866. non-proxy */
  3867. if(no_proxy)
  3868. free(no_proxy);
  3869. #else /* !CURL_DISABLE_HTTP */
  3870. (void)conn;
  3871. #endif /* CURL_DISABLE_HTTP */
  3872. return proxy;
  3873. }
  3874. /*
  3875. * If this is supposed to use a proxy, we need to figure out the proxy
  3876. * host name, so that we can re-use an existing connection
  3877. * that may exist registered to the same proxy host.
  3878. * proxy will be freed before this function returns.
  3879. */
  3880. static CURLcode parse_proxy(struct SessionHandle *data,
  3881. struct connectdata *conn, char *proxy)
  3882. {
  3883. char *prox_portno;
  3884. char *endofprot;
  3885. /* We use 'proxyptr' to point to the proxy name from now on... */
  3886. char *proxyptr;
  3887. char *portptr;
  3888. char *atsign;
  3889. /* We do the proxy host string parsing here. We want the host name and the
  3890. * port name. Accept a protocol:// prefix
  3891. */
  3892. /* Parse the protocol part if present */
  3893. endofprot = strstr(proxy, "://");
  3894. if(endofprot) {
  3895. proxyptr = endofprot+3;
  3896. if(checkprefix("socks5h", proxy))
  3897. conn->proxytype = CURLPROXY_SOCKS5_HOSTNAME;
  3898. else if(checkprefix("socks5", proxy))
  3899. conn->proxytype = CURLPROXY_SOCKS5;
  3900. else if(checkprefix("socks4a", proxy))
  3901. conn->proxytype = CURLPROXY_SOCKS4A;
  3902. else if(checkprefix("socks4", proxy) || checkprefix("socks", proxy))
  3903. conn->proxytype = CURLPROXY_SOCKS4;
  3904. /* Any other xxx:// : change to http proxy */
  3905. }
  3906. else
  3907. proxyptr = proxy; /* No xxx:// head: It's a HTTP proxy */
  3908. /* Is there a username and password given in this proxy url? */
  3909. atsign = strchr(proxyptr, '@');
  3910. if(atsign) {
  3911. char *proxyuser = NULL;
  3912. char *proxypasswd = NULL;
  3913. CURLcode result =
  3914. parse_login_details(proxyptr, atsign - proxyptr,
  3915. &proxyuser, &proxypasswd, NULL);
  3916. if(!result) {
  3917. /* found user and password, rip them out. note that we are
  3918. unescaping them, as there is otherwise no way to have a
  3919. username or password with reserved characters like ':' in
  3920. them. */
  3921. Curl_safefree(conn->proxyuser);
  3922. if(proxyuser && strlen(proxyuser) < MAX_CURL_USER_LENGTH)
  3923. conn->proxyuser = curl_easy_unescape(data, proxyuser, 0, NULL);
  3924. else
  3925. conn->proxyuser = strdup("");
  3926. if(!conn->proxyuser)
  3927. result = CURLE_OUT_OF_MEMORY;
  3928. else {
  3929. Curl_safefree(conn->proxypasswd);
  3930. if(proxypasswd && strlen(proxypasswd) < MAX_CURL_PASSWORD_LENGTH)
  3931. conn->proxypasswd = curl_easy_unescape(data, proxypasswd, 0, NULL);
  3932. else
  3933. conn->proxypasswd = strdup("");
  3934. if(!conn->proxypasswd)
  3935. result = CURLE_OUT_OF_MEMORY;
  3936. }
  3937. if(!result) {
  3938. conn->bits.proxy_user_passwd = TRUE; /* enable it */
  3939. atsign++; /* the right side of the @-letter */
  3940. proxyptr = atsign; /* now use this instead */
  3941. }
  3942. }
  3943. Curl_safefree(proxyuser);
  3944. Curl_safefree(proxypasswd);
  3945. if(result)
  3946. return result;
  3947. }
  3948. /* start scanning for port number at this point */
  3949. portptr = proxyptr;
  3950. /* detect and extract RFC6874-style IPv6-addresses */
  3951. if(*proxyptr == '[') {
  3952. char *ptr = ++proxyptr; /* advance beyond the initial bracket */
  3953. while(*ptr && (ISXDIGIT(*ptr) || (*ptr == ':') || (*ptr == '.')))
  3954. ptr++;
  3955. if(*ptr == '%') {
  3956. /* There might be a zone identifier */
  3957. if(strncmp("%25", ptr, 3))
  3958. infof(data, "Please URL encode %% as %%25, see RFC 6874.\n");
  3959. ptr++;
  3960. /* Allow unresered characters as defined in RFC 3986 */
  3961. while(*ptr && (ISALPHA(*ptr) || ISXDIGIT(*ptr) || (*ptr == '-') ||
  3962. (*ptr == '.') || (*ptr == '_') || (*ptr == '~')))
  3963. ptr++;
  3964. }
  3965. if(*ptr == ']')
  3966. /* yeps, it ended nicely with a bracket as well */
  3967. *ptr++ = 0;
  3968. else
  3969. infof(data, "Invalid IPv6 address format\n");
  3970. portptr = ptr;
  3971. /* Note that if this didn't end with a bracket, we still advanced the
  3972. * proxyptr first, but I can't see anything wrong with that as no host
  3973. * name nor a numeric can legally start with a bracket.
  3974. */
  3975. }
  3976. /* Get port number off proxy.server.com:1080 */
  3977. prox_portno = strchr(portptr, ':');
  3978. if(prox_portno) {
  3979. *prox_portno = 0x0; /* cut off number from host name */
  3980. prox_portno ++;
  3981. /* now set the local port number */
  3982. conn->port = strtol(prox_portno, NULL, 10);
  3983. }
  3984. else {
  3985. if(proxyptr[0]=='/')
  3986. /* If the first character in the proxy string is a slash, fail
  3987. immediately. The following code will otherwise clear the string which
  3988. will lead to code running as if no proxy was set! */
  3989. return CURLE_COULDNT_RESOLVE_PROXY;
  3990. /* without a port number after the host name, some people seem to use
  3991. a slash so we strip everything from the first slash */
  3992. atsign = strchr(proxyptr, '/');
  3993. if(atsign)
  3994. *atsign = 0x0; /* cut off path part from host name */
  3995. if(data->set.proxyport)
  3996. /* None given in the proxy string, then get the default one if it is
  3997. given */
  3998. conn->port = data->set.proxyport;
  3999. }
  4000. /* now, clone the cleaned proxy host name */
  4001. conn->proxy.rawalloc = strdup(proxyptr);
  4002. conn->proxy.name = conn->proxy.rawalloc;
  4003. if(!conn->proxy.rawalloc)
  4004. return CURLE_OUT_OF_MEMORY;
  4005. return CURLE_OK;
  4006. }
  4007. /*
  4008. * Extract the user and password from the authentication string
  4009. */
  4010. static CURLcode parse_proxy_auth(struct SessionHandle *data,
  4011. struct connectdata *conn)
  4012. {
  4013. char proxyuser[MAX_CURL_USER_LENGTH]="";
  4014. char proxypasswd[MAX_CURL_PASSWORD_LENGTH]="";
  4015. if(data->set.str[STRING_PROXYUSERNAME] != NULL) {
  4016. strncpy(proxyuser, data->set.str[STRING_PROXYUSERNAME],
  4017. MAX_CURL_USER_LENGTH);
  4018. proxyuser[MAX_CURL_USER_LENGTH-1] = '\0'; /*To be on safe side*/
  4019. }
  4020. if(data->set.str[STRING_PROXYPASSWORD] != NULL) {
  4021. strncpy(proxypasswd, data->set.str[STRING_PROXYPASSWORD],
  4022. MAX_CURL_PASSWORD_LENGTH);
  4023. proxypasswd[MAX_CURL_PASSWORD_LENGTH-1] = '\0'; /*To be on safe side*/
  4024. }
  4025. conn->proxyuser = curl_easy_unescape(data, proxyuser, 0, NULL);
  4026. if(!conn->proxyuser)
  4027. return CURLE_OUT_OF_MEMORY;
  4028. conn->proxypasswd = curl_easy_unescape(data, proxypasswd, 0, NULL);
  4029. if(!conn->proxypasswd)
  4030. return CURLE_OUT_OF_MEMORY;
  4031. return CURLE_OK;
  4032. }
  4033. #endif /* CURL_DISABLE_PROXY */
  4034. /*
  4035. * parse_url_login()
  4036. *
  4037. * Parse the login details (user name, password and options) from the URL and
  4038. * strip them out of the host name
  4039. *
  4040. * Inputs: data->set.use_netrc (CURLOPT_NETRC)
  4041. * conn->host.name
  4042. *
  4043. * Outputs: (almost :- all currently undefined)
  4044. * conn->bits.user_passwd - non-zero if non-default passwords exist
  4045. * user - non-zero length if defined
  4046. * passwd - non-zero length if defined
  4047. * options - non-zero length if defined
  4048. * conn->host.name - remove user name and password
  4049. */
  4050. static CURLcode parse_url_login(struct SessionHandle *data,
  4051. struct connectdata *conn,
  4052. char **user, char **passwd, char **options)
  4053. {
  4054. CURLcode result = CURLE_OK;
  4055. char *userp = NULL;
  4056. char *passwdp = NULL;
  4057. char *optionsp = NULL;
  4058. /* At this point, we're hoping all the other special cases have
  4059. * been taken care of, so conn->host.name is at most
  4060. * [user[:password][;options]]@]hostname
  4061. *
  4062. * We need somewhere to put the embedded details, so do that first.
  4063. */
  4064. char *ptr = strchr(conn->host.name, '@');
  4065. char *login = conn->host.name;
  4066. DEBUGASSERT(!**user);
  4067. DEBUGASSERT(!**passwd);
  4068. DEBUGASSERT(!**options);
  4069. if(!ptr)
  4070. goto out;
  4071. /* We will now try to extract the
  4072. * possible login information in a string like:
  4073. * ftp://user:password@ftp.my.site:8021/README */
  4074. conn->host.name = ++ptr;
  4075. /* So the hostname is sane. Only bother interpreting the
  4076. * results if we could care. It could still be wasted
  4077. * work because it might be overtaken by the programmatically
  4078. * set user/passwd, but doing that first adds more cases here :-(
  4079. */
  4080. if(data->set.use_netrc == CURL_NETRC_REQUIRED)
  4081. goto out;
  4082. /* We could use the login information in the URL so extract it */
  4083. result = parse_login_details(login, ptr - login - 1,
  4084. &userp, &passwdp, &optionsp);
  4085. if(result)
  4086. goto out;
  4087. if(userp) {
  4088. char *newname;
  4089. /* We have a user in the URL */
  4090. conn->bits.userpwd_in_url = TRUE;
  4091. conn->bits.user_passwd = TRUE; /* enable user+password */
  4092. /* Decode the user */
  4093. newname = curl_easy_unescape(data, userp, 0, NULL);
  4094. if(!newname) {
  4095. result = CURLE_OUT_OF_MEMORY;
  4096. goto out;
  4097. }
  4098. free(*user);
  4099. *user = newname;
  4100. }
  4101. if(passwdp) {
  4102. /* We have a password in the URL so decode it */
  4103. char *newpasswd = curl_easy_unescape(data, passwdp, 0, NULL);
  4104. if(!newpasswd) {
  4105. result = CURLE_OUT_OF_MEMORY;
  4106. goto out;
  4107. }
  4108. free(*passwd);
  4109. *passwd = newpasswd;
  4110. }
  4111. if(optionsp) {
  4112. /* We have an options list in the URL so decode it */
  4113. char *newoptions = curl_easy_unescape(data, optionsp, 0, NULL);
  4114. if(!newoptions) {
  4115. result = CURLE_OUT_OF_MEMORY;
  4116. goto out;
  4117. }
  4118. free(*options);
  4119. *options = newoptions;
  4120. }
  4121. out:
  4122. Curl_safefree(userp);
  4123. Curl_safefree(passwdp);
  4124. Curl_safefree(optionsp);
  4125. return result;
  4126. }
  4127. /*
  4128. * parse_login_details()
  4129. *
  4130. * This is used to parse a login string for user name, password and options in
  4131. * the following formats:
  4132. *
  4133. * user
  4134. * user:password
  4135. * user:password;options
  4136. * user;options
  4137. * user;options:password
  4138. * :password
  4139. * :password;options
  4140. * ;options
  4141. * ;options:password
  4142. *
  4143. * Parameters:
  4144. *
  4145. * login [in] - The login string.
  4146. * len [in] - The length of the login string.
  4147. * userp [in/out] - The address where a pointer to newly allocated memory
  4148. * holding the user will be stored upon completion.
  4149. * passdwp [in/out] - The address where a pointer to newly allocated memory
  4150. * holding the password will be stored upon completion.
  4151. * optionsp [in/out] - The address where a pointer to newly allocated memory
  4152. * holding the options will be stored upon completion.
  4153. *
  4154. * Returns CURLE_OK on success.
  4155. */
  4156. static CURLcode parse_login_details(const char *login, const size_t len,
  4157. char **userp, char **passwdp,
  4158. char **optionsp)
  4159. {
  4160. CURLcode result = CURLE_OK;
  4161. char *ubuf = NULL;
  4162. char *pbuf = NULL;
  4163. char *obuf = NULL;
  4164. const char *psep = NULL;
  4165. const char *osep = NULL;
  4166. size_t ulen;
  4167. size_t plen;
  4168. size_t olen;
  4169. /* Attempt to find the password separator */
  4170. if(passwdp) {
  4171. psep = strchr(login, ':');
  4172. /* Within the constraint of the login string */
  4173. if(psep >= login + len)
  4174. psep = NULL;
  4175. }
  4176. /* Attempt to find the options separator */
  4177. if(optionsp) {
  4178. osep = strchr(login, ';');
  4179. /* Within the constraint of the login string */
  4180. if(osep >= login + len)
  4181. osep = NULL;
  4182. }
  4183. /* Calculate the portion lengths */
  4184. ulen = (psep ?
  4185. (size_t)(osep && psep > osep ? osep - login : psep - login) :
  4186. (osep ? (size_t)(osep - login) : len));
  4187. plen = (psep ?
  4188. (osep && osep > psep ? (size_t)(osep - psep) :
  4189. (size_t)(login + len - psep)) - 1 : 0);
  4190. olen = (osep ?
  4191. (psep && psep > osep ? (size_t)(psep - osep) :
  4192. (size_t)(login + len - osep)) - 1 : 0);
  4193. /* Allocate the user portion buffer */
  4194. if(userp && ulen) {
  4195. ubuf = malloc(ulen + 1);
  4196. if(!ubuf)
  4197. result = CURLE_OUT_OF_MEMORY;
  4198. }
  4199. /* Allocate the password portion buffer */
  4200. if(!result && passwdp && plen) {
  4201. pbuf = malloc(plen + 1);
  4202. if(!pbuf) {
  4203. Curl_safefree(ubuf);
  4204. result = CURLE_OUT_OF_MEMORY;
  4205. }
  4206. }
  4207. /* Allocate the options portion buffer */
  4208. if(!result && optionsp && olen) {
  4209. obuf = malloc(olen + 1);
  4210. if(!obuf) {
  4211. Curl_safefree(pbuf);
  4212. Curl_safefree(ubuf);
  4213. result = CURLE_OUT_OF_MEMORY;
  4214. }
  4215. }
  4216. if(!result) {
  4217. /* Store the user portion if necessary */
  4218. if(ubuf) {
  4219. memcpy(ubuf, login, ulen);
  4220. ubuf[ulen] = '\0';
  4221. Curl_safefree(*userp);
  4222. *userp = ubuf;
  4223. }
  4224. /* Store the password portion if necessary */
  4225. if(pbuf) {
  4226. memcpy(pbuf, psep + 1, plen);
  4227. pbuf[plen] = '\0';
  4228. Curl_safefree(*passwdp);
  4229. *passwdp = pbuf;
  4230. }
  4231. /* Store the options portion if necessary */
  4232. if(obuf) {
  4233. memcpy(obuf, osep + 1, olen);
  4234. obuf[olen] = '\0';
  4235. Curl_safefree(*optionsp);
  4236. *optionsp = obuf;
  4237. }
  4238. }
  4239. return result;
  4240. }
  4241. /*************************************************************
  4242. * Figure out the remote port number and fix it in the URL
  4243. *
  4244. * No matter if we use a proxy or not, we have to figure out the remote
  4245. * port number of various reasons.
  4246. *
  4247. * To be able to detect port number flawlessly, we must not confuse them
  4248. * IPv6-specified addresses in the [0::1] style. (RFC2732)
  4249. *
  4250. * The conn->host.name is currently [user:passwd@]host[:port] where host
  4251. * could be a hostname, IPv4 address or IPv6 address.
  4252. *
  4253. * The port number embedded in the URL is replaced, if necessary.
  4254. *************************************************************/
  4255. static CURLcode parse_remote_port(struct SessionHandle *data,
  4256. struct connectdata *conn)
  4257. {
  4258. char *portptr;
  4259. char endbracket;
  4260. /* Note that at this point, the IPv6 address cannot contain any scope
  4261. suffix as that has already been removed in the parseurlandfillconn()
  4262. function */
  4263. if((1 == sscanf(conn->host.name, "[%*45[0123456789abcdefABCDEF:.]%c",
  4264. &endbracket)) &&
  4265. (']' == endbracket)) {
  4266. /* this is a RFC2732-style specified IP-address */
  4267. conn->bits.ipv6_ip = TRUE;
  4268. conn->host.name++; /* skip over the starting bracket */
  4269. portptr = strchr(conn->host.name, ']');
  4270. if(portptr) {
  4271. *portptr++ = '\0'; /* zero terminate, killing the bracket */
  4272. if(':' != *portptr)
  4273. portptr = NULL; /* no port number available */
  4274. }
  4275. }
  4276. else {
  4277. #ifdef ENABLE_IPV6
  4278. struct in6_addr in6;
  4279. if(Curl_inet_pton(AF_INET6, conn->host.name, &in6) > 0) {
  4280. /* This is a numerical IPv6 address, meaning this is a wrongly formatted
  4281. URL */
  4282. failf(data, "IPv6 numerical address used in URL without brackets");
  4283. return CURLE_URL_MALFORMAT;
  4284. }
  4285. #endif
  4286. portptr = strrchr(conn->host.name, ':');
  4287. }
  4288. if(data->set.use_port && data->state.allow_port) {
  4289. /* if set, we use this and ignore the port possibly given in the URL */
  4290. conn->remote_port = (unsigned short)data->set.use_port;
  4291. if(portptr)
  4292. *portptr = '\0'; /* cut off the name there anyway - if there was a port
  4293. number - since the port number is to be ignored! */
  4294. if(conn->bits.httpproxy) {
  4295. /* we need to create new URL with the new port number */
  4296. char *url;
  4297. char type[12]="";
  4298. if(conn->bits.type_set)
  4299. snprintf(type, sizeof(type), ";type=%c",
  4300. data->set.prefer_ascii?'A':
  4301. (data->set.ftp_list_only?'D':'I'));
  4302. /*
  4303. * This synthesized URL isn't always right--suffixes like ;type=A are
  4304. * stripped off. It would be better to work directly from the original
  4305. * URL and simply replace the port part of it.
  4306. */
  4307. url = aprintf("%s://%s%s%s:%hu%s%s%s", conn->given->scheme,
  4308. conn->bits.ipv6_ip?"[":"", conn->host.name,
  4309. conn->bits.ipv6_ip?"]":"", conn->remote_port,
  4310. data->state.slash_removed?"/":"", data->state.path,
  4311. type);
  4312. if(!url)
  4313. return CURLE_OUT_OF_MEMORY;
  4314. if(data->change.url_alloc) {
  4315. Curl_safefree(data->change.url);
  4316. data->change.url_alloc = FALSE;
  4317. }
  4318. data->change.url = url;
  4319. data->change.url_alloc = TRUE;
  4320. }
  4321. }
  4322. else if(portptr) {
  4323. /* no CURLOPT_PORT given, extract the one from the URL */
  4324. char *rest;
  4325. long port;
  4326. port=strtol(portptr+1, &rest, 10); /* Port number must be decimal */
  4327. if((port < 0) || (port > 0xffff)) {
  4328. /* Single unix standard says port numbers are 16 bits long */
  4329. failf(data, "Port number out of range");
  4330. return CURLE_URL_MALFORMAT;
  4331. }
  4332. else if(rest != &portptr[1]) {
  4333. *portptr = '\0'; /* cut off the name there */
  4334. conn->remote_port = curlx_ultous(port);
  4335. }
  4336. else
  4337. /* Browser behavior adaptation. If there's a colon with no digits after,
  4338. just cut off the name there which makes us ignore the colon and just
  4339. use the default port. Firefox and Chrome both do that. */
  4340. *portptr = '\0';
  4341. }
  4342. return CURLE_OK;
  4343. }
  4344. /*
  4345. * Override the login details from the URL with that in the CURLOPT_USERPWD
  4346. * option or a .netrc file, if applicable.
  4347. */
  4348. static CURLcode override_login(struct SessionHandle *data,
  4349. struct connectdata *conn,
  4350. char **userp, char **passwdp, char **optionsp)
  4351. {
  4352. if(data->set.str[STRING_USERNAME]) {
  4353. free(*userp);
  4354. *userp = strdup(data->set.str[STRING_USERNAME]);
  4355. if(!*userp)
  4356. return CURLE_OUT_OF_MEMORY;
  4357. }
  4358. if(data->set.str[STRING_PASSWORD]) {
  4359. free(*passwdp);
  4360. *passwdp = strdup(data->set.str[STRING_PASSWORD]);
  4361. if(!*passwdp)
  4362. return CURLE_OUT_OF_MEMORY;
  4363. }
  4364. if(data->set.str[STRING_OPTIONS]) {
  4365. free(*optionsp);
  4366. *optionsp = strdup(data->set.str[STRING_OPTIONS]);
  4367. if(!*optionsp)
  4368. return CURLE_OUT_OF_MEMORY;
  4369. }
  4370. conn->bits.netrc = FALSE;
  4371. if(data->set.use_netrc != CURL_NETRC_IGNORED) {
  4372. int ret = Curl_parsenetrc(conn->host.name,
  4373. userp, passwdp,
  4374. data->set.str[STRING_NETRC_FILE]);
  4375. if(ret > 0) {
  4376. infof(data, "Couldn't find host %s in the "
  4377. DOT_CHAR "netrc file; using defaults\n",
  4378. conn->host.name);
  4379. }
  4380. else if(ret < 0 ) {
  4381. return CURLE_OUT_OF_MEMORY;
  4382. }
  4383. else {
  4384. /* set bits.netrc TRUE to remember that we got the name from a .netrc
  4385. file, so that it is safe to use even if we followed a Location: to a
  4386. different host or similar. */
  4387. conn->bits.netrc = TRUE;
  4388. conn->bits.user_passwd = TRUE; /* enable user+password */
  4389. }
  4390. }
  4391. return CURLE_OK;
  4392. }
  4393. /*
  4394. * Set the login details so they're available in the connection
  4395. */
  4396. static CURLcode set_login(struct connectdata *conn,
  4397. const char *user, const char *passwd,
  4398. const char *options)
  4399. {
  4400. CURLcode result = CURLE_OK;
  4401. /* If our protocol needs a password and we have none, use the defaults */
  4402. if((conn->handler->flags & PROTOPT_NEEDSPWD) && !conn->bits.user_passwd) {
  4403. /* Store the default user */
  4404. conn->user = strdup(CURL_DEFAULT_USER);
  4405. /* Store the default password */
  4406. if(conn->user)
  4407. conn->passwd = strdup(CURL_DEFAULT_PASSWORD);
  4408. else
  4409. conn->passwd = NULL;
  4410. /* This is the default password, so DON'T set conn->bits.user_passwd */
  4411. }
  4412. else {
  4413. /* Store the user, zero-length if not set */
  4414. conn->user = strdup(user);
  4415. /* Store the password (only if user is present), zero-length if not set */
  4416. if(conn->user)
  4417. conn->passwd = strdup(passwd);
  4418. else
  4419. conn->passwd = NULL;
  4420. }
  4421. if(!conn->user || !conn->passwd)
  4422. result = CURLE_OUT_OF_MEMORY;
  4423. /* Store the options, null if not set */
  4424. if(!result && options[0]) {
  4425. conn->options = strdup(options);
  4426. if(!conn->options)
  4427. result = CURLE_OUT_OF_MEMORY;
  4428. }
  4429. return result;
  4430. }
  4431. /*************************************************************
  4432. * Resolve the address of the server or proxy
  4433. *************************************************************/
  4434. static CURLcode resolve_server(struct SessionHandle *data,
  4435. struct connectdata *conn,
  4436. bool *async)
  4437. {
  4438. CURLcode result=CURLE_OK;
  4439. long timeout_ms = Curl_timeleft(data, NULL, TRUE);
  4440. /*************************************************************
  4441. * Resolve the name of the server or proxy
  4442. *************************************************************/
  4443. if(conn->bits.reuse)
  4444. /* We're reusing the connection - no need to resolve anything, and
  4445. fix_hostname() was called already in create_conn() for the re-use
  4446. case. */
  4447. *async = FALSE;
  4448. else {
  4449. /* this is a fresh connect */
  4450. int rc;
  4451. struct Curl_dns_entry *hostaddr;
  4452. /* set a pointer to the hostname we display */
  4453. fix_hostname(data, conn, &conn->host);
  4454. #ifdef USE_UNIX_SOCKETS
  4455. if(data->set.str[STRING_UNIX_SOCKET_PATH]) {
  4456. /* Unix domain sockets are local. The host gets ignored, just use the
  4457. * specified domain socket address. Do not cache "DNS entries". There is
  4458. * no DNS involved and we already have the filesystem path available */
  4459. const char *path = data->set.str[STRING_UNIX_SOCKET_PATH];
  4460. hostaddr = calloc(1, sizeof(struct Curl_dns_entry));
  4461. if(!hostaddr)
  4462. result = CURLE_OUT_OF_MEMORY;
  4463. else if((hostaddr->addr = Curl_unix2addr(path)) != NULL)
  4464. hostaddr->inuse++;
  4465. else {
  4466. /* Long paths are not supported for now */
  4467. if(strlen(path) >= sizeof(((struct sockaddr_un *)0)->sun_path)) {
  4468. failf(data, "Unix socket path too long: '%s'", path);
  4469. result = CURLE_COULDNT_RESOLVE_HOST;
  4470. }
  4471. else
  4472. result = CURLE_OUT_OF_MEMORY;
  4473. free(hostaddr);
  4474. hostaddr = NULL;
  4475. }
  4476. }
  4477. else
  4478. #endif
  4479. if(!conn->proxy.name || !*conn->proxy.name) {
  4480. /* If not connecting via a proxy, extract the port from the URL, if it is
  4481. * there, thus overriding any defaults that might have been set above. */
  4482. conn->port = conn->remote_port; /* it is the same port */
  4483. /* Resolve target host right on */
  4484. rc = Curl_resolv_timeout(conn, conn->host.name, (int)conn->port,
  4485. &hostaddr, timeout_ms);
  4486. if(rc == CURLRESOLV_PENDING)
  4487. *async = TRUE;
  4488. else if(rc == CURLRESOLV_TIMEDOUT)
  4489. result = CURLE_OPERATION_TIMEDOUT;
  4490. else if(!hostaddr) {
  4491. failf(data, "Couldn't resolve host '%s'", conn->host.dispname);
  4492. result = CURLE_COULDNT_RESOLVE_HOST;
  4493. /* don't return yet, we need to clean up the timeout first */
  4494. }
  4495. }
  4496. else {
  4497. /* This is a proxy that hasn't been resolved yet. */
  4498. /* IDN-fix the proxy name */
  4499. fix_hostname(data, conn, &conn->proxy);
  4500. /* resolve proxy */
  4501. rc = Curl_resolv_timeout(conn, conn->proxy.name, (int)conn->port,
  4502. &hostaddr, timeout_ms);
  4503. if(rc == CURLRESOLV_PENDING)
  4504. *async = TRUE;
  4505. else if(rc == CURLRESOLV_TIMEDOUT)
  4506. result = CURLE_OPERATION_TIMEDOUT;
  4507. else if(!hostaddr) {
  4508. failf(data, "Couldn't resolve proxy '%s'", conn->proxy.dispname);
  4509. result = CURLE_COULDNT_RESOLVE_PROXY;
  4510. /* don't return yet, we need to clean up the timeout first */
  4511. }
  4512. }
  4513. DEBUGASSERT(conn->dns_entry == NULL);
  4514. conn->dns_entry = hostaddr;
  4515. }
  4516. return result;
  4517. }
  4518. /*
  4519. * Cleanup the connection just allocated before we can move along and use the
  4520. * previously existing one. All relevant data is copied over and old_conn is
  4521. * ready for freeing once this function returns.
  4522. */
  4523. static void reuse_conn(struct connectdata *old_conn,
  4524. struct connectdata *conn)
  4525. {
  4526. if(old_conn->proxy.rawalloc)
  4527. free(old_conn->proxy.rawalloc);
  4528. /* free the SSL config struct from this connection struct as this was
  4529. allocated in vain and is targeted for destruction */
  4530. Curl_free_ssl_config(&old_conn->ssl_config);
  4531. conn->data = old_conn->data;
  4532. /* get the user+password information from the old_conn struct since it may
  4533. * be new for this request even when we re-use an existing connection */
  4534. conn->bits.user_passwd = old_conn->bits.user_passwd;
  4535. if(conn->bits.user_passwd) {
  4536. /* use the new user name and password though */
  4537. Curl_safefree(conn->user);
  4538. Curl_safefree(conn->passwd);
  4539. conn->user = old_conn->user;
  4540. conn->passwd = old_conn->passwd;
  4541. old_conn->user = NULL;
  4542. old_conn->passwd = NULL;
  4543. }
  4544. conn->bits.proxy_user_passwd = old_conn->bits.proxy_user_passwd;
  4545. if(conn->bits.proxy_user_passwd) {
  4546. /* use the new proxy user name and proxy password though */
  4547. Curl_safefree(conn->proxyuser);
  4548. Curl_safefree(conn->proxypasswd);
  4549. conn->proxyuser = old_conn->proxyuser;
  4550. conn->proxypasswd = old_conn->proxypasswd;
  4551. old_conn->proxyuser = NULL;
  4552. old_conn->proxypasswd = NULL;
  4553. }
  4554. /* host can change, when doing keepalive with a proxy or if the case is
  4555. different this time etc */
  4556. Curl_safefree(conn->host.rawalloc);
  4557. conn->host=old_conn->host;
  4558. /* persist connection info in session handle */
  4559. Curl_persistconninfo(conn);
  4560. /* re-use init */
  4561. conn->bits.reuse = TRUE; /* yes, we're re-using here */
  4562. Curl_safefree(old_conn->user);
  4563. Curl_safefree(old_conn->passwd);
  4564. Curl_safefree(old_conn->proxyuser);
  4565. Curl_safefree(old_conn->proxypasswd);
  4566. Curl_safefree(old_conn->localdev);
  4567. Curl_llist_destroy(old_conn->send_pipe, NULL);
  4568. Curl_llist_destroy(old_conn->recv_pipe, NULL);
  4569. old_conn->send_pipe = NULL;
  4570. old_conn->recv_pipe = NULL;
  4571. Curl_safefree(old_conn->master_buffer);
  4572. }
  4573. /**
  4574. * create_conn() sets up a new connectdata struct, or re-uses an already
  4575. * existing one, and resolves host name.
  4576. *
  4577. * if this function returns CURLE_OK and *async is set to TRUE, the resolve
  4578. * response will be coming asynchronously. If *async is FALSE, the name is
  4579. * already resolved.
  4580. *
  4581. * @param data The sessionhandle pointer
  4582. * @param in_connect is set to the next connection data pointer
  4583. * @param async is set TRUE when an async DNS resolution is pending
  4584. * @see Curl_setup_conn()
  4585. *
  4586. * *NOTE* this function assigns the conn->data pointer!
  4587. */
  4588. static CURLcode create_conn(struct SessionHandle *data,
  4589. struct connectdata **in_connect,
  4590. bool *async)
  4591. {
  4592. CURLcode result = CURLE_OK;
  4593. struct connectdata *conn;
  4594. struct connectdata *conn_temp = NULL;
  4595. size_t urllen;
  4596. char *user = NULL;
  4597. char *passwd = NULL;
  4598. char *options = NULL;
  4599. bool reuse;
  4600. char *proxy = NULL;
  4601. bool prot_missing = FALSE;
  4602. bool no_connections_available = FALSE;
  4603. bool force_reuse = FALSE;
  4604. size_t max_host_connections = Curl_multi_max_host_connections(data->multi);
  4605. size_t max_total_connections = Curl_multi_max_total_connections(data->multi);
  4606. *async = FALSE;
  4607. /*************************************************************
  4608. * Check input data
  4609. *************************************************************/
  4610. if(!data->change.url) {
  4611. result = CURLE_URL_MALFORMAT;
  4612. goto out;
  4613. }
  4614. /* First, split up the current URL in parts so that we can use the
  4615. parts for checking against the already present connections. In order
  4616. to not have to modify everything at once, we allocate a temporary
  4617. connection data struct and fill in for comparison purposes. */
  4618. conn = allocate_conn(data);
  4619. if(!conn) {
  4620. result = CURLE_OUT_OF_MEMORY;
  4621. goto out;
  4622. }
  4623. /* We must set the return variable as soon as possible, so that our
  4624. parent can cleanup any possible allocs we may have done before
  4625. any failure */
  4626. *in_connect = conn;
  4627. /* This initing continues below, see the comment "Continue connectdata
  4628. * initialization here" */
  4629. /***********************************************************
  4630. * We need to allocate memory to store the path in. We get the size of the
  4631. * full URL to be sure, and we need to make it at least 256 bytes since
  4632. * other parts of the code will rely on this fact
  4633. ***********************************************************/
  4634. #define LEAST_PATH_ALLOC 256
  4635. urllen=strlen(data->change.url);
  4636. if(urllen < LEAST_PATH_ALLOC)
  4637. urllen=LEAST_PATH_ALLOC;
  4638. /*
  4639. * We malloc() the buffers below urllen+2 to make room for 2 possibilities:
  4640. * 1 - an extra terminating zero
  4641. * 2 - an extra slash (in case a syntax like "www.host.com?moo" is used)
  4642. */
  4643. Curl_safefree(data->state.pathbuffer);
  4644. data->state.path = NULL;
  4645. data->state.pathbuffer = malloc(urllen+2);
  4646. if(NULL == data->state.pathbuffer) {
  4647. result = CURLE_OUT_OF_MEMORY; /* really bad error */
  4648. goto out;
  4649. }
  4650. data->state.path = data->state.pathbuffer;
  4651. conn->host.rawalloc = malloc(urllen+2);
  4652. if(NULL == conn->host.rawalloc) {
  4653. Curl_safefree(data->state.pathbuffer);
  4654. data->state.path = NULL;
  4655. result = CURLE_OUT_OF_MEMORY;
  4656. goto out;
  4657. }
  4658. conn->host.name = conn->host.rawalloc;
  4659. conn->host.name[0] = 0;
  4660. user = strdup("");
  4661. passwd = strdup("");
  4662. options = strdup("");
  4663. if(!user || !passwd || !options) {
  4664. result = CURLE_OUT_OF_MEMORY;
  4665. goto out;
  4666. }
  4667. result = parseurlandfillconn(data, conn, &prot_missing, &user, &passwd,
  4668. &options);
  4669. if(result)
  4670. goto out;
  4671. /*************************************************************
  4672. * No protocol part in URL was used, add it!
  4673. *************************************************************/
  4674. if(prot_missing) {
  4675. /* We're guessing prefixes here and if we're told to use a proxy or if
  4676. we're gonna follow a Location: later or... then we need the protocol
  4677. part added so that we have a valid URL. */
  4678. char *reurl;
  4679. reurl = aprintf("%s://%s", conn->handler->scheme, data->change.url);
  4680. if(!reurl) {
  4681. result = CURLE_OUT_OF_MEMORY;
  4682. goto out;
  4683. }
  4684. if(data->change.url_alloc) {
  4685. Curl_safefree(data->change.url);
  4686. data->change.url_alloc = FALSE;
  4687. }
  4688. data->change.url = reurl;
  4689. data->change.url_alloc = TRUE; /* free this later */
  4690. }
  4691. /*************************************************************
  4692. * If the protocol can't handle url query strings, then cut
  4693. * off the unhandable part
  4694. *************************************************************/
  4695. if((conn->given->flags&PROTOPT_NOURLQUERY)) {
  4696. char *path_q_sep = strchr(conn->data->state.path, '?');
  4697. if(path_q_sep) {
  4698. /* according to rfc3986, allow the query (?foo=bar)
  4699. also on protocols that can't handle it.
  4700. cut the string-part after '?'
  4701. */
  4702. /* terminate the string */
  4703. path_q_sep[0] = 0;
  4704. }
  4705. }
  4706. if(data->set.str[STRING_BEARER]) {
  4707. conn->xoauth2_bearer = strdup(data->set.str[STRING_BEARER]);
  4708. if(!conn->xoauth2_bearer) {
  4709. result = CURLE_OUT_OF_MEMORY;
  4710. goto out;
  4711. }
  4712. }
  4713. #ifndef CURL_DISABLE_PROXY
  4714. /*************************************************************
  4715. * Extract the user and password from the authentication string
  4716. *************************************************************/
  4717. if(conn->bits.proxy_user_passwd) {
  4718. result = parse_proxy_auth(data, conn);
  4719. if(result)
  4720. goto out;
  4721. }
  4722. /*************************************************************
  4723. * Detect what (if any) proxy to use
  4724. *************************************************************/
  4725. if(data->set.str[STRING_PROXY]) {
  4726. proxy = strdup(data->set.str[STRING_PROXY]);
  4727. /* if global proxy is set, this is it */
  4728. if(NULL == proxy) {
  4729. failf(data, "memory shortage");
  4730. result = CURLE_OUT_OF_MEMORY;
  4731. goto out;
  4732. }
  4733. }
  4734. if(data->set.str[STRING_NOPROXY] &&
  4735. check_noproxy(conn->host.name, data->set.str[STRING_NOPROXY])) {
  4736. if(proxy) {
  4737. free(proxy); /* proxy is in exception list */
  4738. proxy = NULL;
  4739. }
  4740. }
  4741. else if(!proxy)
  4742. proxy = detect_proxy(conn);
  4743. #ifdef USE_UNIX_SOCKETS
  4744. if(proxy && data->set.str[STRING_UNIX_SOCKET_PATH]) {
  4745. free(proxy); /* Unix domain sockets cannot be proxied, so disable it */
  4746. proxy = NULL;
  4747. }
  4748. #endif
  4749. if(proxy && (!*proxy || (conn->handler->flags & PROTOPT_NONETWORK))) {
  4750. free(proxy); /* Don't bother with an empty proxy string or if the
  4751. protocol doesn't work with network */
  4752. proxy = NULL;
  4753. }
  4754. /***********************************************************************
  4755. * If this is supposed to use a proxy, we need to figure out the proxy host
  4756. * name, proxy type and port number, so that we can re-use an existing
  4757. * connection that may exist registered to the same proxy host.
  4758. ***********************************************************************/
  4759. if(proxy) {
  4760. result = parse_proxy(data, conn, proxy);
  4761. Curl_safefree(proxy); /* parse_proxy copies the proxy string */
  4762. if(result)
  4763. goto out;
  4764. if((conn->proxytype == CURLPROXY_HTTP) ||
  4765. (conn->proxytype == CURLPROXY_HTTP_1_0)) {
  4766. #ifdef CURL_DISABLE_HTTP
  4767. /* asking for a HTTP proxy is a bit funny when HTTP is disabled... */
  4768. result = CURLE_UNSUPPORTED_PROTOCOL;
  4769. goto out;
  4770. #else
  4771. /* force this connection's protocol to become HTTP if not already
  4772. compatible - if it isn't tunneling through */
  4773. if(!(conn->handler->protocol & PROTO_FAMILY_HTTP) &&
  4774. !conn->bits.tunnel_proxy)
  4775. conn->handler = &Curl_handler_http;
  4776. conn->bits.httpproxy = TRUE;
  4777. #endif
  4778. }
  4779. else
  4780. conn->bits.httpproxy = FALSE; /* not a HTTP proxy */
  4781. conn->bits.proxy = TRUE;
  4782. }
  4783. else {
  4784. /* we aren't using the proxy after all... */
  4785. conn->bits.proxy = FALSE;
  4786. conn->bits.httpproxy = FALSE;
  4787. conn->bits.proxy_user_passwd = FALSE;
  4788. conn->bits.tunnel_proxy = FALSE;
  4789. }
  4790. #endif /* CURL_DISABLE_PROXY */
  4791. /*************************************************************
  4792. * If the protocol is using SSL and HTTP proxy is used, we set
  4793. * the tunnel_proxy bit.
  4794. *************************************************************/
  4795. if((conn->given->flags&PROTOPT_SSL) && conn->bits.httpproxy)
  4796. conn->bits.tunnel_proxy = TRUE;
  4797. /*************************************************************
  4798. * Figure out the remote port number and fix it in the URL
  4799. *************************************************************/
  4800. result = parse_remote_port(data, conn);
  4801. if(result)
  4802. goto out;
  4803. /* Check for overridden login details and set them accordingly so they
  4804. they are known when protocol->setup_connection is called! */
  4805. result = override_login(data, conn, &user, &passwd, &options);
  4806. if(result)
  4807. goto out;
  4808. result = set_login(conn, user, passwd, options);
  4809. if(result)
  4810. goto out;
  4811. /*************************************************************
  4812. * Setup internals depending on protocol. Needs to be done after
  4813. * we figured out what/if proxy to use.
  4814. *************************************************************/
  4815. result = setup_connection_internals(conn);
  4816. if(result)
  4817. goto out;
  4818. conn->recv[FIRSTSOCKET] = Curl_recv_plain;
  4819. conn->send[FIRSTSOCKET] = Curl_send_plain;
  4820. conn->recv[SECONDARYSOCKET] = Curl_recv_plain;
  4821. conn->send[SECONDARYSOCKET] = Curl_send_plain;
  4822. /***********************************************************************
  4823. * file: is a special case in that it doesn't need a network connection
  4824. ***********************************************************************/
  4825. #ifndef CURL_DISABLE_FILE
  4826. if(conn->handler->flags & PROTOPT_NONETWORK) {
  4827. bool done;
  4828. /* this is supposed to be the connect function so we better at least check
  4829. that the file is present here! */
  4830. DEBUGASSERT(conn->handler->connect_it);
  4831. result = conn->handler->connect_it(conn, &done);
  4832. /* Setup a "faked" transfer that'll do nothing */
  4833. if(!result) {
  4834. conn->data = data;
  4835. conn->bits.tcpconnect[FIRSTSOCKET] = TRUE; /* we are "connected */
  4836. ConnectionStore(data, conn);
  4837. /*
  4838. * Setup whatever necessary for a resumed transfer
  4839. */
  4840. result = setup_range(data);
  4841. if(result) {
  4842. DEBUGASSERT(conn->handler->done);
  4843. /* we ignore the return code for the protocol-specific DONE */
  4844. (void)conn->handler->done(conn, result, FALSE);
  4845. goto out;
  4846. }
  4847. Curl_setup_transfer(conn, -1, -1, FALSE, NULL, /* no download */
  4848. -1, NULL); /* no upload */
  4849. }
  4850. /* since we skip do_init() */
  4851. do_init(conn);
  4852. goto out;
  4853. }
  4854. #endif
  4855. /* Get a cloned copy of the SSL config situation stored in the
  4856. connection struct. But to get this going nicely, we must first make
  4857. sure that the strings in the master copy are pointing to the correct
  4858. strings in the session handle strings array!
  4859. Keep in mind that the pointers in the master copy are pointing to strings
  4860. that will be freed as part of the SessionHandle struct, but all cloned
  4861. copies will be separately allocated.
  4862. */
  4863. data->set.ssl.CApath = data->set.str[STRING_SSL_CAPATH];
  4864. data->set.ssl.CAfile = data->set.str[STRING_SSL_CAFILE];
  4865. data->set.ssl.CRLfile = data->set.str[STRING_SSL_CRLFILE];
  4866. data->set.ssl.issuercert = data->set.str[STRING_SSL_ISSUERCERT];
  4867. data->set.ssl.random_file = data->set.str[STRING_SSL_RANDOM_FILE];
  4868. data->set.ssl.egdsocket = data->set.str[STRING_SSL_EGDSOCKET];
  4869. data->set.ssl.cipher_list = data->set.str[STRING_SSL_CIPHER_LIST];
  4870. #ifdef USE_TLS_SRP
  4871. data->set.ssl.username = data->set.str[STRING_TLSAUTH_USERNAME];
  4872. data->set.ssl.password = data->set.str[STRING_TLSAUTH_PASSWORD];
  4873. #endif
  4874. if(!Curl_clone_ssl_config(&data->set.ssl, &conn->ssl_config)) {
  4875. result = CURLE_OUT_OF_MEMORY;
  4876. goto out;
  4877. }
  4878. prune_dead_connections(data);
  4879. /*************************************************************
  4880. * Check the current list of connections to see if we can
  4881. * re-use an already existing one or if we have to create a
  4882. * new one.
  4883. *************************************************************/
  4884. /* reuse_fresh is TRUE if we are told to use a new connection by force, but
  4885. we only acknowledge this option if this is not a re-used connection
  4886. already (which happens due to follow-location or during a HTTP
  4887. authentication phase). */
  4888. if(data->set.reuse_fresh && !data->state.this_is_a_follow)
  4889. reuse = FALSE;
  4890. else
  4891. reuse = ConnectionExists(data, conn, &conn_temp, &force_reuse);
  4892. /* If we found a reusable connection, we may still want to
  4893. open a new connection if we are pipelining. */
  4894. if(reuse && !force_reuse && IsPipeliningPossible(data, conn_temp)) {
  4895. size_t pipelen = conn_temp->send_pipe->size + conn_temp->recv_pipe->size;
  4896. if(pipelen > 0) {
  4897. infof(data, "Found connection %ld, with requests in the pipe (%zu)\n",
  4898. conn_temp->connection_id, pipelen);
  4899. if(conn_temp->bundle->num_connections < max_host_connections &&
  4900. data->state.conn_cache->num_connections < max_total_connections) {
  4901. /* We want a new connection anyway */
  4902. reuse = FALSE;
  4903. infof(data, "We can reuse, but we want a new connection anyway\n");
  4904. }
  4905. }
  4906. }
  4907. if(reuse) {
  4908. /*
  4909. * We already have a connection for this, we got the former connection
  4910. * in the conn_temp variable and thus we need to cleanup the one we
  4911. * just allocated before we can move along and use the previously
  4912. * existing one.
  4913. */
  4914. conn_temp->inuse = TRUE; /* mark this as being in use so that no other
  4915. handle in a multi stack may nick it */
  4916. reuse_conn(conn, conn_temp);
  4917. free(conn); /* we don't need this anymore */
  4918. conn = conn_temp;
  4919. *in_connect = conn;
  4920. /* set a pointer to the hostname we display */
  4921. fix_hostname(data, conn, &conn->host);
  4922. infof(data, "Re-using existing connection! (#%ld) with host %s\n",
  4923. conn->connection_id,
  4924. conn->proxy.name?conn->proxy.dispname:conn->host.dispname);
  4925. }
  4926. else {
  4927. /* We have decided that we want a new connection. However, we may not
  4928. be able to do that if we have reached the limit of how many
  4929. connections we are allowed to open. */
  4930. struct connectbundle *bundle;
  4931. bundle = Curl_conncache_find_bundle(data->state.conn_cache,
  4932. conn->host.name);
  4933. if(max_host_connections > 0 && bundle &&
  4934. (bundle->num_connections >= max_host_connections)) {
  4935. struct connectdata *conn_candidate;
  4936. /* The bundle is full. Let's see if we can kill a connection. */
  4937. conn_candidate = find_oldest_idle_connection_in_bundle(data, bundle);
  4938. if(conn_candidate) {
  4939. /* Set the connection's owner correctly, then kill it */
  4940. conn_candidate->data = data;
  4941. (void)Curl_disconnect(conn_candidate, /* dead_connection */ FALSE);
  4942. }
  4943. else
  4944. no_connections_available = TRUE;
  4945. }
  4946. if(max_total_connections > 0 &&
  4947. (data->state.conn_cache->num_connections >= max_total_connections)) {
  4948. struct connectdata *conn_candidate;
  4949. /* The cache is full. Let's see if we can kill a connection. */
  4950. conn_candidate = find_oldest_idle_connection(data);
  4951. if(conn_candidate) {
  4952. /* Set the connection's owner correctly, then kill it */
  4953. conn_candidate->data = data;
  4954. (void)Curl_disconnect(conn_candidate, /* dead_connection */ FALSE);
  4955. }
  4956. else
  4957. no_connections_available = TRUE;
  4958. }
  4959. if(no_connections_available) {
  4960. infof(data, "No connections available.\n");
  4961. conn_free(conn);
  4962. *in_connect = NULL;
  4963. result = CURLE_NO_CONNECTION_AVAILABLE;
  4964. goto out;
  4965. }
  4966. else {
  4967. /*
  4968. * This is a brand new connection, so let's store it in the connection
  4969. * cache of ours!
  4970. */
  4971. ConnectionStore(data, conn);
  4972. }
  4973. #if defined(USE_NTLM)
  4974. /* If NTLM is requested in a part of this connection, make sure we don't
  4975. assume the state is fine as this is a fresh connection and NTLM is
  4976. connection based. */
  4977. if((data->state.authhost.picked & (CURLAUTH_NTLM | CURLAUTH_NTLM_WB)) &&
  4978. data->state.authhost.done) {
  4979. infof(data, "NTLM picked AND auth done set, clear picked!\n");
  4980. data->state.authhost.picked = CURLAUTH_NONE;
  4981. }
  4982. if((data->state.authproxy.picked & (CURLAUTH_NTLM | CURLAUTH_NTLM_WB)) &&
  4983. data->state.authproxy.done) {
  4984. infof(data, "NTLM-proxy picked AND auth done set, clear picked!\n");
  4985. data->state.authproxy.picked = CURLAUTH_NONE;
  4986. }
  4987. #endif
  4988. }
  4989. /* Mark the connection as used */
  4990. conn->inuse = TRUE;
  4991. /* Setup and init stuff before DO starts, in preparing for the transfer. */
  4992. do_init(conn);
  4993. /*
  4994. * Setup whatever necessary for a resumed transfer
  4995. */
  4996. result = setup_range(data);
  4997. if(result)
  4998. goto out;
  4999. /* Continue connectdata initialization here. */
  5000. /*
  5001. * Inherit the proper values from the urldata struct AFTER we have arranged
  5002. * the persistent connection stuff
  5003. */
  5004. conn->fread_func = data->set.fread_func;
  5005. conn->fread_in = data->set.in;
  5006. conn->seek_func = data->set.seek_func;
  5007. conn->seek_client = data->set.seek_client;
  5008. /*************************************************************
  5009. * Resolve the address of the server or proxy
  5010. *************************************************************/
  5011. result = resolve_server(data, conn, async);
  5012. out:
  5013. Curl_safefree(options);
  5014. Curl_safefree(passwd);
  5015. Curl_safefree(user);
  5016. Curl_safefree(proxy);
  5017. return result;
  5018. }
  5019. /* Curl_setup_conn() is called after the name resolve initiated in
  5020. * create_conn() is all done.
  5021. *
  5022. * Curl_setup_conn() also handles reused connections
  5023. *
  5024. * conn->data MUST already have been setup fine (in create_conn)
  5025. */
  5026. CURLcode Curl_setup_conn(struct connectdata *conn,
  5027. bool *protocol_done)
  5028. {
  5029. CURLcode result = CURLE_OK;
  5030. struct SessionHandle *data = conn->data;
  5031. Curl_pgrsTime(data, TIMER_NAMELOOKUP);
  5032. if(conn->handler->flags & PROTOPT_NONETWORK) {
  5033. /* nothing to setup when not using a network */
  5034. *protocol_done = TRUE;
  5035. return result;
  5036. }
  5037. *protocol_done = FALSE; /* default to not done */
  5038. /* set proxy_connect_closed to false unconditionally already here since it
  5039. is used strictly to provide extra information to a parent function in the
  5040. case of proxy CONNECT failures and we must make sure we don't have it
  5041. lingering set from a previous invoke */
  5042. conn->bits.proxy_connect_closed = FALSE;
  5043. /*
  5044. * Set user-agent. Used for HTTP, but since we can attempt to tunnel
  5045. * basically anything through a http proxy we can't limit this based on
  5046. * protocol.
  5047. */
  5048. if(data->set.str[STRING_USERAGENT]) {
  5049. Curl_safefree(conn->allocptr.uagent);
  5050. conn->allocptr.uagent =
  5051. aprintf("User-Agent: %s\r\n", data->set.str[STRING_USERAGENT]);
  5052. if(!conn->allocptr.uagent)
  5053. return CURLE_OUT_OF_MEMORY;
  5054. }
  5055. data->req.headerbytecount = 0;
  5056. #ifdef CURL_DO_LINEEND_CONV
  5057. data->state.crlf_conversions = 0; /* reset CRLF conversion counter */
  5058. #endif /* CURL_DO_LINEEND_CONV */
  5059. /* set start time here for timeout purposes in the connect procedure, it
  5060. is later set again for the progress meter purpose */
  5061. conn->now = Curl_tvnow();
  5062. if(CURL_SOCKET_BAD == conn->sock[FIRSTSOCKET]) {
  5063. conn->bits.tcpconnect[FIRSTSOCKET] = FALSE;
  5064. result = Curl_connecthost(conn, conn->dns_entry);
  5065. if(result)
  5066. return result;
  5067. }
  5068. else {
  5069. Curl_pgrsTime(data, TIMER_CONNECT); /* we're connected already */
  5070. Curl_pgrsTime(data, TIMER_APPCONNECT); /* we're connected already */
  5071. conn->bits.tcpconnect[FIRSTSOCKET] = TRUE;
  5072. *protocol_done = TRUE;
  5073. Curl_updateconninfo(conn, conn->sock[FIRSTSOCKET]);
  5074. Curl_verboseconnect(conn);
  5075. }
  5076. conn->now = Curl_tvnow(); /* time this *after* the connect is done, we
  5077. set this here perhaps a second time */
  5078. #ifdef __EMX__
  5079. /*
  5080. * This check is quite a hack. We're calling _fsetmode to fix the problem
  5081. * with fwrite converting newline characters (you get mangled text files,
  5082. * and corrupted binary files when you download to stdout and redirect it to
  5083. * a file).
  5084. */
  5085. if((data->set.out)->_handle == NULL) {
  5086. _fsetmode(stdout, "b");
  5087. }
  5088. #endif
  5089. return result;
  5090. }
  5091. CURLcode Curl_connect(struct SessionHandle *data,
  5092. struct connectdata **in_connect,
  5093. bool *asyncp,
  5094. bool *protocol_done)
  5095. {
  5096. CURLcode result;
  5097. *asyncp = FALSE; /* assume synchronous resolves by default */
  5098. /* call the stuff that needs to be called */
  5099. result = create_conn(data, in_connect, asyncp);
  5100. if(!result) {
  5101. /* no error */
  5102. if((*in_connect)->send_pipe->size || (*in_connect)->recv_pipe->size)
  5103. /* pipelining */
  5104. *protocol_done = TRUE;
  5105. else if(!*asyncp) {
  5106. /* DNS resolution is done: that's either because this is a reused
  5107. connection, in which case DNS was unnecessary, or because DNS
  5108. really did finish already (synch resolver/fast async resolve) */
  5109. result = Curl_setup_conn(*in_connect, protocol_done);
  5110. }
  5111. }
  5112. if(result == CURLE_NO_CONNECTION_AVAILABLE) {
  5113. *in_connect = NULL;
  5114. return result;
  5115. }
  5116. if(result && *in_connect) {
  5117. /* We're not allowed to return failure with memory left allocated
  5118. in the connectdata struct, free those here */
  5119. Curl_disconnect(*in_connect, FALSE); /* close the connection */
  5120. *in_connect = NULL; /* return a NULL */
  5121. }
  5122. return result;
  5123. }
  5124. CURLcode Curl_done(struct connectdata **connp,
  5125. CURLcode status, /* an error if this is called after an
  5126. error was detected */
  5127. bool premature)
  5128. {
  5129. CURLcode result;
  5130. struct connectdata *conn;
  5131. struct SessionHandle *data;
  5132. DEBUGASSERT(*connp);
  5133. conn = *connp;
  5134. data = conn->data;
  5135. if(conn->bits.done)
  5136. /* Stop if Curl_done() has already been called */
  5137. return CURLE_OK;
  5138. Curl_getoff_all_pipelines(data, conn);
  5139. if((conn->send_pipe->size + conn->recv_pipe->size != 0 &&
  5140. !data->set.reuse_forbid &&
  5141. !conn->bits.close))
  5142. /* Stop if pipeline is not empty and we do not have to close
  5143. connection. */
  5144. return CURLE_OK;
  5145. conn->bits.done = TRUE; /* called just now! */
  5146. /* Cleanup possible redirect junk */
  5147. if(data->req.newurl) {
  5148. free(data->req.newurl);
  5149. data->req.newurl = NULL;
  5150. }
  5151. if(data->req.location) {
  5152. free(data->req.location);
  5153. data->req.location = NULL;
  5154. }
  5155. Curl_resolver_cancel(conn);
  5156. if(conn->dns_entry) {
  5157. Curl_resolv_unlock(data, conn->dns_entry); /* done with this */
  5158. conn->dns_entry = NULL;
  5159. }
  5160. switch(status) {
  5161. case CURLE_ABORTED_BY_CALLBACK:
  5162. case CURLE_READ_ERROR:
  5163. case CURLE_WRITE_ERROR:
  5164. /* When we're aborted due to a callback return code it basically have to
  5165. be counted as premature as there is trouble ahead if we don't. We have
  5166. many callbacks and protocols work differently, we could potentially do
  5167. this more fine-grained in the future. */
  5168. premature = TRUE;
  5169. default:
  5170. break;
  5171. }
  5172. /* this calls the protocol-specific function pointer previously set */
  5173. if(conn->handler->done)
  5174. result = conn->handler->done(conn, status, premature);
  5175. else
  5176. result = status;
  5177. if(!result && Curl_pgrsDone(conn))
  5178. result = CURLE_ABORTED_BY_CALLBACK;
  5179. /* if the transfer was completed in a paused state there can be buffered
  5180. data left to write and then kill */
  5181. if(data->state.tempwrite) {
  5182. free(data->state.tempwrite);
  5183. data->state.tempwrite = NULL;
  5184. }
  5185. /* if data->set.reuse_forbid is TRUE, it means the libcurl client has
  5186. forced us to close this connection. This is ignored for requests taking
  5187. place in a NTLM authentication handshake
  5188. if conn->bits.close is TRUE, it means that the connection should be
  5189. closed in spite of all our efforts to be nice, due to protocol
  5190. restrictions in our or the server's end
  5191. if premature is TRUE, it means this connection was said to be DONE before
  5192. the entire request operation is complete and thus we can't know in what
  5193. state it is for re-using, so we're forced to close it. In a perfect world
  5194. we can add code that keep track of if we really must close it here or not,
  5195. but currently we have no such detail knowledge.
  5196. */
  5197. if((data->set.reuse_forbid
  5198. #if defined(USE_NTLM)
  5199. && !(conn->ntlm.state == NTLMSTATE_TYPE2 ||
  5200. conn->proxyntlm.state == NTLMSTATE_TYPE2)
  5201. #endif
  5202. ) || conn->bits.close || premature) {
  5203. CURLcode res2 = Curl_disconnect(conn, premature); /* close connection */
  5204. /* If we had an error already, make sure we return that one. But
  5205. if we got a new error, return that. */
  5206. if(!result && res2)
  5207. result = res2;
  5208. }
  5209. else {
  5210. /* the connection is no longer in use */
  5211. if(ConnectionDone(data, conn)) {
  5212. /* remember the most recently used connection */
  5213. data->state.lastconnect = conn;
  5214. infof(data, "Connection #%ld to host %s left intact\n",
  5215. conn->connection_id,
  5216. conn->bits.httpproxy?conn->proxy.dispname:conn->host.dispname);
  5217. }
  5218. else
  5219. data->state.lastconnect = NULL;
  5220. }
  5221. *connp = NULL; /* to make the caller of this function better detect that
  5222. this was either closed or handed over to the connection
  5223. cache here, and therefore cannot be used from this point on
  5224. */
  5225. Curl_free_request_state(data);
  5226. return result;
  5227. }
  5228. /*
  5229. * do_init() inits the readwrite session. This is inited each time (in the DO
  5230. * function before the protocol-specific DO functions are invoked) for a
  5231. * transfer, sometimes multiple times on the same SessionHandle. Make sure
  5232. * nothing in here depends on stuff that are setup dynamically for the
  5233. * transfer.
  5234. */
  5235. static CURLcode do_init(struct connectdata *conn)
  5236. {
  5237. struct SessionHandle *data = conn->data;
  5238. struct SingleRequest *k = &data->req;
  5239. conn->bits.done = FALSE; /* Curl_done() is not called yet */
  5240. conn->bits.do_more = FALSE; /* by default there's no curl_do_more() to use */
  5241. data->state.expect100header = FALSE;
  5242. if(data->set.opt_no_body)
  5243. /* in HTTP lingo, no body means using the HEAD request... */
  5244. data->set.httpreq = HTTPREQ_HEAD;
  5245. else if(HTTPREQ_HEAD == data->set.httpreq)
  5246. /* ... but if unset there really is no perfect method that is the
  5247. "opposite" of HEAD but in reality most people probably think GET
  5248. then. The important thing is that we can't let it remain HEAD if the
  5249. opt_no_body is set FALSE since then we'll behave wrong when getting
  5250. HTTP. */
  5251. data->set.httpreq = HTTPREQ_GET;
  5252. k->start = Curl_tvnow(); /* start time */
  5253. k->now = k->start; /* current time is now */
  5254. k->header = TRUE; /* assume header */
  5255. k->bytecount = 0;
  5256. k->buf = data->state.buffer;
  5257. k->uploadbuf = data->state.uploadbuffer;
  5258. k->hbufp = data->state.headerbuff;
  5259. k->ignorebody=FALSE;
  5260. Curl_speedinit(data);
  5261. Curl_pgrsSetUploadCounter(data, 0);
  5262. Curl_pgrsSetDownloadCounter(data, 0);
  5263. return CURLE_OK;
  5264. }
  5265. /*
  5266. * do_complete is called when the DO actions are complete.
  5267. *
  5268. * We init chunking and trailer bits to their default values here immediately
  5269. * before receiving any header data for the current request in the pipeline.
  5270. */
  5271. static void do_complete(struct connectdata *conn)
  5272. {
  5273. conn->data->req.chunk=FALSE;
  5274. conn->data->req.maxfd = (conn->sockfd>conn->writesockfd?
  5275. conn->sockfd:conn->writesockfd)+1;
  5276. Curl_pgrsTime(conn->data, TIMER_PRETRANSFER);
  5277. }
  5278. CURLcode Curl_do(struct connectdata **connp, bool *done)
  5279. {
  5280. CURLcode result=CURLE_OK;
  5281. struct connectdata *conn = *connp;
  5282. struct SessionHandle *data = conn->data;
  5283. if(conn->handler->do_it) {
  5284. /* generic protocol-specific function pointer set in curl_connect() */
  5285. result = conn->handler->do_it(conn, done);
  5286. /* This was formerly done in transfer.c, but we better do it here */
  5287. if((CURLE_SEND_ERROR == result) && conn->bits.reuse) {
  5288. /*
  5289. * If the connection is using an easy handle, call reconnect
  5290. * to re-establish the connection. Otherwise, let the multi logic
  5291. * figure out how to re-establish the connection.
  5292. */
  5293. if(!data->multi) {
  5294. result = Curl_reconnect_request(connp);
  5295. if(!result) {
  5296. /* ... finally back to actually retry the DO phase */
  5297. conn = *connp; /* re-assign conn since Curl_reconnect_request
  5298. creates a new connection */
  5299. result = conn->handler->do_it(conn, done);
  5300. }
  5301. }
  5302. else
  5303. return result;
  5304. }
  5305. if(!result && *done)
  5306. /* do_complete must be called after the protocol-specific DO function */
  5307. do_complete(conn);
  5308. }
  5309. return result;
  5310. }
  5311. /*
  5312. * Curl_do_more() is called during the DO_MORE multi state. It is basically a
  5313. * second stage DO state which (wrongly) was introduced to support FTP's
  5314. * second connection.
  5315. *
  5316. * TODO: A future libcurl should be able to work away this state.
  5317. *
  5318. * 'complete' can return 0 for incomplete, 1 for done and -1 for go back to
  5319. * DOING state there's more work to do!
  5320. */
  5321. CURLcode Curl_do_more(struct connectdata *conn, int *complete)
  5322. {
  5323. CURLcode result=CURLE_OK;
  5324. *complete = 0;
  5325. if(conn->handler->do_more)
  5326. result = conn->handler->do_more(conn, complete);
  5327. if(!result && (*complete == 1))
  5328. /* do_complete must be called after the protocol-specific DO function */
  5329. do_complete(conn);
  5330. return result;
  5331. }