url.c 219 KB

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