url.c 217 KB

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