url.c 163 KB

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