url.c 159 KB

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