url.c 215 KB

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