url.c 181 KB

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