url.c 182 KB

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