url.c 195 KB

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