url.c 161 KB

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