url.c 166 KB

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