url.c 217 KB

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