http.c 152 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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 https://curl.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifndef CURL_DISABLE_HTTP
  26. #ifdef HAVE_NETINET_IN_H
  27. #include <netinet/in.h>
  28. #endif
  29. #ifdef HAVE_NETDB_H
  30. #include <netdb.h>
  31. #endif
  32. #ifdef HAVE_ARPA_INET_H
  33. #include <arpa/inet.h>
  34. #endif
  35. #ifdef HAVE_NET_IF_H
  36. #include <net/if.h>
  37. #endif
  38. #ifdef HAVE_SYS_IOCTL_H
  39. #include <sys/ioctl.h>
  40. #endif
  41. #ifdef HAVE_SYS_PARAM_H
  42. #include <sys/param.h>
  43. #endif
  44. #ifdef USE_HYPER
  45. #include <hyper.h>
  46. #endif
  47. #include "urldata.h"
  48. #include <curl/curl.h>
  49. #include "transfer.h"
  50. #include "sendf.h"
  51. #include "formdata.h"
  52. #include "mime.h"
  53. #include "progress.h"
  54. #include "curl_base64.h"
  55. #include "cookie.h"
  56. #include "vauth/vauth.h"
  57. #include "vtls/vtls.h"
  58. #include "vquic/vquic.h"
  59. #include "http_digest.h"
  60. #include "http_ntlm.h"
  61. #include "curl_ntlm_wb.h"
  62. #include "http_negotiate.h"
  63. #include "http_aws_sigv4.h"
  64. #include "url.h"
  65. #include "share.h"
  66. #include "hostip.h"
  67. #include "dynhds.h"
  68. #include "http.h"
  69. #include "select.h"
  70. #include "parsedate.h" /* for the week day and month names */
  71. #include "strtoofft.h"
  72. #include "multiif.h"
  73. #include "strcase.h"
  74. #include "content_encoding.h"
  75. #include "http_proxy.h"
  76. #include "warnless.h"
  77. #include "http2.h"
  78. #include "cfilters.h"
  79. #include "connect.h"
  80. #include "strdup.h"
  81. #include "altsvc.h"
  82. #include "hsts.h"
  83. #include "ws.h"
  84. #include "c-hyper.h"
  85. #include "curl_ctype.h"
  86. /* The last 3 #include files should be in this order */
  87. #include "curl_printf.h"
  88. #include "curl_memory.h"
  89. #include "memdebug.h"
  90. /*
  91. * Forward declarations.
  92. */
  93. static int http_getsock_do(struct Curl_easy *data,
  94. struct connectdata *conn,
  95. curl_socket_t *socks);
  96. static bool http_should_fail(struct Curl_easy *data);
  97. static CURLcode http_setup_conn(struct Curl_easy *data,
  98. struct connectdata *conn);
  99. #ifdef USE_WEBSOCKETS
  100. static CURLcode ws_setup_conn(struct Curl_easy *data,
  101. struct connectdata *conn);
  102. #endif
  103. /*
  104. * HTTP handler interface.
  105. */
  106. const struct Curl_handler Curl_handler_http = {
  107. "HTTP", /* scheme */
  108. http_setup_conn, /* setup_connection */
  109. Curl_http, /* do_it */
  110. Curl_http_done, /* done */
  111. ZERO_NULL, /* do_more */
  112. Curl_http_connect, /* connect_it */
  113. ZERO_NULL, /* connecting */
  114. ZERO_NULL, /* doing */
  115. ZERO_NULL, /* proto_getsock */
  116. http_getsock_do, /* doing_getsock */
  117. ZERO_NULL, /* domore_getsock */
  118. ZERO_NULL, /* perform_getsock */
  119. ZERO_NULL, /* disconnect */
  120. ZERO_NULL, /* readwrite */
  121. ZERO_NULL, /* connection_check */
  122. ZERO_NULL, /* attach connection */
  123. PORT_HTTP, /* defport */
  124. CURLPROTO_HTTP, /* protocol */
  125. CURLPROTO_HTTP, /* family */
  126. PROTOPT_CREDSPERREQUEST | /* flags */
  127. PROTOPT_USERPWDCTRL
  128. };
  129. #ifdef USE_WEBSOCKETS
  130. const struct Curl_handler Curl_handler_ws = {
  131. "WS", /* scheme */
  132. ws_setup_conn, /* setup_connection */
  133. Curl_http, /* do_it */
  134. Curl_http_done, /* done */
  135. ZERO_NULL, /* do_more */
  136. Curl_http_connect, /* connect_it */
  137. ZERO_NULL, /* connecting */
  138. ZERO_NULL, /* doing */
  139. ZERO_NULL, /* proto_getsock */
  140. http_getsock_do, /* doing_getsock */
  141. ZERO_NULL, /* domore_getsock */
  142. ZERO_NULL, /* perform_getsock */
  143. Curl_ws_disconnect, /* disconnect */
  144. ZERO_NULL, /* readwrite */
  145. ZERO_NULL, /* connection_check */
  146. ZERO_NULL, /* attach connection */
  147. PORT_HTTP, /* defport */
  148. CURLPROTO_WS, /* protocol */
  149. CURLPROTO_HTTP, /* family */
  150. PROTOPT_CREDSPERREQUEST | /* flags */
  151. PROTOPT_USERPWDCTRL
  152. };
  153. #endif
  154. #ifdef USE_SSL
  155. /*
  156. * HTTPS handler interface.
  157. */
  158. const struct Curl_handler Curl_handler_https = {
  159. "HTTPS", /* scheme */
  160. http_setup_conn, /* setup_connection */
  161. Curl_http, /* do_it */
  162. Curl_http_done, /* done */
  163. ZERO_NULL, /* do_more */
  164. Curl_http_connect, /* connect_it */
  165. NULL, /* connecting */
  166. ZERO_NULL, /* doing */
  167. NULL, /* proto_getsock */
  168. http_getsock_do, /* doing_getsock */
  169. ZERO_NULL, /* domore_getsock */
  170. ZERO_NULL, /* perform_getsock */
  171. ZERO_NULL, /* disconnect */
  172. ZERO_NULL, /* readwrite */
  173. ZERO_NULL, /* connection_check */
  174. ZERO_NULL, /* attach connection */
  175. PORT_HTTPS, /* defport */
  176. CURLPROTO_HTTPS, /* protocol */
  177. CURLPROTO_HTTP, /* family */
  178. PROTOPT_SSL | PROTOPT_CREDSPERREQUEST | PROTOPT_ALPN | /* flags */
  179. PROTOPT_USERPWDCTRL
  180. };
  181. #ifdef USE_WEBSOCKETS
  182. const struct Curl_handler Curl_handler_wss = {
  183. "WSS", /* scheme */
  184. ws_setup_conn, /* setup_connection */
  185. Curl_http, /* do_it */
  186. Curl_http_done, /* done */
  187. ZERO_NULL, /* do_more */
  188. Curl_http_connect, /* connect_it */
  189. NULL, /* connecting */
  190. ZERO_NULL, /* doing */
  191. NULL, /* proto_getsock */
  192. http_getsock_do, /* doing_getsock */
  193. ZERO_NULL, /* domore_getsock */
  194. ZERO_NULL, /* perform_getsock */
  195. Curl_ws_disconnect, /* disconnect */
  196. ZERO_NULL, /* readwrite */
  197. ZERO_NULL, /* connection_check */
  198. ZERO_NULL, /* attach connection */
  199. PORT_HTTPS, /* defport */
  200. CURLPROTO_WSS, /* protocol */
  201. CURLPROTO_HTTP, /* family */
  202. PROTOPT_SSL | PROTOPT_CREDSPERREQUEST | /* flags */
  203. PROTOPT_USERPWDCTRL
  204. };
  205. #endif
  206. #endif
  207. static CURLcode http_setup_conn(struct Curl_easy *data,
  208. struct connectdata *conn)
  209. {
  210. /* allocate the HTTP-specific struct for the Curl_easy, only to survive
  211. during this request */
  212. struct HTTP *http;
  213. DEBUGASSERT(data->req.p.http == NULL);
  214. http = calloc(1, sizeof(struct HTTP));
  215. if(!http)
  216. return CURLE_OUT_OF_MEMORY;
  217. Curl_mime_initpart(&http->form);
  218. data->req.p.http = http;
  219. connkeep(conn, "HTTP default");
  220. if(data->state.httpwant == CURL_HTTP_VERSION_3ONLY) {
  221. CURLcode result = Curl_conn_may_http3(data, conn);
  222. if(result)
  223. return result;
  224. }
  225. return CURLE_OK;
  226. }
  227. #ifdef USE_WEBSOCKETS
  228. static CURLcode ws_setup_conn(struct Curl_easy *data,
  229. struct connectdata *conn)
  230. {
  231. /* websockets is 1.1 only (for now) */
  232. data->state.httpwant = CURL_HTTP_VERSION_1_1;
  233. return http_setup_conn(data, conn);
  234. }
  235. #endif
  236. #ifndef CURL_DISABLE_PROXY
  237. /*
  238. * checkProxyHeaders() checks the linked list of custom proxy headers
  239. * if proxy headers are not available, then it will lookup into http header
  240. * link list
  241. *
  242. * It takes a connectdata struct as input to see if this is a proxy request or
  243. * not, as it then might check a different header list. Provide the header
  244. * prefix without colon!
  245. */
  246. char *Curl_checkProxyheaders(struct Curl_easy *data,
  247. const struct connectdata *conn,
  248. const char *thisheader,
  249. const size_t thislen)
  250. {
  251. struct curl_slist *head;
  252. for(head = (conn->bits.proxy && data->set.sep_headers) ?
  253. data->set.proxyheaders : data->set.headers;
  254. head; head = head->next) {
  255. if(strncasecompare(head->data, thisheader, thislen) &&
  256. Curl_headersep(head->data[thislen]))
  257. return head->data;
  258. }
  259. return NULL;
  260. }
  261. #else
  262. /* disabled */
  263. #define Curl_checkProxyheaders(x,y,z,a) NULL
  264. #endif
  265. /*
  266. * Strip off leading and trailing whitespace from the value in the
  267. * given HTTP header line and return a strdupped copy. Returns NULL in
  268. * case of allocation failure. Returns an empty string if the header value
  269. * consists entirely of whitespace.
  270. */
  271. char *Curl_copy_header_value(const char *header)
  272. {
  273. const char *start;
  274. const char *end;
  275. char *value;
  276. size_t len;
  277. /* Find the end of the header name */
  278. while(*header && (*header != ':'))
  279. ++header;
  280. if(*header)
  281. /* Skip over colon */
  282. ++header;
  283. /* Find the first non-space letter */
  284. start = header;
  285. while(*start && ISSPACE(*start))
  286. start++;
  287. /* data is in the host encoding so
  288. use '\r' and '\n' instead of 0x0d and 0x0a */
  289. end = strchr(start, '\r');
  290. if(!end)
  291. end = strchr(start, '\n');
  292. if(!end)
  293. end = strchr(start, '\0');
  294. if(!end)
  295. return NULL;
  296. /* skip all trailing space letters */
  297. while((end > start) && ISSPACE(*end))
  298. end--;
  299. /* get length of the type */
  300. len = end - start + 1;
  301. value = malloc(len + 1);
  302. if(!value)
  303. return NULL;
  304. memcpy(value, start, len);
  305. value[len] = 0; /* null-terminate */
  306. return value;
  307. }
  308. #ifndef CURL_DISABLE_HTTP_AUTH
  309. /*
  310. * http_output_basic() sets up an Authorization: header (or the proxy version)
  311. * for HTTP Basic authentication.
  312. *
  313. * Returns CURLcode.
  314. */
  315. static CURLcode http_output_basic(struct Curl_easy *data, bool proxy)
  316. {
  317. size_t size = 0;
  318. char *authorization = NULL;
  319. char **userp;
  320. const char *user;
  321. const char *pwd;
  322. CURLcode result;
  323. char *out;
  324. /* credentials are unique per transfer for HTTP, do not use the ones for the
  325. connection */
  326. if(proxy) {
  327. #ifndef CURL_DISABLE_PROXY
  328. userp = &data->state.aptr.proxyuserpwd;
  329. user = data->state.aptr.proxyuser;
  330. pwd = data->state.aptr.proxypasswd;
  331. #else
  332. return CURLE_NOT_BUILT_IN;
  333. #endif
  334. }
  335. else {
  336. userp = &data->state.aptr.userpwd;
  337. user = data->state.aptr.user;
  338. pwd = data->state.aptr.passwd;
  339. }
  340. out = aprintf("%s:%s", user ? user : "", pwd ? pwd : "");
  341. if(!out)
  342. return CURLE_OUT_OF_MEMORY;
  343. result = Curl_base64_encode(out, strlen(out), &authorization, &size);
  344. if(result)
  345. goto fail;
  346. if(!authorization) {
  347. result = CURLE_REMOTE_ACCESS_DENIED;
  348. goto fail;
  349. }
  350. free(*userp);
  351. *userp = aprintf("%sAuthorization: Basic %s\r\n",
  352. proxy ? "Proxy-" : "",
  353. authorization);
  354. free(authorization);
  355. if(!*userp) {
  356. result = CURLE_OUT_OF_MEMORY;
  357. goto fail;
  358. }
  359. fail:
  360. free(out);
  361. return result;
  362. }
  363. /*
  364. * http_output_bearer() sets up an Authorization: header
  365. * for HTTP Bearer authentication.
  366. *
  367. * Returns CURLcode.
  368. */
  369. static CURLcode http_output_bearer(struct Curl_easy *data)
  370. {
  371. char **userp;
  372. CURLcode result = CURLE_OK;
  373. userp = &data->state.aptr.userpwd;
  374. free(*userp);
  375. *userp = aprintf("Authorization: Bearer %s\r\n",
  376. data->set.str[STRING_BEARER]);
  377. if(!*userp) {
  378. result = CURLE_OUT_OF_MEMORY;
  379. goto fail;
  380. }
  381. fail:
  382. return result;
  383. }
  384. #endif
  385. /* pickoneauth() selects the most favourable authentication method from the
  386. * ones available and the ones we want.
  387. *
  388. * return TRUE if one was picked
  389. */
  390. static bool pickoneauth(struct auth *pick, unsigned long mask)
  391. {
  392. bool picked;
  393. /* only deal with authentication we want */
  394. unsigned long avail = pick->avail & pick->want & mask;
  395. picked = TRUE;
  396. /* The order of these checks is highly relevant, as this will be the order
  397. of preference in case of the existence of multiple accepted types. */
  398. if(avail & CURLAUTH_NEGOTIATE)
  399. pick->picked = CURLAUTH_NEGOTIATE;
  400. else if(avail & CURLAUTH_BEARER)
  401. pick->picked = CURLAUTH_BEARER;
  402. else if(avail & CURLAUTH_DIGEST)
  403. pick->picked = CURLAUTH_DIGEST;
  404. else if(avail & CURLAUTH_NTLM)
  405. pick->picked = CURLAUTH_NTLM;
  406. else if(avail & CURLAUTH_NTLM_WB)
  407. pick->picked = CURLAUTH_NTLM_WB;
  408. else if(avail & CURLAUTH_BASIC)
  409. pick->picked = CURLAUTH_BASIC;
  410. else if(avail & CURLAUTH_AWS_SIGV4)
  411. pick->picked = CURLAUTH_AWS_SIGV4;
  412. else {
  413. pick->picked = CURLAUTH_PICKNONE; /* we select to use nothing */
  414. picked = FALSE;
  415. }
  416. pick->avail = CURLAUTH_NONE; /* clear it here */
  417. return picked;
  418. }
  419. /*
  420. * http_perhapsrewind()
  421. *
  422. * If we are doing POST or PUT {
  423. * If we have more data to send {
  424. * If we are doing NTLM {
  425. * Keep sending since we must not disconnect
  426. * }
  427. * else {
  428. * If there is more than just a little data left to send, close
  429. * the current connection by force.
  430. * }
  431. * }
  432. * If we have sent any data {
  433. * If we don't have track of all the data {
  434. * call app to tell it to rewind
  435. * }
  436. * else {
  437. * rewind internally so that the operation can restart fine
  438. * }
  439. * }
  440. * }
  441. */
  442. static CURLcode http_perhapsrewind(struct Curl_easy *data,
  443. struct connectdata *conn)
  444. {
  445. struct HTTP *http = data->req.p.http;
  446. curl_off_t bytessent;
  447. curl_off_t expectsend = -1; /* default is unknown */
  448. if(!http)
  449. /* If this is still NULL, we have not reach very far and we can safely
  450. skip this rewinding stuff */
  451. return CURLE_OK;
  452. switch(data->state.httpreq) {
  453. case HTTPREQ_GET:
  454. case HTTPREQ_HEAD:
  455. return CURLE_OK;
  456. default:
  457. break;
  458. }
  459. bytessent = data->req.writebytecount;
  460. if(conn->bits.authneg) {
  461. /* This is a state where we are known to be negotiating and we don't send
  462. any data then. */
  463. expectsend = 0;
  464. }
  465. else if(!conn->bits.protoconnstart) {
  466. /* HTTP CONNECT in progress: there is no body */
  467. expectsend = 0;
  468. }
  469. else {
  470. /* figure out how much data we are expected to send */
  471. switch(data->state.httpreq) {
  472. case HTTPREQ_POST:
  473. case HTTPREQ_PUT:
  474. if(data->state.infilesize != -1)
  475. expectsend = data->state.infilesize;
  476. break;
  477. case HTTPREQ_POST_FORM:
  478. case HTTPREQ_POST_MIME:
  479. expectsend = http->postsize;
  480. break;
  481. default:
  482. break;
  483. }
  484. }
  485. data->state.rewindbeforesend = FALSE; /* default */
  486. if((expectsend == -1) || (expectsend > bytessent)) {
  487. #if defined(USE_NTLM)
  488. /* There is still data left to send */
  489. if((data->state.authproxy.picked == CURLAUTH_NTLM) ||
  490. (data->state.authhost.picked == CURLAUTH_NTLM) ||
  491. (data->state.authproxy.picked == CURLAUTH_NTLM_WB) ||
  492. (data->state.authhost.picked == CURLAUTH_NTLM_WB)) {
  493. if(((expectsend - bytessent) < 2000) ||
  494. (conn->http_ntlm_state != NTLMSTATE_NONE) ||
  495. (conn->proxy_ntlm_state != NTLMSTATE_NONE)) {
  496. /* The NTLM-negotiation has started *OR* there is just a little (<2K)
  497. data left to send, keep on sending. */
  498. /* rewind data when completely done sending! */
  499. if(!conn->bits.authneg && (conn->writesockfd != CURL_SOCKET_BAD)) {
  500. data->state.rewindbeforesend = TRUE;
  501. infof(data, "Rewind stream before next send");
  502. }
  503. return CURLE_OK;
  504. }
  505. if(conn->bits.close)
  506. /* this is already marked to get closed */
  507. return CURLE_OK;
  508. infof(data, "NTLM send, close instead of sending %"
  509. CURL_FORMAT_CURL_OFF_T " bytes",
  510. (curl_off_t)(expectsend - bytessent));
  511. }
  512. #endif
  513. #if defined(USE_SPNEGO)
  514. /* There is still data left to send */
  515. if((data->state.authproxy.picked == CURLAUTH_NEGOTIATE) ||
  516. (data->state.authhost.picked == CURLAUTH_NEGOTIATE)) {
  517. if(((expectsend - bytessent) < 2000) ||
  518. (conn->http_negotiate_state != GSS_AUTHNONE) ||
  519. (conn->proxy_negotiate_state != GSS_AUTHNONE)) {
  520. /* The NEGOTIATE-negotiation has started *OR*
  521. there is just a little (<2K) data left to send, keep on sending. */
  522. /* rewind data when completely done sending! */
  523. if(!conn->bits.authneg && (conn->writesockfd != CURL_SOCKET_BAD)) {
  524. data->state.rewindbeforesend = TRUE;
  525. infof(data, "Rewind stream before next send");
  526. }
  527. return CURLE_OK;
  528. }
  529. if(conn->bits.close)
  530. /* this is already marked to get closed */
  531. return CURLE_OK;
  532. infof(data, "NEGOTIATE send, close instead of sending %"
  533. CURL_FORMAT_CURL_OFF_T " bytes",
  534. (curl_off_t)(expectsend - bytessent));
  535. }
  536. #endif
  537. /* This is not NEGOTIATE/NTLM or many bytes left to send: close */
  538. streamclose(conn, "Mid-auth HTTP and much data left to send");
  539. data->req.size = 0; /* don't download any more than 0 bytes */
  540. /* There still is data left to send, but this connection is marked for
  541. closure so we can safely do the rewind right now */
  542. }
  543. if(bytessent) {
  544. /* mark for rewind since if we already sent something */
  545. data->state.rewindbeforesend = TRUE;
  546. infof(data, "Please rewind output before next send");
  547. }
  548. return CURLE_OK;
  549. }
  550. /*
  551. * Curl_http_auth_act() gets called when all HTTP headers have been received
  552. * and it checks what authentication methods that are available and decides
  553. * which one (if any) to use. It will set 'newurl' if an auth method was
  554. * picked.
  555. */
  556. CURLcode Curl_http_auth_act(struct Curl_easy *data)
  557. {
  558. struct connectdata *conn = data->conn;
  559. bool pickhost = FALSE;
  560. bool pickproxy = FALSE;
  561. CURLcode result = CURLE_OK;
  562. unsigned long authmask = ~0ul;
  563. if(!data->set.str[STRING_BEARER])
  564. authmask &= (unsigned long)~CURLAUTH_BEARER;
  565. if(100 <= data->req.httpcode && data->req.httpcode <= 199)
  566. /* this is a transient response code, ignore */
  567. return CURLE_OK;
  568. if(data->state.authproblem)
  569. return data->set.http_fail_on_error?CURLE_HTTP_RETURNED_ERROR:CURLE_OK;
  570. if((data->state.aptr.user || data->set.str[STRING_BEARER]) &&
  571. ((data->req.httpcode == 401) ||
  572. (conn->bits.authneg && data->req.httpcode < 300))) {
  573. pickhost = pickoneauth(&data->state.authhost, authmask);
  574. if(!pickhost)
  575. data->state.authproblem = TRUE;
  576. if(data->state.authhost.picked == CURLAUTH_NTLM &&
  577. conn->httpversion > 11) {
  578. infof(data, "Forcing HTTP/1.1 for NTLM");
  579. connclose(conn, "Force HTTP/1.1 connection");
  580. data->state.httpwant = CURL_HTTP_VERSION_1_1;
  581. }
  582. }
  583. #ifndef CURL_DISABLE_PROXY
  584. if(conn->bits.proxy_user_passwd &&
  585. ((data->req.httpcode == 407) ||
  586. (conn->bits.authneg && data->req.httpcode < 300))) {
  587. pickproxy = pickoneauth(&data->state.authproxy,
  588. authmask & ~CURLAUTH_BEARER);
  589. if(!pickproxy)
  590. data->state.authproblem = TRUE;
  591. }
  592. #endif
  593. if(pickhost || pickproxy) {
  594. if((data->state.httpreq != HTTPREQ_GET) &&
  595. (data->state.httpreq != HTTPREQ_HEAD) &&
  596. !data->state.rewindbeforesend) {
  597. result = http_perhapsrewind(data, conn);
  598. if(result)
  599. return result;
  600. }
  601. /* In case this is GSS auth, the newurl field is already allocated so
  602. we must make sure to free it before allocating a new one. As figured
  603. out in bug #2284386 */
  604. Curl_safefree(data->req.newurl);
  605. data->req.newurl = strdup(data->state.url); /* clone URL */
  606. if(!data->req.newurl)
  607. return CURLE_OUT_OF_MEMORY;
  608. }
  609. else if((data->req.httpcode < 300) &&
  610. (!data->state.authhost.done) &&
  611. conn->bits.authneg) {
  612. /* no (known) authentication available,
  613. authentication is not "done" yet and
  614. no authentication seems to be required and
  615. we didn't try HEAD or GET */
  616. if((data->state.httpreq != HTTPREQ_GET) &&
  617. (data->state.httpreq != HTTPREQ_HEAD)) {
  618. data->req.newurl = strdup(data->state.url); /* clone URL */
  619. if(!data->req.newurl)
  620. return CURLE_OUT_OF_MEMORY;
  621. data->state.authhost.done = TRUE;
  622. }
  623. }
  624. if(http_should_fail(data)) {
  625. failf(data, "The requested URL returned error: %d",
  626. data->req.httpcode);
  627. result = CURLE_HTTP_RETURNED_ERROR;
  628. }
  629. return result;
  630. }
  631. #ifndef CURL_DISABLE_HTTP_AUTH
  632. /*
  633. * Output the correct authentication header depending on the auth type
  634. * and whether or not it is to a proxy.
  635. */
  636. static CURLcode
  637. output_auth_headers(struct Curl_easy *data,
  638. struct connectdata *conn,
  639. struct auth *authstatus,
  640. const char *request,
  641. const char *path,
  642. bool proxy)
  643. {
  644. const char *auth = NULL;
  645. CURLcode result = CURLE_OK;
  646. (void)conn;
  647. #ifdef CURL_DISABLE_CRYPTO_AUTH
  648. (void)request;
  649. (void)path;
  650. #endif
  651. #ifndef CURL_DISABLE_CRYPTO_AUTH
  652. if(authstatus->picked == CURLAUTH_AWS_SIGV4) {
  653. auth = "AWS_SIGV4";
  654. result = Curl_output_aws_sigv4(data, proxy);
  655. if(result)
  656. return result;
  657. }
  658. else
  659. #endif
  660. #ifdef USE_SPNEGO
  661. if(authstatus->picked == CURLAUTH_NEGOTIATE) {
  662. auth = "Negotiate";
  663. result = Curl_output_negotiate(data, conn, proxy);
  664. if(result)
  665. return result;
  666. }
  667. else
  668. #endif
  669. #ifdef USE_NTLM
  670. if(authstatus->picked == CURLAUTH_NTLM) {
  671. auth = "NTLM";
  672. result = Curl_output_ntlm(data, proxy);
  673. if(result)
  674. return result;
  675. }
  676. else
  677. #endif
  678. #if defined(USE_NTLM) && defined(NTLM_WB_ENABLED)
  679. if(authstatus->picked == CURLAUTH_NTLM_WB) {
  680. auth = "NTLM_WB";
  681. result = Curl_output_ntlm_wb(data, conn, proxy);
  682. if(result)
  683. return result;
  684. }
  685. else
  686. #endif
  687. #ifndef CURL_DISABLE_CRYPTO_AUTH
  688. if(authstatus->picked == CURLAUTH_DIGEST) {
  689. auth = "Digest";
  690. result = Curl_output_digest(data,
  691. proxy,
  692. (const unsigned char *)request,
  693. (const unsigned char *)path);
  694. if(result)
  695. return result;
  696. }
  697. else
  698. #endif
  699. if(authstatus->picked == CURLAUTH_BASIC) {
  700. /* Basic */
  701. if(
  702. #ifndef CURL_DISABLE_PROXY
  703. (proxy && conn->bits.proxy_user_passwd &&
  704. !Curl_checkProxyheaders(data, conn, STRCONST("Proxy-authorization"))) ||
  705. #endif
  706. (!proxy && data->state.aptr.user &&
  707. !Curl_checkheaders(data, STRCONST("Authorization")))) {
  708. auth = "Basic";
  709. result = http_output_basic(data, proxy);
  710. if(result)
  711. return result;
  712. }
  713. /* NOTE: this function should set 'done' TRUE, as the other auth
  714. functions work that way */
  715. authstatus->done = TRUE;
  716. }
  717. if(authstatus->picked == CURLAUTH_BEARER) {
  718. /* Bearer */
  719. if((!proxy && data->set.str[STRING_BEARER] &&
  720. !Curl_checkheaders(data, STRCONST("Authorization")))) {
  721. auth = "Bearer";
  722. result = http_output_bearer(data);
  723. if(result)
  724. return result;
  725. }
  726. /* NOTE: this function should set 'done' TRUE, as the other auth
  727. functions work that way */
  728. authstatus->done = TRUE;
  729. }
  730. if(auth) {
  731. #ifndef CURL_DISABLE_PROXY
  732. infof(data, "%s auth using %s with user '%s'",
  733. proxy ? "Proxy" : "Server", auth,
  734. proxy ? (data->state.aptr.proxyuser ?
  735. data->state.aptr.proxyuser : "") :
  736. (data->state.aptr.user ?
  737. data->state.aptr.user : ""));
  738. #else
  739. infof(data, "Server auth using %s with user '%s'",
  740. auth, data->state.aptr.user ?
  741. data->state.aptr.user : "");
  742. #endif
  743. authstatus->multipass = (!authstatus->done) ? TRUE : FALSE;
  744. }
  745. else
  746. authstatus->multipass = FALSE;
  747. return CURLE_OK;
  748. }
  749. /**
  750. * Curl_http_output_auth() setups the authentication headers for the
  751. * host/proxy and the correct authentication
  752. * method. data->state.authdone is set to TRUE when authentication is
  753. * done.
  754. *
  755. * @param conn all information about the current connection
  756. * @param request pointer to the request keyword
  757. * @param path pointer to the requested path; should include query part
  758. * @param proxytunnel boolean if this is the request setting up a "proxy
  759. * tunnel"
  760. *
  761. * @returns CURLcode
  762. */
  763. CURLcode
  764. Curl_http_output_auth(struct Curl_easy *data,
  765. struct connectdata *conn,
  766. const char *request,
  767. Curl_HttpReq httpreq,
  768. const char *path,
  769. bool proxytunnel) /* TRUE if this is the request setting
  770. up the proxy tunnel */
  771. {
  772. CURLcode result = CURLE_OK;
  773. struct auth *authhost;
  774. struct auth *authproxy;
  775. DEBUGASSERT(data);
  776. authhost = &data->state.authhost;
  777. authproxy = &data->state.authproxy;
  778. if(
  779. #ifndef CURL_DISABLE_PROXY
  780. (conn->bits.httpproxy && conn->bits.proxy_user_passwd) ||
  781. #endif
  782. data->state.aptr.user || data->set.str[STRING_BEARER])
  783. /* continue please */;
  784. else {
  785. authhost->done = TRUE;
  786. authproxy->done = TRUE;
  787. return CURLE_OK; /* no authentication with no user or password */
  788. }
  789. if(authhost->want && !authhost->picked)
  790. /* The app has selected one or more methods, but none has been picked
  791. so far by a server round-trip. Then we set the picked one to the
  792. want one, and if this is one single bit it'll be used instantly. */
  793. authhost->picked = authhost->want;
  794. if(authproxy->want && !authproxy->picked)
  795. /* The app has selected one or more methods, but none has been picked so
  796. far by a proxy round-trip. Then we set the picked one to the want one,
  797. and if this is one single bit it'll be used instantly. */
  798. authproxy->picked = authproxy->want;
  799. #ifndef CURL_DISABLE_PROXY
  800. /* Send proxy authentication header if needed */
  801. if(conn->bits.httpproxy &&
  802. (conn->bits.tunnel_proxy == (bit)proxytunnel)) {
  803. result = output_auth_headers(data, conn, authproxy, request, path, TRUE);
  804. if(result)
  805. return result;
  806. }
  807. else
  808. #else
  809. (void)proxytunnel;
  810. #endif /* CURL_DISABLE_PROXY */
  811. /* we have no proxy so let's pretend we're done authenticating
  812. with it */
  813. authproxy->done = TRUE;
  814. /* To prevent the user+password to get sent to other than the original host
  815. due to a location-follow */
  816. if(Curl_auth_allowed_to_host(data)
  817. #ifndef CURL_DISABLE_NETRC
  818. || conn->bits.netrc
  819. #endif
  820. )
  821. result = output_auth_headers(data, conn, authhost, request, path, FALSE);
  822. else
  823. authhost->done = TRUE;
  824. if(((authhost->multipass && !authhost->done) ||
  825. (authproxy->multipass && !authproxy->done)) &&
  826. (httpreq != HTTPREQ_GET) &&
  827. (httpreq != HTTPREQ_HEAD)) {
  828. /* Auth is required and we are not authenticated yet. Make a PUT or POST
  829. with content-length zero as a "probe". */
  830. conn->bits.authneg = TRUE;
  831. }
  832. else
  833. conn->bits.authneg = FALSE;
  834. return result;
  835. }
  836. #else
  837. /* when disabled */
  838. CURLcode
  839. Curl_http_output_auth(struct Curl_easy *data,
  840. struct connectdata *conn,
  841. const char *request,
  842. Curl_HttpReq httpreq,
  843. const char *path,
  844. bool proxytunnel)
  845. {
  846. (void)data;
  847. (void)conn;
  848. (void)request;
  849. (void)httpreq;
  850. (void)path;
  851. (void)proxytunnel;
  852. return CURLE_OK;
  853. }
  854. #endif
  855. /*
  856. * Curl_http_input_auth() deals with Proxy-Authenticate: and WWW-Authenticate:
  857. * headers. They are dealt with both in the transfer.c main loop and in the
  858. * proxy CONNECT loop.
  859. */
  860. static int is_valid_auth_separator(char ch)
  861. {
  862. return ch == '\0' || ch == ',' || ISSPACE(ch);
  863. }
  864. CURLcode Curl_http_input_auth(struct Curl_easy *data, bool proxy,
  865. const char *auth) /* the first non-space */
  866. {
  867. /*
  868. * This resource requires authentication
  869. */
  870. struct connectdata *conn = data->conn;
  871. #ifdef USE_SPNEGO
  872. curlnegotiate *negstate = proxy ? &conn->proxy_negotiate_state :
  873. &conn->http_negotiate_state;
  874. #endif
  875. unsigned long *availp;
  876. struct auth *authp;
  877. (void) conn; /* In case conditionals make it unused. */
  878. if(proxy) {
  879. availp = &data->info.proxyauthavail;
  880. authp = &data->state.authproxy;
  881. }
  882. else {
  883. availp = &data->info.httpauthavail;
  884. authp = &data->state.authhost;
  885. }
  886. /*
  887. * Here we check if we want the specific single authentication (using ==) and
  888. * if we do, we initiate usage of it.
  889. *
  890. * If the provided authentication is wanted as one out of several accepted
  891. * types (using &), we OR this authentication type to the authavail
  892. * variable.
  893. *
  894. * Note:
  895. *
  896. * ->picked is first set to the 'want' value (one or more bits) before the
  897. * request is sent, and then it is again set _after_ all response 401/407
  898. * headers have been received but then only to a single preferred method
  899. * (bit).
  900. */
  901. while(*auth) {
  902. #ifdef USE_SPNEGO
  903. if(checkprefix("Negotiate", auth) && is_valid_auth_separator(auth[9])) {
  904. if((authp->avail & CURLAUTH_NEGOTIATE) ||
  905. Curl_auth_is_spnego_supported()) {
  906. *availp |= CURLAUTH_NEGOTIATE;
  907. authp->avail |= CURLAUTH_NEGOTIATE;
  908. if(authp->picked == CURLAUTH_NEGOTIATE) {
  909. CURLcode result = Curl_input_negotiate(data, conn, proxy, auth);
  910. if(!result) {
  911. free(data->req.newurl);
  912. data->req.newurl = strdup(data->state.url);
  913. if(!data->req.newurl)
  914. return CURLE_OUT_OF_MEMORY;
  915. data->state.authproblem = FALSE;
  916. /* we received a GSS auth token and we dealt with it fine */
  917. *negstate = GSS_AUTHRECV;
  918. }
  919. else
  920. data->state.authproblem = TRUE;
  921. }
  922. }
  923. }
  924. else
  925. #endif
  926. #ifdef USE_NTLM
  927. /* NTLM support requires the SSL crypto libs */
  928. if(checkprefix("NTLM", auth) && is_valid_auth_separator(auth[4])) {
  929. if((authp->avail & CURLAUTH_NTLM) ||
  930. (authp->avail & CURLAUTH_NTLM_WB) ||
  931. Curl_auth_is_ntlm_supported()) {
  932. *availp |= CURLAUTH_NTLM;
  933. authp->avail |= CURLAUTH_NTLM;
  934. if(authp->picked == CURLAUTH_NTLM ||
  935. authp->picked == CURLAUTH_NTLM_WB) {
  936. /* NTLM authentication is picked and activated */
  937. CURLcode result = Curl_input_ntlm(data, proxy, auth);
  938. if(!result) {
  939. data->state.authproblem = FALSE;
  940. #ifdef NTLM_WB_ENABLED
  941. if(authp->picked == CURLAUTH_NTLM_WB) {
  942. *availp &= ~CURLAUTH_NTLM;
  943. authp->avail &= ~CURLAUTH_NTLM;
  944. *availp |= CURLAUTH_NTLM_WB;
  945. authp->avail |= CURLAUTH_NTLM_WB;
  946. result = Curl_input_ntlm_wb(data, conn, proxy, auth);
  947. if(result) {
  948. infof(data, "Authentication problem. Ignoring this.");
  949. data->state.authproblem = TRUE;
  950. }
  951. }
  952. #endif
  953. }
  954. else {
  955. infof(data, "Authentication problem. Ignoring this.");
  956. data->state.authproblem = TRUE;
  957. }
  958. }
  959. }
  960. }
  961. else
  962. #endif
  963. #ifndef CURL_DISABLE_CRYPTO_AUTH
  964. if(checkprefix("Digest", auth) && is_valid_auth_separator(auth[6])) {
  965. if((authp->avail & CURLAUTH_DIGEST) != 0)
  966. infof(data, "Ignoring duplicate digest auth header.");
  967. else if(Curl_auth_is_digest_supported()) {
  968. CURLcode result;
  969. *availp |= CURLAUTH_DIGEST;
  970. authp->avail |= CURLAUTH_DIGEST;
  971. /* We call this function on input Digest headers even if Digest
  972. * authentication isn't activated yet, as we need to store the
  973. * incoming data from this header in case we are going to use
  974. * Digest */
  975. result = Curl_input_digest(data, proxy, auth);
  976. if(result) {
  977. infof(data, "Authentication problem. Ignoring this.");
  978. data->state.authproblem = TRUE;
  979. }
  980. }
  981. }
  982. else
  983. #endif
  984. if(checkprefix("Basic", auth) &&
  985. is_valid_auth_separator(auth[5])) {
  986. *availp |= CURLAUTH_BASIC;
  987. authp->avail |= CURLAUTH_BASIC;
  988. if(authp->picked == CURLAUTH_BASIC) {
  989. /* We asked for Basic authentication but got a 40X back
  990. anyway, which basically means our name+password isn't
  991. valid. */
  992. authp->avail = CURLAUTH_NONE;
  993. infof(data, "Authentication problem. Ignoring this.");
  994. data->state.authproblem = TRUE;
  995. }
  996. }
  997. else
  998. if(checkprefix("Bearer", auth) &&
  999. is_valid_auth_separator(auth[6])) {
  1000. *availp |= CURLAUTH_BEARER;
  1001. authp->avail |= CURLAUTH_BEARER;
  1002. if(authp->picked == CURLAUTH_BEARER) {
  1003. /* We asked for Bearer authentication but got a 40X back
  1004. anyway, which basically means our token isn't valid. */
  1005. authp->avail = CURLAUTH_NONE;
  1006. infof(data, "Authentication problem. Ignoring this.");
  1007. data->state.authproblem = TRUE;
  1008. }
  1009. }
  1010. /* there may be multiple methods on one line, so keep reading */
  1011. while(*auth && *auth != ',') /* read up to the next comma */
  1012. auth++;
  1013. if(*auth == ',') /* if we're on a comma, skip it */
  1014. auth++;
  1015. while(*auth && ISSPACE(*auth))
  1016. auth++;
  1017. }
  1018. return CURLE_OK;
  1019. }
  1020. /**
  1021. * http_should_fail() determines whether an HTTP response has gotten us
  1022. * into an error state or not.
  1023. *
  1024. * @param conn all information about the current connection
  1025. *
  1026. * @retval FALSE communications should continue
  1027. *
  1028. * @retval TRUE communications should not continue
  1029. */
  1030. static bool http_should_fail(struct Curl_easy *data)
  1031. {
  1032. int httpcode;
  1033. DEBUGASSERT(data);
  1034. DEBUGASSERT(data->conn);
  1035. httpcode = data->req.httpcode;
  1036. /*
  1037. ** If we haven't been asked to fail on error,
  1038. ** don't fail.
  1039. */
  1040. if(!data->set.http_fail_on_error)
  1041. return FALSE;
  1042. /*
  1043. ** Any code < 400 is never terminal.
  1044. */
  1045. if(httpcode < 400)
  1046. return FALSE;
  1047. /*
  1048. ** A 416 response to a resume request is presumably because the file is
  1049. ** already completely downloaded and thus not actually a fail.
  1050. */
  1051. if(data->state.resume_from && data->state.httpreq == HTTPREQ_GET &&
  1052. httpcode == 416)
  1053. return FALSE;
  1054. /*
  1055. ** Any code >= 400 that's not 401 or 407 is always
  1056. ** a terminal error
  1057. */
  1058. if((httpcode != 401) && (httpcode != 407))
  1059. return TRUE;
  1060. /*
  1061. ** All we have left to deal with is 401 and 407
  1062. */
  1063. DEBUGASSERT((httpcode == 401) || (httpcode == 407));
  1064. /*
  1065. ** Examine the current authentication state to see if this
  1066. ** is an error. The idea is for this function to get
  1067. ** called after processing all the headers in a response
  1068. ** message. So, if we've been to asked to authenticate a
  1069. ** particular stage, and we've done it, we're OK. But, if
  1070. ** we're already completely authenticated, it's not OK to
  1071. ** get another 401 or 407.
  1072. **
  1073. ** It is possible for authentication to go stale such that
  1074. ** the client needs to reauthenticate. Once that info is
  1075. ** available, use it here.
  1076. */
  1077. /*
  1078. ** Either we're not authenticating, or we're supposed to
  1079. ** be authenticating something else. This is an error.
  1080. */
  1081. if((httpcode == 401) && !data->state.aptr.user)
  1082. return TRUE;
  1083. #ifndef CURL_DISABLE_PROXY
  1084. if((httpcode == 407) && !data->conn->bits.proxy_user_passwd)
  1085. return TRUE;
  1086. #endif
  1087. return data->state.authproblem;
  1088. }
  1089. /*
  1090. * readmoredata() is a "fread() emulation" to provide POST and/or request
  1091. * data. It is used when a huge POST is to be made and the entire chunk wasn't
  1092. * sent in the first send(). This function will then be called from the
  1093. * transfer.c loop when more data is to be sent to the peer.
  1094. *
  1095. * Returns the amount of bytes it filled the buffer with.
  1096. */
  1097. static size_t readmoredata(char *buffer,
  1098. size_t size,
  1099. size_t nitems,
  1100. void *userp)
  1101. {
  1102. struct HTTP *http = (struct HTTP *)userp;
  1103. struct Curl_easy *data = http->backup.data;
  1104. size_t fullsize = size * nitems;
  1105. if(!http->postsize)
  1106. /* nothing to return */
  1107. return 0;
  1108. /* make sure that an HTTP request is never sent away chunked! */
  1109. data->req.forbidchunk = (http->sending == HTTPSEND_REQUEST)?TRUE:FALSE;
  1110. if(data->set.max_send_speed &&
  1111. (data->set.max_send_speed < (curl_off_t)fullsize) &&
  1112. (data->set.max_send_speed < http->postsize))
  1113. /* speed limit */
  1114. fullsize = (size_t)data->set.max_send_speed;
  1115. else if(http->postsize <= (curl_off_t)fullsize) {
  1116. memcpy(buffer, http->postdata, (size_t)http->postsize);
  1117. fullsize = (size_t)http->postsize;
  1118. if(http->backup.postsize) {
  1119. /* move backup data into focus and continue on that */
  1120. http->postdata = http->backup.postdata;
  1121. http->postsize = http->backup.postsize;
  1122. data->state.fread_func = http->backup.fread_func;
  1123. data->state.in = http->backup.fread_in;
  1124. http->sending++; /* move one step up */
  1125. http->backup.postsize = 0;
  1126. }
  1127. else
  1128. http->postsize = 0;
  1129. return fullsize;
  1130. }
  1131. memcpy(buffer, http->postdata, fullsize);
  1132. http->postdata += fullsize;
  1133. http->postsize -= fullsize;
  1134. return fullsize;
  1135. }
  1136. /*
  1137. * Curl_buffer_send() sends a header buffer and frees all associated
  1138. * memory. Body data may be appended to the header data if desired.
  1139. *
  1140. * Returns CURLcode
  1141. */
  1142. CURLcode Curl_buffer_send(struct dynbuf *in,
  1143. struct Curl_easy *data,
  1144. struct HTTP *http,
  1145. /* add the number of sent bytes to this
  1146. counter */
  1147. curl_off_t *bytes_written,
  1148. /* how much of the buffer contains body data */
  1149. curl_off_t included_body_bytes,
  1150. int socketindex)
  1151. {
  1152. ssize_t amount;
  1153. CURLcode result;
  1154. char *ptr;
  1155. size_t size;
  1156. struct connectdata *conn = data->conn;
  1157. size_t sendsize;
  1158. curl_socket_t sockfd;
  1159. size_t headersize;
  1160. DEBUGASSERT(socketindex <= SECONDARYSOCKET);
  1161. sockfd = Curl_conn_get_socket(data, socketindex);
  1162. /* The looping below is required since we use non-blocking sockets, but due
  1163. to the circumstances we will just loop and try again and again etc */
  1164. ptr = Curl_dyn_ptr(in);
  1165. size = Curl_dyn_len(in);
  1166. headersize = size - (size_t)included_body_bytes; /* the initial part that
  1167. isn't body is header */
  1168. DEBUGASSERT(size > (size_t)included_body_bytes);
  1169. if((conn->handler->flags & PROTOPT_SSL
  1170. #ifndef CURL_DISABLE_PROXY
  1171. || IS_HTTPS_PROXY(conn->http_proxy.proxytype)
  1172. #endif
  1173. )
  1174. && conn->httpversion < 20) {
  1175. /* Make sure this doesn't send more body bytes than what the max send
  1176. speed says. The request bytes do not count to the max speed.
  1177. */
  1178. if(data->set.max_send_speed &&
  1179. (included_body_bytes > data->set.max_send_speed)) {
  1180. curl_off_t overflow = included_body_bytes - data->set.max_send_speed;
  1181. DEBUGASSERT((size_t)overflow < size);
  1182. sendsize = size - (size_t)overflow;
  1183. }
  1184. else
  1185. sendsize = size;
  1186. /* OpenSSL is very picky and we must send the SAME buffer pointer to the
  1187. library when we attempt to re-send this buffer. Sending the same data
  1188. is not enough, we must use the exact same address. For this reason, we
  1189. must copy the data to the uploadbuffer first, since that is the buffer
  1190. we will be using if this send is retried later.
  1191. */
  1192. result = Curl_get_upload_buffer(data);
  1193. if(result) {
  1194. /* malloc failed, free memory and return to the caller */
  1195. Curl_dyn_free(in);
  1196. return result;
  1197. }
  1198. /* We never send more than upload_buffer_size bytes in one single chunk
  1199. when we speak HTTPS, as if only a fraction of it is sent now, this data
  1200. needs to fit into the normal read-callback buffer later on and that
  1201. buffer is using this size.
  1202. */
  1203. if(sendsize > (size_t)data->set.upload_buffer_size)
  1204. sendsize = (size_t)data->set.upload_buffer_size;
  1205. memcpy(data->state.ulbuf, ptr, sendsize);
  1206. ptr = data->state.ulbuf;
  1207. }
  1208. else {
  1209. #ifdef CURLDEBUG
  1210. /* Allow debug builds to override this logic to force short initial
  1211. sends
  1212. */
  1213. char *p = getenv("CURL_SMALLREQSEND");
  1214. if(p) {
  1215. size_t altsize = (size_t)strtoul(p, NULL, 10);
  1216. if(altsize)
  1217. sendsize = CURLMIN(size, altsize);
  1218. else
  1219. sendsize = size;
  1220. }
  1221. else
  1222. #endif
  1223. {
  1224. /* Make sure this doesn't send more body bytes than what the max send
  1225. speed says. The request bytes do not count to the max speed.
  1226. */
  1227. if(data->set.max_send_speed &&
  1228. (included_body_bytes > data->set.max_send_speed)) {
  1229. curl_off_t overflow = included_body_bytes - data->set.max_send_speed;
  1230. DEBUGASSERT((size_t)overflow < size);
  1231. sendsize = size - (size_t)overflow;
  1232. }
  1233. else
  1234. sendsize = size;
  1235. }
  1236. }
  1237. result = Curl_write(data, sockfd, ptr, sendsize, &amount);
  1238. if(!result) {
  1239. /*
  1240. * Note that we may not send the entire chunk at once, and we have a set
  1241. * number of data bytes at the end of the big buffer (out of which we may
  1242. * only send away a part).
  1243. */
  1244. /* how much of the header that was sent */
  1245. size_t headlen = (size_t)amount>headersize ? headersize : (size_t)amount;
  1246. size_t bodylen = amount - headlen;
  1247. /* this data _may_ contain binary stuff */
  1248. Curl_debug(data, CURLINFO_HEADER_OUT, ptr, headlen);
  1249. if(bodylen)
  1250. /* there was body data sent beyond the initial header part, pass that on
  1251. to the debug callback too */
  1252. Curl_debug(data, CURLINFO_DATA_OUT, ptr + headlen, bodylen);
  1253. /* 'amount' can never be a very large value here so typecasting it so a
  1254. signed 31 bit value should not cause problems even if ssize_t is
  1255. 64bit */
  1256. *bytes_written += (long)amount;
  1257. if(http) {
  1258. /* if we sent a piece of the body here, up the byte counter for it
  1259. accordingly */
  1260. data->req.writebytecount += bodylen;
  1261. Curl_pgrsSetUploadCounter(data, data->req.writebytecount);
  1262. if((size_t)amount != size) {
  1263. /* The whole request could not be sent in one system call. We must
  1264. queue it up and send it later when we get the chance. We must not
  1265. loop here and wait until it might work again. */
  1266. size -= amount;
  1267. ptr = Curl_dyn_ptr(in) + amount;
  1268. /* backup the currently set pointers */
  1269. http->backup.fread_func = data->state.fread_func;
  1270. http->backup.fread_in = data->state.in;
  1271. http->backup.postdata = http->postdata;
  1272. http->backup.postsize = http->postsize;
  1273. http->backup.data = data;
  1274. /* set the new pointers for the request-sending */
  1275. data->state.fread_func = (curl_read_callback)readmoredata;
  1276. data->state.in = (void *)http;
  1277. http->postdata = ptr;
  1278. http->postsize = (curl_off_t)size;
  1279. /* this much data is remaining header: */
  1280. data->req.pendingheader = headersize - headlen;
  1281. http->send_buffer = *in; /* copy the whole struct */
  1282. http->sending = HTTPSEND_REQUEST;
  1283. return CURLE_OK;
  1284. }
  1285. http->sending = HTTPSEND_BODY;
  1286. /* the full buffer was sent, clean up and return */
  1287. }
  1288. else {
  1289. if((size_t)amount != size)
  1290. /* We have no continue-send mechanism now, fail. This can only happen
  1291. when this function is used from the CONNECT sending function. We
  1292. currently (stupidly) assume that the whole request is always sent
  1293. away in the first single chunk.
  1294. This needs FIXing.
  1295. */
  1296. return CURLE_SEND_ERROR;
  1297. }
  1298. }
  1299. Curl_dyn_free(in);
  1300. /* no remaining header data */
  1301. data->req.pendingheader = 0;
  1302. return result;
  1303. }
  1304. /* end of the add_buffer functions */
  1305. /* ------------------------------------------------------------------------- */
  1306. /*
  1307. * Curl_compareheader()
  1308. *
  1309. * Returns TRUE if 'headerline' contains the 'header' with given 'content'.
  1310. * Pass headers WITH the colon.
  1311. */
  1312. bool
  1313. Curl_compareheader(const char *headerline, /* line to check */
  1314. const char *header, /* header keyword _with_ colon */
  1315. const size_t hlen, /* len of the keyword in bytes */
  1316. const char *content, /* content string to find */
  1317. const size_t clen) /* len of the content in bytes */
  1318. {
  1319. /* RFC2616, section 4.2 says: "Each header field consists of a name followed
  1320. * by a colon (":") and the field value. Field names are case-insensitive.
  1321. * The field value MAY be preceded by any amount of LWS, though a single SP
  1322. * is preferred." */
  1323. size_t len;
  1324. const char *start;
  1325. const char *end;
  1326. DEBUGASSERT(hlen);
  1327. DEBUGASSERT(clen);
  1328. DEBUGASSERT(header);
  1329. DEBUGASSERT(content);
  1330. if(!strncasecompare(headerline, header, hlen))
  1331. return FALSE; /* doesn't start with header */
  1332. /* pass the header */
  1333. start = &headerline[hlen];
  1334. /* pass all whitespace */
  1335. while(*start && ISSPACE(*start))
  1336. start++;
  1337. /* find the end of the header line */
  1338. end = strchr(start, '\r'); /* lines end with CRLF */
  1339. if(!end) {
  1340. /* in case there's a non-standard compliant line here */
  1341. end = strchr(start, '\n');
  1342. if(!end)
  1343. /* hm, there's no line ending here, use the zero byte! */
  1344. end = strchr(start, '\0');
  1345. }
  1346. len = end-start; /* length of the content part of the input line */
  1347. /* find the content string in the rest of the line */
  1348. for(; len >= clen; len--, start++) {
  1349. if(strncasecompare(start, content, clen))
  1350. return TRUE; /* match! */
  1351. }
  1352. return FALSE; /* no match */
  1353. }
  1354. /*
  1355. * Curl_http_connect() performs HTTP stuff to do at connect-time, called from
  1356. * the generic Curl_connect().
  1357. */
  1358. CURLcode Curl_http_connect(struct Curl_easy *data, bool *done)
  1359. {
  1360. struct connectdata *conn = data->conn;
  1361. /* We default to persistent connections. We set this already in this connect
  1362. function to make the re-use checks properly be able to check this bit. */
  1363. connkeep(conn, "HTTP default");
  1364. return Curl_conn_connect(data, FIRSTSOCKET, FALSE, done);
  1365. }
  1366. /* this returns the socket to wait for in the DO and DOING state for the multi
  1367. interface and then we're always _sending_ a request and thus we wait for
  1368. the single socket to become writable only */
  1369. static int http_getsock_do(struct Curl_easy *data,
  1370. struct connectdata *conn,
  1371. curl_socket_t *socks)
  1372. {
  1373. /* write mode */
  1374. (void)conn;
  1375. socks[0] = Curl_conn_get_socket(data, FIRSTSOCKET);
  1376. return GETSOCK_WRITESOCK(0);
  1377. }
  1378. /*
  1379. * Curl_http_done() gets called after a single HTTP request has been
  1380. * performed.
  1381. */
  1382. CURLcode Curl_http_done(struct Curl_easy *data,
  1383. CURLcode status, bool premature)
  1384. {
  1385. struct connectdata *conn = data->conn;
  1386. struct HTTP *http = data->req.p.http;
  1387. /* Clear multipass flag. If authentication isn't done yet, then it will get
  1388. * a chance to be set back to true when we output the next auth header */
  1389. data->state.authhost.multipass = FALSE;
  1390. data->state.authproxy.multipass = FALSE;
  1391. Curl_unencode_cleanup(data);
  1392. /* set the proper values (possibly modified on POST) */
  1393. conn->seek_func = data->set.seek_func; /* restore */
  1394. conn->seek_client = data->set.seek_client; /* restore */
  1395. if(!http)
  1396. return CURLE_OK;
  1397. Curl_dyn_free(&http->send_buffer);
  1398. Curl_mime_cleanpart(&http->form);
  1399. Curl_dyn_reset(&data->state.headerb);
  1400. Curl_hyper_done(data);
  1401. Curl_ws_done(data);
  1402. if(status)
  1403. return status;
  1404. if(!premature && /* this check is pointless when DONE is called before the
  1405. entire operation is complete */
  1406. !conn->bits.retry &&
  1407. !data->set.connect_only &&
  1408. (data->req.bytecount +
  1409. data->req.headerbytecount -
  1410. data->req.deductheadercount) <= 0) {
  1411. /* If this connection isn't simply closed to be retried, AND nothing was
  1412. read from the HTTP server (that counts), this can't be right so we
  1413. return an error here */
  1414. failf(data, "Empty reply from server");
  1415. /* Mark it as closed to avoid the "left intact" message */
  1416. streamclose(conn, "Empty reply from server");
  1417. return CURLE_GOT_NOTHING;
  1418. }
  1419. return CURLE_OK;
  1420. }
  1421. /*
  1422. * Determine if we should use HTTP 1.1 (OR BETTER) for this request. Reasons
  1423. * to avoid it include:
  1424. *
  1425. * - if the user specifically requested HTTP 1.0
  1426. * - if the server we are connected to only supports 1.0
  1427. * - if any server previously contacted to handle this request only supports
  1428. * 1.0.
  1429. */
  1430. bool Curl_use_http_1_1plus(const struct Curl_easy *data,
  1431. const struct connectdata *conn)
  1432. {
  1433. if((data->state.httpversion == 10) || (conn->httpversion == 10))
  1434. return FALSE;
  1435. if((data->state.httpwant == CURL_HTTP_VERSION_1_0) &&
  1436. (conn->httpversion <= 10))
  1437. return FALSE;
  1438. return ((data->state.httpwant == CURL_HTTP_VERSION_NONE) ||
  1439. (data->state.httpwant >= CURL_HTTP_VERSION_1_1));
  1440. }
  1441. #ifndef USE_HYPER
  1442. static const char *get_http_string(const struct Curl_easy *data,
  1443. const struct connectdata *conn)
  1444. {
  1445. if(Curl_conn_is_http3(data, conn, FIRSTSOCKET))
  1446. return "3";
  1447. if(Curl_conn_is_http2(data, conn, FIRSTSOCKET))
  1448. return "2";
  1449. if(Curl_use_http_1_1plus(data, conn))
  1450. return "1.1";
  1451. return "1.0";
  1452. }
  1453. #endif
  1454. /* check and possibly add an Expect: header */
  1455. static CURLcode expect100(struct Curl_easy *data,
  1456. struct connectdata *conn,
  1457. struct dynbuf *req)
  1458. {
  1459. CURLcode result = CURLE_OK;
  1460. data->state.expect100header = FALSE; /* default to false unless it is set
  1461. to TRUE below */
  1462. if(!data->state.disableexpect && Curl_use_http_1_1plus(data, conn) &&
  1463. (conn->httpversion < 20)) {
  1464. /* if not doing HTTP 1.0 or version 2, or disabled explicitly, we add an
  1465. Expect: 100-continue to the headers which actually speeds up post
  1466. operations (as there is one packet coming back from the web server) */
  1467. const char *ptr = Curl_checkheaders(data, STRCONST("Expect"));
  1468. if(ptr) {
  1469. data->state.expect100header =
  1470. Curl_compareheader(ptr, STRCONST("Expect:"), STRCONST("100-continue"));
  1471. }
  1472. else {
  1473. result = Curl_dyn_addn(req, STRCONST("Expect: 100-continue\r\n"));
  1474. if(!result)
  1475. data->state.expect100header = TRUE;
  1476. }
  1477. }
  1478. return result;
  1479. }
  1480. enum proxy_use {
  1481. HEADER_SERVER, /* direct to server */
  1482. HEADER_PROXY, /* regular request to proxy */
  1483. HEADER_CONNECT /* sending CONNECT to a proxy */
  1484. };
  1485. /* used to compile the provided trailers into one buffer
  1486. will return an error code if one of the headers is
  1487. not formatted correctly */
  1488. CURLcode Curl_http_compile_trailers(struct curl_slist *trailers,
  1489. struct dynbuf *b,
  1490. struct Curl_easy *handle)
  1491. {
  1492. char *ptr = NULL;
  1493. CURLcode result = CURLE_OK;
  1494. const char *endofline_native = NULL;
  1495. const char *endofline_network = NULL;
  1496. if(
  1497. #ifdef CURL_DO_LINEEND_CONV
  1498. (handle->state.prefer_ascii) ||
  1499. #endif
  1500. (handle->set.crlf)) {
  1501. /* \n will become \r\n later on */
  1502. endofline_native = "\n";
  1503. endofline_network = "\x0a";
  1504. }
  1505. else {
  1506. endofline_native = "\r\n";
  1507. endofline_network = "\x0d\x0a";
  1508. }
  1509. while(trailers) {
  1510. /* only add correctly formatted trailers */
  1511. ptr = strchr(trailers->data, ':');
  1512. if(ptr && *(ptr + 1) == ' ') {
  1513. result = Curl_dyn_add(b, trailers->data);
  1514. if(result)
  1515. return result;
  1516. result = Curl_dyn_add(b, endofline_native);
  1517. if(result)
  1518. return result;
  1519. }
  1520. else
  1521. infof(handle, "Malformatted trailing header, skipping trailer");
  1522. trailers = trailers->next;
  1523. }
  1524. result = Curl_dyn_add(b, endofline_network);
  1525. return result;
  1526. }
  1527. static bool hd_name_eq(const char *n1, size_t n1len,
  1528. const char *n2, size_t n2len)
  1529. {
  1530. if(n1len == n2len) {
  1531. return strncasecompare(n1, n2, n1len);
  1532. }
  1533. return FALSE;
  1534. }
  1535. CURLcode Curl_dynhds_add_custom(struct Curl_easy *data,
  1536. bool is_connect,
  1537. struct dynhds *hds)
  1538. {
  1539. struct connectdata *conn = data->conn;
  1540. char *ptr;
  1541. struct curl_slist *h[2];
  1542. struct curl_slist *headers;
  1543. int numlists = 1; /* by default */
  1544. int i;
  1545. #ifndef CURL_DISABLE_PROXY
  1546. enum proxy_use proxy;
  1547. if(is_connect)
  1548. proxy = HEADER_CONNECT;
  1549. else
  1550. proxy = conn->bits.httpproxy && !conn->bits.tunnel_proxy?
  1551. HEADER_PROXY:HEADER_SERVER;
  1552. switch(proxy) {
  1553. case HEADER_SERVER:
  1554. h[0] = data->set.headers;
  1555. break;
  1556. case HEADER_PROXY:
  1557. h[0] = data->set.headers;
  1558. if(data->set.sep_headers) {
  1559. h[1] = data->set.proxyheaders;
  1560. numlists++;
  1561. }
  1562. break;
  1563. case HEADER_CONNECT:
  1564. if(data->set.sep_headers)
  1565. h[0] = data->set.proxyheaders;
  1566. else
  1567. h[0] = data->set.headers;
  1568. break;
  1569. }
  1570. #else
  1571. (void)is_connect;
  1572. h[0] = data->set.headers;
  1573. #endif
  1574. /* loop through one or two lists */
  1575. for(i = 0; i < numlists; i++) {
  1576. for(headers = h[i]; headers; headers = headers->next) {
  1577. const char *name, *value;
  1578. size_t namelen, valuelen;
  1579. /* There are 2 quirks in place for custom headers:
  1580. * 1. setting only 'name:' to suppress a header from being sent
  1581. * 2. setting only 'name;' to send an empty (illegal) header
  1582. */
  1583. ptr = strchr(headers->data, ':');
  1584. if(ptr) {
  1585. name = headers->data;
  1586. namelen = ptr - headers->data;
  1587. ptr++; /* pass the colon */
  1588. while(*ptr && ISSPACE(*ptr))
  1589. ptr++;
  1590. if(*ptr) {
  1591. value = ptr;
  1592. valuelen = strlen(value);
  1593. }
  1594. else {
  1595. /* quirk #1, suppress this header */
  1596. continue;
  1597. }
  1598. }
  1599. else {
  1600. ptr = strchr(headers->data, ';');
  1601. if(!ptr) {
  1602. /* neither : nor ; in provided header value. We seem
  1603. * to ignore this silently */
  1604. continue;
  1605. }
  1606. name = headers->data;
  1607. namelen = ptr - headers->data;
  1608. ptr++; /* pass the semicolon */
  1609. while(*ptr && ISSPACE(*ptr))
  1610. ptr++;
  1611. if(!*ptr) {
  1612. /* quirk #2, send an empty header */
  1613. value = "";
  1614. valuelen = 0;
  1615. }
  1616. else {
  1617. /* this may be used for something else in the future,
  1618. * ignore this for now */
  1619. continue;
  1620. }
  1621. }
  1622. DEBUGASSERT(name && value);
  1623. if(data->state.aptr.host &&
  1624. /* a Host: header was sent already, don't pass on any custom Host:
  1625. header as that will produce *two* in the same request! */
  1626. hd_name_eq(name, namelen, STRCONST("Host:")))
  1627. ;
  1628. else if(data->state.httpreq == HTTPREQ_POST_FORM &&
  1629. /* this header (extended by formdata.c) is sent later */
  1630. hd_name_eq(name, namelen, STRCONST("Content-Type:")))
  1631. ;
  1632. else if(data->state.httpreq == HTTPREQ_POST_MIME &&
  1633. /* this header is sent later */
  1634. hd_name_eq(name, namelen, STRCONST("Content-Type:")))
  1635. ;
  1636. else if(conn->bits.authneg &&
  1637. /* while doing auth neg, don't allow the custom length since
  1638. we will force length zero then */
  1639. hd_name_eq(name, namelen, STRCONST("Content-Length:")))
  1640. ;
  1641. else if(data->state.aptr.te &&
  1642. /* when asking for Transfer-Encoding, don't pass on a custom
  1643. Connection: */
  1644. hd_name_eq(name, namelen, STRCONST("Connection:")))
  1645. ;
  1646. else if((conn->httpversion >= 20) &&
  1647. hd_name_eq(name, namelen, STRCONST("Transfer-Encoding:")))
  1648. /* HTTP/2 doesn't support chunked requests */
  1649. ;
  1650. else if((hd_name_eq(name, namelen, STRCONST("Authorization:")) ||
  1651. hd_name_eq(name, namelen, STRCONST("Cookie:"))) &&
  1652. /* be careful of sending this potentially sensitive header to
  1653. other hosts */
  1654. !Curl_auth_allowed_to_host(data))
  1655. ;
  1656. else {
  1657. CURLcode result;
  1658. result = Curl_dynhds_add(hds, name, namelen, value, valuelen);
  1659. if(result)
  1660. return result;
  1661. }
  1662. }
  1663. }
  1664. return CURLE_OK;
  1665. }
  1666. CURLcode Curl_add_custom_headers(struct Curl_easy *data,
  1667. bool is_connect,
  1668. #ifndef USE_HYPER
  1669. struct dynbuf *req
  1670. #else
  1671. void *req
  1672. #endif
  1673. )
  1674. {
  1675. struct connectdata *conn = data->conn;
  1676. char *ptr;
  1677. struct curl_slist *h[2];
  1678. struct curl_slist *headers;
  1679. int numlists = 1; /* by default */
  1680. int i;
  1681. #ifndef CURL_DISABLE_PROXY
  1682. enum proxy_use proxy;
  1683. if(is_connect)
  1684. proxy = HEADER_CONNECT;
  1685. else
  1686. proxy = conn->bits.httpproxy && !conn->bits.tunnel_proxy?
  1687. HEADER_PROXY:HEADER_SERVER;
  1688. switch(proxy) {
  1689. case HEADER_SERVER:
  1690. h[0] = data->set.headers;
  1691. break;
  1692. case HEADER_PROXY:
  1693. h[0] = data->set.headers;
  1694. if(data->set.sep_headers) {
  1695. h[1] = data->set.proxyheaders;
  1696. numlists++;
  1697. }
  1698. break;
  1699. case HEADER_CONNECT:
  1700. if(data->set.sep_headers)
  1701. h[0] = data->set.proxyheaders;
  1702. else
  1703. h[0] = data->set.headers;
  1704. break;
  1705. }
  1706. #else
  1707. (void)is_connect;
  1708. h[0] = data->set.headers;
  1709. #endif
  1710. /* loop through one or two lists */
  1711. for(i = 0; i < numlists; i++) {
  1712. headers = h[i];
  1713. while(headers) {
  1714. char *semicolonp = NULL;
  1715. ptr = strchr(headers->data, ':');
  1716. if(!ptr) {
  1717. char *optr;
  1718. /* no colon, semicolon? */
  1719. ptr = strchr(headers->data, ';');
  1720. if(ptr) {
  1721. optr = ptr;
  1722. ptr++; /* pass the semicolon */
  1723. while(*ptr && ISSPACE(*ptr))
  1724. ptr++;
  1725. if(*ptr) {
  1726. /* this may be used for something else in the future */
  1727. optr = NULL;
  1728. }
  1729. else {
  1730. if(*(--ptr) == ';') {
  1731. /* copy the source */
  1732. semicolonp = strdup(headers->data);
  1733. if(!semicolonp) {
  1734. #ifndef USE_HYPER
  1735. Curl_dyn_free(req);
  1736. #endif
  1737. return CURLE_OUT_OF_MEMORY;
  1738. }
  1739. /* put a colon where the semicolon is */
  1740. semicolonp[ptr - headers->data] = ':';
  1741. /* point at the colon */
  1742. optr = &semicolonp [ptr - headers->data];
  1743. }
  1744. }
  1745. ptr = optr;
  1746. }
  1747. }
  1748. if(ptr && (ptr != headers->data)) {
  1749. /* we require a colon for this to be a true header */
  1750. ptr++; /* pass the colon */
  1751. while(*ptr && ISSPACE(*ptr))
  1752. ptr++;
  1753. if(*ptr || semicolonp) {
  1754. /* only send this if the contents was non-blank or done special */
  1755. CURLcode result = CURLE_OK;
  1756. char *compare = semicolonp ? semicolonp : headers->data;
  1757. if(data->state.aptr.host &&
  1758. /* a Host: header was sent already, don't pass on any custom Host:
  1759. header as that will produce *two* in the same request! */
  1760. checkprefix("Host:", compare))
  1761. ;
  1762. else if(data->state.httpreq == HTTPREQ_POST_FORM &&
  1763. /* this header (extended by formdata.c) is sent later */
  1764. checkprefix("Content-Type:", compare))
  1765. ;
  1766. else if(data->state.httpreq == HTTPREQ_POST_MIME &&
  1767. /* this header is sent later */
  1768. checkprefix("Content-Type:", compare))
  1769. ;
  1770. else if(conn->bits.authneg &&
  1771. /* while doing auth neg, don't allow the custom length since
  1772. we will force length zero then */
  1773. checkprefix("Content-Length:", compare))
  1774. ;
  1775. else if(data->state.aptr.te &&
  1776. /* when asking for Transfer-Encoding, don't pass on a custom
  1777. Connection: */
  1778. checkprefix("Connection:", compare))
  1779. ;
  1780. else if((conn->httpversion >= 20) &&
  1781. checkprefix("Transfer-Encoding:", compare))
  1782. /* HTTP/2 doesn't support chunked requests */
  1783. ;
  1784. else if((checkprefix("Authorization:", compare) ||
  1785. checkprefix("Cookie:", compare)) &&
  1786. /* be careful of sending this potentially sensitive header to
  1787. other hosts */
  1788. !Curl_auth_allowed_to_host(data))
  1789. ;
  1790. else {
  1791. #ifdef USE_HYPER
  1792. result = Curl_hyper_header(data, req, compare);
  1793. #else
  1794. result = Curl_dyn_addf(req, "%s\r\n", compare);
  1795. #endif
  1796. }
  1797. if(semicolonp)
  1798. free(semicolonp);
  1799. if(result)
  1800. return result;
  1801. }
  1802. }
  1803. headers = headers->next;
  1804. }
  1805. }
  1806. return CURLE_OK;
  1807. }
  1808. #ifndef CURL_DISABLE_PARSEDATE
  1809. CURLcode Curl_add_timecondition(struct Curl_easy *data,
  1810. #ifndef USE_HYPER
  1811. struct dynbuf *req
  1812. #else
  1813. void *req
  1814. #endif
  1815. )
  1816. {
  1817. const struct tm *tm;
  1818. struct tm keeptime;
  1819. CURLcode result;
  1820. char datestr[80];
  1821. const char *condp;
  1822. size_t len;
  1823. if(data->set.timecondition == CURL_TIMECOND_NONE)
  1824. /* no condition was asked for */
  1825. return CURLE_OK;
  1826. result = Curl_gmtime(data->set.timevalue, &keeptime);
  1827. if(result) {
  1828. failf(data, "Invalid TIMEVALUE");
  1829. return result;
  1830. }
  1831. tm = &keeptime;
  1832. switch(data->set.timecondition) {
  1833. default:
  1834. return CURLE_BAD_FUNCTION_ARGUMENT;
  1835. case CURL_TIMECOND_IFMODSINCE:
  1836. condp = "If-Modified-Since";
  1837. len = 17;
  1838. break;
  1839. case CURL_TIMECOND_IFUNMODSINCE:
  1840. condp = "If-Unmodified-Since";
  1841. len = 19;
  1842. break;
  1843. case CURL_TIMECOND_LASTMOD:
  1844. condp = "Last-Modified";
  1845. len = 13;
  1846. break;
  1847. }
  1848. if(Curl_checkheaders(data, condp, len)) {
  1849. /* A custom header was specified; it will be sent instead. */
  1850. return CURLE_OK;
  1851. }
  1852. /* The If-Modified-Since header family should have their times set in
  1853. * GMT as RFC2616 defines: "All HTTP date/time stamps MUST be
  1854. * represented in Greenwich Mean Time (GMT), without exception. For the
  1855. * purposes of HTTP, GMT is exactly equal to UTC (Coordinated Universal
  1856. * Time)." (see page 20 of RFC2616).
  1857. */
  1858. /* format: "Tue, 15 Nov 1994 12:45:26 GMT" */
  1859. msnprintf(datestr, sizeof(datestr),
  1860. "%s: %s, %02d %s %4d %02d:%02d:%02d GMT\r\n",
  1861. condp,
  1862. Curl_wkday[tm->tm_wday?tm->tm_wday-1:6],
  1863. tm->tm_mday,
  1864. Curl_month[tm->tm_mon],
  1865. tm->tm_year + 1900,
  1866. tm->tm_hour,
  1867. tm->tm_min,
  1868. tm->tm_sec);
  1869. #ifndef USE_HYPER
  1870. result = Curl_dyn_add(req, datestr);
  1871. #else
  1872. result = Curl_hyper_header(data, req, datestr);
  1873. #endif
  1874. return result;
  1875. }
  1876. #else
  1877. /* disabled */
  1878. CURLcode Curl_add_timecondition(struct Curl_easy *data,
  1879. struct dynbuf *req)
  1880. {
  1881. (void)data;
  1882. (void)req;
  1883. return CURLE_OK;
  1884. }
  1885. #endif
  1886. void Curl_http_method(struct Curl_easy *data, struct connectdata *conn,
  1887. const char **method, Curl_HttpReq *reqp)
  1888. {
  1889. Curl_HttpReq httpreq = (Curl_HttpReq)data->state.httpreq;
  1890. const char *request;
  1891. if((conn->handler->protocol&(PROTO_FAMILY_HTTP|CURLPROTO_FTP)) &&
  1892. data->state.upload)
  1893. httpreq = HTTPREQ_PUT;
  1894. /* Now set the 'request' pointer to the proper request string */
  1895. if(data->set.str[STRING_CUSTOMREQUEST])
  1896. request = data->set.str[STRING_CUSTOMREQUEST];
  1897. else {
  1898. if(data->req.no_body)
  1899. request = "HEAD";
  1900. else {
  1901. DEBUGASSERT((httpreq >= HTTPREQ_GET) && (httpreq <= HTTPREQ_HEAD));
  1902. switch(httpreq) {
  1903. case HTTPREQ_POST:
  1904. case HTTPREQ_POST_FORM:
  1905. case HTTPREQ_POST_MIME:
  1906. request = "POST";
  1907. break;
  1908. case HTTPREQ_PUT:
  1909. request = "PUT";
  1910. break;
  1911. default: /* this should never happen */
  1912. case HTTPREQ_GET:
  1913. request = "GET";
  1914. break;
  1915. case HTTPREQ_HEAD:
  1916. request = "HEAD";
  1917. break;
  1918. }
  1919. }
  1920. }
  1921. *method = request;
  1922. *reqp = httpreq;
  1923. }
  1924. CURLcode Curl_http_useragent(struct Curl_easy *data)
  1925. {
  1926. /* The User-Agent string might have been allocated in url.c already, because
  1927. it might have been used in the proxy connect, but if we have got a header
  1928. with the user-agent string specified, we erase the previously made string
  1929. here. */
  1930. if(Curl_checkheaders(data, STRCONST("User-Agent"))) {
  1931. free(data->state.aptr.uagent);
  1932. data->state.aptr.uagent = NULL;
  1933. }
  1934. return CURLE_OK;
  1935. }
  1936. CURLcode Curl_http_host(struct Curl_easy *data, struct connectdata *conn)
  1937. {
  1938. const char *ptr;
  1939. struct dynamically_allocated_data *aptr = &data->state.aptr;
  1940. if(!data->state.this_is_a_follow) {
  1941. /* Free to avoid leaking memory on multiple requests */
  1942. free(data->state.first_host);
  1943. data->state.first_host = strdup(conn->host.name);
  1944. if(!data->state.first_host)
  1945. return CURLE_OUT_OF_MEMORY;
  1946. data->state.first_remote_port = conn->remote_port;
  1947. data->state.first_remote_protocol = conn->handler->protocol;
  1948. }
  1949. Curl_safefree(aptr->host);
  1950. ptr = Curl_checkheaders(data, STRCONST("Host"));
  1951. if(ptr && (!data->state.this_is_a_follow ||
  1952. strcasecompare(data->state.first_host, conn->host.name))) {
  1953. #if !defined(CURL_DISABLE_COOKIES)
  1954. /* If we have a given custom Host: header, we extract the host name in
  1955. order to possibly use it for cookie reasons later on. We only allow the
  1956. custom Host: header if this is NOT a redirect, as setting Host: in the
  1957. redirected request is being out on thin ice. Except if the host name
  1958. is the same as the first one! */
  1959. char *cookiehost = Curl_copy_header_value(ptr);
  1960. if(!cookiehost)
  1961. return CURLE_OUT_OF_MEMORY;
  1962. if(!*cookiehost)
  1963. /* ignore empty data */
  1964. free(cookiehost);
  1965. else {
  1966. /* If the host begins with '[', we start searching for the port after
  1967. the bracket has been closed */
  1968. if(*cookiehost == '[') {
  1969. char *closingbracket;
  1970. /* since the 'cookiehost' is an allocated memory area that will be
  1971. freed later we cannot simply increment the pointer */
  1972. memmove(cookiehost, cookiehost + 1, strlen(cookiehost) - 1);
  1973. closingbracket = strchr(cookiehost, ']');
  1974. if(closingbracket)
  1975. *closingbracket = 0;
  1976. }
  1977. else {
  1978. int startsearch = 0;
  1979. char *colon = strchr(cookiehost + startsearch, ':');
  1980. if(colon)
  1981. *colon = 0; /* The host must not include an embedded port number */
  1982. }
  1983. Curl_safefree(aptr->cookiehost);
  1984. aptr->cookiehost = cookiehost;
  1985. }
  1986. #endif
  1987. if(strcmp("Host:", ptr)) {
  1988. aptr->host = aprintf("Host:%s\r\n", &ptr[5]);
  1989. if(!aptr->host)
  1990. return CURLE_OUT_OF_MEMORY;
  1991. }
  1992. }
  1993. else {
  1994. /* When building Host: headers, we must put the host name within
  1995. [brackets] if the host name is a plain IPv6-address. RFC2732-style. */
  1996. const char *host = conn->host.name;
  1997. if(((conn->given->protocol&(CURLPROTO_HTTPS|CURLPROTO_WSS)) &&
  1998. (conn->remote_port == PORT_HTTPS)) ||
  1999. ((conn->given->protocol&(CURLPROTO_HTTP|CURLPROTO_WS)) &&
  2000. (conn->remote_port == PORT_HTTP)) )
  2001. /* if(HTTPS on port 443) OR (HTTP on port 80) then don't include
  2002. the port number in the host string */
  2003. aptr->host = aprintf("Host: %s%s%s\r\n", conn->bits.ipv6_ip?"[":"",
  2004. host, conn->bits.ipv6_ip?"]":"");
  2005. else
  2006. aptr->host = aprintf("Host: %s%s%s:%d\r\n", conn->bits.ipv6_ip?"[":"",
  2007. host, conn->bits.ipv6_ip?"]":"",
  2008. conn->remote_port);
  2009. if(!aptr->host)
  2010. /* without Host: we can't make a nice request */
  2011. return CURLE_OUT_OF_MEMORY;
  2012. }
  2013. return CURLE_OK;
  2014. }
  2015. /*
  2016. * Append the request-target to the HTTP request
  2017. */
  2018. CURLcode Curl_http_target(struct Curl_easy *data,
  2019. struct connectdata *conn,
  2020. struct dynbuf *r)
  2021. {
  2022. CURLcode result = CURLE_OK;
  2023. const char *path = data->state.up.path;
  2024. const char *query = data->state.up.query;
  2025. if(data->set.str[STRING_TARGET]) {
  2026. path = data->set.str[STRING_TARGET];
  2027. query = NULL;
  2028. }
  2029. #ifndef CURL_DISABLE_PROXY
  2030. if(conn->bits.httpproxy && !conn->bits.tunnel_proxy) {
  2031. /* Using a proxy but does not tunnel through it */
  2032. /* The path sent to the proxy is in fact the entire URL. But if the remote
  2033. host is a IDN-name, we must make sure that the request we produce only
  2034. uses the encoded host name! */
  2035. /* and no fragment part */
  2036. CURLUcode uc;
  2037. char *url;
  2038. CURLU *h = curl_url_dup(data->state.uh);
  2039. if(!h)
  2040. return CURLE_OUT_OF_MEMORY;
  2041. if(conn->host.dispname != conn->host.name) {
  2042. uc = curl_url_set(h, CURLUPART_HOST, conn->host.name, 0);
  2043. if(uc) {
  2044. curl_url_cleanup(h);
  2045. return CURLE_OUT_OF_MEMORY;
  2046. }
  2047. }
  2048. uc = curl_url_set(h, CURLUPART_FRAGMENT, NULL, 0);
  2049. if(uc) {
  2050. curl_url_cleanup(h);
  2051. return CURLE_OUT_OF_MEMORY;
  2052. }
  2053. if(strcasecompare("http", data->state.up.scheme)) {
  2054. /* when getting HTTP, we don't want the userinfo the URL */
  2055. uc = curl_url_set(h, CURLUPART_USER, NULL, 0);
  2056. if(uc) {
  2057. curl_url_cleanup(h);
  2058. return CURLE_OUT_OF_MEMORY;
  2059. }
  2060. uc = curl_url_set(h, CURLUPART_PASSWORD, NULL, 0);
  2061. if(uc) {
  2062. curl_url_cleanup(h);
  2063. return CURLE_OUT_OF_MEMORY;
  2064. }
  2065. }
  2066. /* Extract the URL to use in the request. Store in STRING_TEMP_URL for
  2067. clean-up reasons if the function returns before the free() further
  2068. down. */
  2069. uc = curl_url_get(h, CURLUPART_URL, &url, CURLU_NO_DEFAULT_PORT);
  2070. if(uc) {
  2071. curl_url_cleanup(h);
  2072. return CURLE_OUT_OF_MEMORY;
  2073. }
  2074. curl_url_cleanup(h);
  2075. /* target or url */
  2076. result = Curl_dyn_add(r, data->set.str[STRING_TARGET]?
  2077. data->set.str[STRING_TARGET]:url);
  2078. free(url);
  2079. if(result)
  2080. return (result);
  2081. if(strcasecompare("ftp", data->state.up.scheme)) {
  2082. if(data->set.proxy_transfer_mode) {
  2083. /* when doing ftp, append ;type=<a|i> if not present */
  2084. char *type = strstr(path, ";type=");
  2085. if(type && type[6] && type[7] == 0) {
  2086. switch(Curl_raw_toupper(type[6])) {
  2087. case 'A':
  2088. case 'D':
  2089. case 'I':
  2090. break;
  2091. default:
  2092. type = NULL;
  2093. }
  2094. }
  2095. if(!type) {
  2096. result = Curl_dyn_addf(r, ";type=%c",
  2097. data->state.prefer_ascii ? 'a' : 'i');
  2098. if(result)
  2099. return result;
  2100. }
  2101. }
  2102. }
  2103. }
  2104. else
  2105. #else
  2106. (void)conn; /* not used in disabled-proxy builds */
  2107. #endif
  2108. {
  2109. result = Curl_dyn_add(r, path);
  2110. if(result)
  2111. return result;
  2112. if(query)
  2113. result = Curl_dyn_addf(r, "?%s", query);
  2114. }
  2115. return result;
  2116. }
  2117. CURLcode Curl_http_body(struct Curl_easy *data, struct connectdata *conn,
  2118. Curl_HttpReq httpreq, const char **tep)
  2119. {
  2120. CURLcode result = CURLE_OK;
  2121. const char *ptr;
  2122. struct HTTP *http = data->req.p.http;
  2123. http->postsize = 0;
  2124. switch(httpreq) {
  2125. case HTTPREQ_POST_MIME:
  2126. http->sendit = &data->set.mimepost;
  2127. break;
  2128. #ifndef CURL_DISABLE_FORM_API
  2129. case HTTPREQ_POST_FORM:
  2130. /* Convert the form structure into a mime structure. */
  2131. Curl_mime_cleanpart(&http->form);
  2132. result = Curl_getformdata(data, &http->form, data->set.httppost,
  2133. data->state.fread_func);
  2134. if(result)
  2135. return result;
  2136. http->sendit = &http->form;
  2137. break;
  2138. #endif
  2139. default:
  2140. http->sendit = NULL;
  2141. }
  2142. #ifndef CURL_DISABLE_MIME
  2143. if(http->sendit) {
  2144. const char *cthdr = Curl_checkheaders(data, STRCONST("Content-Type"));
  2145. /* Read and seek body only. */
  2146. http->sendit->flags |= MIME_BODY_ONLY;
  2147. /* Prepare the mime structure headers & set content type. */
  2148. if(cthdr)
  2149. for(cthdr += 13; *cthdr == ' '; cthdr++)
  2150. ;
  2151. else if(http->sendit->kind == MIMEKIND_MULTIPART)
  2152. cthdr = "multipart/form-data";
  2153. curl_mime_headers(http->sendit, data->set.headers, 0);
  2154. result = Curl_mime_prepare_headers(data, http->sendit, cthdr,
  2155. NULL, MIMESTRATEGY_FORM);
  2156. curl_mime_headers(http->sendit, NULL, 0);
  2157. if(!result)
  2158. result = Curl_mime_rewind(http->sendit);
  2159. if(result)
  2160. return result;
  2161. http->postsize = Curl_mime_size(http->sendit);
  2162. }
  2163. #endif
  2164. ptr = Curl_checkheaders(data, STRCONST("Transfer-Encoding"));
  2165. if(ptr) {
  2166. /* Some kind of TE is requested, check if 'chunked' is chosen */
  2167. data->req.upload_chunky =
  2168. Curl_compareheader(ptr,
  2169. STRCONST("Transfer-Encoding:"), STRCONST("chunked"));
  2170. }
  2171. else {
  2172. if((conn->handler->protocol & PROTO_FAMILY_HTTP) &&
  2173. (((httpreq == HTTPREQ_POST_MIME || httpreq == HTTPREQ_POST_FORM) &&
  2174. http->postsize < 0) ||
  2175. ((data->state.upload || httpreq == HTTPREQ_POST) &&
  2176. data->state.infilesize == -1))) {
  2177. if(conn->bits.authneg)
  2178. /* don't enable chunked during auth neg */
  2179. ;
  2180. else if(Curl_use_http_1_1plus(data, conn)) {
  2181. if(conn->httpversion < 20)
  2182. /* HTTP, upload, unknown file size and not HTTP 1.0 */
  2183. data->req.upload_chunky = TRUE;
  2184. }
  2185. else {
  2186. failf(data, "Chunky upload is not supported by HTTP 1.0");
  2187. return CURLE_UPLOAD_FAILED;
  2188. }
  2189. }
  2190. else {
  2191. /* else, no chunky upload */
  2192. data->req.upload_chunky = FALSE;
  2193. }
  2194. if(data->req.upload_chunky)
  2195. *tep = "Transfer-Encoding: chunked\r\n";
  2196. }
  2197. return result;
  2198. }
  2199. CURLcode Curl_http_bodysend(struct Curl_easy *data, struct connectdata *conn,
  2200. struct dynbuf *r, Curl_HttpReq httpreq)
  2201. {
  2202. #ifndef USE_HYPER
  2203. /* Hyper always handles the body separately */
  2204. curl_off_t included_body = 0;
  2205. #else
  2206. /* from this point down, this function should not be used */
  2207. #define Curl_buffer_send(a,b,c,d,e,f) CURLE_OK
  2208. #endif
  2209. CURLcode result = CURLE_OK;
  2210. struct HTTP *http = data->req.p.http;
  2211. const char *ptr;
  2212. /* If 'authdone' is FALSE, we must not set the write socket index to the
  2213. Curl_transfer() call below, as we're not ready to actually upload any
  2214. data yet. */
  2215. switch(httpreq) {
  2216. case HTTPREQ_PUT: /* Let's PUT the data to the server! */
  2217. if(conn->bits.authneg)
  2218. http->postsize = 0;
  2219. else
  2220. http->postsize = data->state.infilesize;
  2221. if((http->postsize != -1) && !data->req.upload_chunky &&
  2222. (conn->bits.authneg ||
  2223. !Curl_checkheaders(data, STRCONST("Content-Length")))) {
  2224. /* only add Content-Length if not uploading chunked */
  2225. result = Curl_dyn_addf(r, "Content-Length: %" CURL_FORMAT_CURL_OFF_T
  2226. "\r\n", http->postsize);
  2227. if(result)
  2228. return result;
  2229. }
  2230. /* For really small puts we don't use Expect: headers at all, and for
  2231. the somewhat bigger ones we allow the app to disable it. Just make
  2232. sure that the expect100header is always set to the preferred value
  2233. here. */
  2234. ptr = Curl_checkheaders(data, STRCONST("Expect"));
  2235. if(ptr) {
  2236. data->state.expect100header =
  2237. Curl_compareheader(ptr, STRCONST("Expect:"), STRCONST("100-continue"));
  2238. }
  2239. else if(http->postsize > EXPECT_100_THRESHOLD || http->postsize < 0) {
  2240. result = expect100(data, conn, r);
  2241. if(result)
  2242. return result;
  2243. }
  2244. /* end of headers */
  2245. result = Curl_dyn_addn(r, STRCONST("\r\n"));
  2246. if(result)
  2247. return result;
  2248. /* set the upload size to the progress meter */
  2249. Curl_pgrsSetUploadSize(data, http->postsize);
  2250. /* this sends the buffer and frees all the buffer resources */
  2251. result = Curl_buffer_send(r, data, data->req.p.http,
  2252. &data->info.request_size, 0,
  2253. FIRSTSOCKET);
  2254. if(result)
  2255. failf(data, "Failed sending PUT request");
  2256. else
  2257. /* prepare for transfer */
  2258. Curl_setup_transfer(data, FIRSTSOCKET, -1, TRUE,
  2259. http->postsize?FIRSTSOCKET:-1);
  2260. if(result)
  2261. return result;
  2262. break;
  2263. case HTTPREQ_POST_FORM:
  2264. case HTTPREQ_POST_MIME:
  2265. /* This is form posting using mime data. */
  2266. if(conn->bits.authneg) {
  2267. /* nothing to post! */
  2268. result = Curl_dyn_addn(r, STRCONST("Content-Length: 0\r\n\r\n"));
  2269. if(result)
  2270. return result;
  2271. result = Curl_buffer_send(r, data, data->req.p.http,
  2272. &data->info.request_size, 0,
  2273. FIRSTSOCKET);
  2274. if(result)
  2275. failf(data, "Failed sending POST request");
  2276. else
  2277. /* setup variables for the upcoming transfer */
  2278. Curl_setup_transfer(data, FIRSTSOCKET, -1, TRUE, -1);
  2279. break;
  2280. }
  2281. data->state.infilesize = http->postsize;
  2282. /* We only set Content-Length and allow a custom Content-Length if
  2283. we don't upload data chunked, as RFC2616 forbids us to set both
  2284. kinds of headers (Transfer-Encoding: chunked and Content-Length) */
  2285. if(http->postsize != -1 && !data->req.upload_chunky &&
  2286. (!Curl_checkheaders(data, STRCONST("Content-Length")))) {
  2287. /* we allow replacing this header if not during auth negotiation,
  2288. although it isn't very wise to actually set your own */
  2289. result = Curl_dyn_addf(r,
  2290. "Content-Length: %" CURL_FORMAT_CURL_OFF_T
  2291. "\r\n", http->postsize);
  2292. if(result)
  2293. return result;
  2294. }
  2295. #ifndef CURL_DISABLE_MIME
  2296. /* Output mime-generated headers. */
  2297. {
  2298. struct curl_slist *hdr;
  2299. for(hdr = http->sendit->curlheaders; hdr; hdr = hdr->next) {
  2300. result = Curl_dyn_addf(r, "%s\r\n", hdr->data);
  2301. if(result)
  2302. return result;
  2303. }
  2304. }
  2305. #endif
  2306. /* For really small posts we don't use Expect: headers at all, and for
  2307. the somewhat bigger ones we allow the app to disable it. Just make
  2308. sure that the expect100header is always set to the preferred value
  2309. here. */
  2310. ptr = Curl_checkheaders(data, STRCONST("Expect"));
  2311. if(ptr) {
  2312. data->state.expect100header =
  2313. Curl_compareheader(ptr, STRCONST("Expect:"), STRCONST("100-continue"));
  2314. }
  2315. else if(http->postsize > EXPECT_100_THRESHOLD || http->postsize < 0) {
  2316. result = expect100(data, conn, r);
  2317. if(result)
  2318. return result;
  2319. }
  2320. else
  2321. data->state.expect100header = FALSE;
  2322. /* make the request end in a true CRLF */
  2323. result = Curl_dyn_addn(r, STRCONST("\r\n"));
  2324. if(result)
  2325. return result;
  2326. /* set the upload size to the progress meter */
  2327. Curl_pgrsSetUploadSize(data, http->postsize);
  2328. /* Read from mime structure. */
  2329. data->state.fread_func = (curl_read_callback) Curl_mime_read;
  2330. data->state.in = (void *) http->sendit;
  2331. http->sending = HTTPSEND_BODY;
  2332. /* this sends the buffer and frees all the buffer resources */
  2333. result = Curl_buffer_send(r, data, data->req.p.http,
  2334. &data->info.request_size, 0,
  2335. FIRSTSOCKET);
  2336. if(result)
  2337. failf(data, "Failed sending POST request");
  2338. else
  2339. /* prepare for transfer */
  2340. Curl_setup_transfer(data, FIRSTSOCKET, -1, TRUE,
  2341. http->postsize?FIRSTSOCKET:-1);
  2342. if(result)
  2343. return result;
  2344. break;
  2345. case HTTPREQ_POST:
  2346. /* this is the simple POST, using x-www-form-urlencoded style */
  2347. if(conn->bits.authneg)
  2348. http->postsize = 0;
  2349. else
  2350. /* the size of the post body */
  2351. http->postsize = data->state.infilesize;
  2352. /* We only set Content-Length and allow a custom Content-Length if
  2353. we don't upload data chunked, as RFC2616 forbids us to set both
  2354. kinds of headers (Transfer-Encoding: chunked and Content-Length) */
  2355. if((http->postsize != -1) && !data->req.upload_chunky &&
  2356. (conn->bits.authneg ||
  2357. !Curl_checkheaders(data, STRCONST("Content-Length")))) {
  2358. /* we allow replacing this header if not during auth negotiation,
  2359. although it isn't very wise to actually set your own */
  2360. result = Curl_dyn_addf(r, "Content-Length: %" CURL_FORMAT_CURL_OFF_T
  2361. "\r\n", http->postsize);
  2362. if(result)
  2363. return result;
  2364. }
  2365. if(!Curl_checkheaders(data, STRCONST("Content-Type"))) {
  2366. result = Curl_dyn_addn(r, STRCONST("Content-Type: application/"
  2367. "x-www-form-urlencoded\r\n"));
  2368. if(result)
  2369. return result;
  2370. }
  2371. /* For really small posts we don't use Expect: headers at all, and for
  2372. the somewhat bigger ones we allow the app to disable it. Just make
  2373. sure that the expect100header is always set to the preferred value
  2374. here. */
  2375. ptr = Curl_checkheaders(data, STRCONST("Expect"));
  2376. if(ptr) {
  2377. data->state.expect100header =
  2378. Curl_compareheader(ptr, STRCONST("Expect:"), STRCONST("100-continue"));
  2379. }
  2380. else if(http->postsize > EXPECT_100_THRESHOLD || http->postsize < 0) {
  2381. result = expect100(data, conn, r);
  2382. if(result)
  2383. return result;
  2384. }
  2385. else
  2386. data->state.expect100header = FALSE;
  2387. #ifndef USE_HYPER
  2388. /* With Hyper the body is always passed on separately */
  2389. if(data->set.postfields) {
  2390. if(!data->state.expect100header &&
  2391. (http->postsize < MAX_INITIAL_POST_SIZE)) {
  2392. /* if we don't use expect: 100 AND
  2393. postsize is less than MAX_INITIAL_POST_SIZE
  2394. then append the post data to the HTTP request header. This limit
  2395. is no magic limit but only set to prevent really huge POSTs to
  2396. get the data duplicated with malloc() and family. */
  2397. /* end of headers! */
  2398. result = Curl_dyn_addn(r, STRCONST("\r\n"));
  2399. if(result)
  2400. return result;
  2401. if(!data->req.upload_chunky) {
  2402. /* We're not sending it 'chunked', append it to the request
  2403. already now to reduce the number if send() calls */
  2404. result = Curl_dyn_addn(r, data->set.postfields,
  2405. (size_t)http->postsize);
  2406. included_body = http->postsize;
  2407. }
  2408. else {
  2409. if(http->postsize) {
  2410. char chunk[16];
  2411. /* Append the POST data chunky-style */
  2412. msnprintf(chunk, sizeof(chunk), "%x\r\n", (int)http->postsize);
  2413. result = Curl_dyn_add(r, chunk);
  2414. if(!result) {
  2415. included_body = http->postsize + strlen(chunk);
  2416. result = Curl_dyn_addn(r, data->set.postfields,
  2417. (size_t)http->postsize);
  2418. if(!result)
  2419. result = Curl_dyn_addn(r, STRCONST("\r\n"));
  2420. included_body += 2;
  2421. }
  2422. }
  2423. if(!result) {
  2424. result = Curl_dyn_addn(r, STRCONST("\x30\x0d\x0a\x0d\x0a"));
  2425. /* 0 CR LF CR LF */
  2426. included_body += 5;
  2427. }
  2428. }
  2429. if(result)
  2430. return result;
  2431. /* Make sure the progress information is accurate */
  2432. Curl_pgrsSetUploadSize(data, http->postsize);
  2433. }
  2434. else {
  2435. /* A huge POST coming up, do data separate from the request */
  2436. http->postdata = data->set.postfields;
  2437. http->sending = HTTPSEND_BODY;
  2438. http->backup.data = data;
  2439. data->state.fread_func = (curl_read_callback)readmoredata;
  2440. data->state.in = (void *)http;
  2441. /* set the upload size to the progress meter */
  2442. Curl_pgrsSetUploadSize(data, http->postsize);
  2443. /* end of headers! */
  2444. result = Curl_dyn_addn(r, STRCONST("\r\n"));
  2445. if(result)
  2446. return result;
  2447. }
  2448. }
  2449. else
  2450. #endif
  2451. {
  2452. /* end of headers! */
  2453. result = Curl_dyn_addn(r, STRCONST("\r\n"));
  2454. if(result)
  2455. return result;
  2456. if(data->req.upload_chunky && conn->bits.authneg) {
  2457. /* Chunky upload is selected and we're negotiating auth still, send
  2458. end-of-data only */
  2459. result = Curl_dyn_addn(r, (char *)STRCONST("\x30\x0d\x0a\x0d\x0a"));
  2460. /* 0 CR LF CR LF */
  2461. if(result)
  2462. return result;
  2463. }
  2464. else if(data->state.infilesize) {
  2465. /* set the upload size to the progress meter */
  2466. Curl_pgrsSetUploadSize(data, http->postsize?http->postsize:-1);
  2467. /* set the pointer to mark that we will send the post body using the
  2468. read callback, but only if we're not in authenticate negotiation */
  2469. if(!conn->bits.authneg)
  2470. http->postdata = (char *)&http->postdata;
  2471. }
  2472. }
  2473. /* issue the request */
  2474. result = Curl_buffer_send(r, data, data->req.p.http,
  2475. &data->info.request_size, included_body,
  2476. FIRSTSOCKET);
  2477. if(result)
  2478. failf(data, "Failed sending HTTP POST request");
  2479. else
  2480. Curl_setup_transfer(data, FIRSTSOCKET, -1, TRUE,
  2481. http->postdata?FIRSTSOCKET:-1);
  2482. break;
  2483. default:
  2484. result = Curl_dyn_addn(r, STRCONST("\r\n"));
  2485. if(result)
  2486. return result;
  2487. /* issue the request */
  2488. result = Curl_buffer_send(r, data, data->req.p.http,
  2489. &data->info.request_size, 0,
  2490. FIRSTSOCKET);
  2491. if(result)
  2492. failf(data, "Failed sending HTTP request");
  2493. #ifdef USE_WEBSOCKETS
  2494. else if((conn->handler->protocol & (CURLPROTO_WS|CURLPROTO_WSS)) &&
  2495. !(data->set.connect_only))
  2496. /* Set up the transfer for two-way since without CONNECT_ONLY set, this
  2497. request probably wants to send data too post upgrade */
  2498. Curl_setup_transfer(data, FIRSTSOCKET, -1, TRUE, FIRSTSOCKET);
  2499. #endif
  2500. else
  2501. /* HTTP GET/HEAD download: */
  2502. Curl_setup_transfer(data, FIRSTSOCKET, -1, TRUE, -1);
  2503. }
  2504. return result;
  2505. }
  2506. #if !defined(CURL_DISABLE_COOKIES)
  2507. CURLcode Curl_http_cookies(struct Curl_easy *data,
  2508. struct connectdata *conn,
  2509. struct dynbuf *r)
  2510. {
  2511. CURLcode result = CURLE_OK;
  2512. char *addcookies = NULL;
  2513. bool linecap = FALSE;
  2514. if(data->set.str[STRING_COOKIE] &&
  2515. !Curl_checkheaders(data, STRCONST("Cookie")))
  2516. addcookies = data->set.str[STRING_COOKIE];
  2517. if(data->cookies || addcookies) {
  2518. struct Cookie *co = NULL; /* no cookies from start */
  2519. int count = 0;
  2520. if(data->cookies && data->state.cookie_engine) {
  2521. const char *host = data->state.aptr.cookiehost ?
  2522. data->state.aptr.cookiehost : conn->host.name;
  2523. const bool secure_context =
  2524. conn->handler->protocol&(CURLPROTO_HTTPS|CURLPROTO_WSS) ||
  2525. strcasecompare("localhost", host) ||
  2526. !strcmp(host, "127.0.0.1") ||
  2527. !strcmp(host, "::1") ? TRUE : FALSE;
  2528. Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
  2529. co = Curl_cookie_getlist(data, data->cookies, host, data->state.up.path,
  2530. secure_context);
  2531. Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
  2532. }
  2533. if(co) {
  2534. struct Cookie *store = co;
  2535. size_t clen = 8; /* hold the size of the generated Cookie: header */
  2536. /* now loop through all cookies that matched */
  2537. while(co) {
  2538. if(co->value) {
  2539. size_t add;
  2540. if(!count) {
  2541. result = Curl_dyn_addn(r, STRCONST("Cookie: "));
  2542. if(result)
  2543. break;
  2544. }
  2545. add = strlen(co->name) + strlen(co->value) + 1;
  2546. if(clen + add >= MAX_COOKIE_HEADER_LEN) {
  2547. infof(data, "Restricted outgoing cookies due to header size, "
  2548. "'%s' not sent", co->name);
  2549. linecap = TRUE;
  2550. break;
  2551. }
  2552. result = Curl_dyn_addf(r, "%s%s=%s", count?"; ":"",
  2553. co->name, co->value);
  2554. if(result)
  2555. break;
  2556. clen += add + (count ? 2 : 0);
  2557. count++;
  2558. }
  2559. co = co->next; /* next cookie please */
  2560. }
  2561. Curl_cookie_freelist(store);
  2562. }
  2563. if(addcookies && !result && !linecap) {
  2564. if(!count)
  2565. result = Curl_dyn_addn(r, STRCONST("Cookie: "));
  2566. if(!result) {
  2567. result = Curl_dyn_addf(r, "%s%s", count?"; ":"", addcookies);
  2568. count++;
  2569. }
  2570. }
  2571. if(count && !result)
  2572. result = Curl_dyn_addn(r, STRCONST("\r\n"));
  2573. if(result)
  2574. return result;
  2575. }
  2576. return result;
  2577. }
  2578. #endif
  2579. CURLcode Curl_http_range(struct Curl_easy *data,
  2580. Curl_HttpReq httpreq)
  2581. {
  2582. if(data->state.use_range) {
  2583. /*
  2584. * A range is selected. We use different headers whether we're downloading
  2585. * or uploading and we always let customized headers override our internal
  2586. * ones if any such are specified.
  2587. */
  2588. if(((httpreq == HTTPREQ_GET) || (httpreq == HTTPREQ_HEAD)) &&
  2589. !Curl_checkheaders(data, STRCONST("Range"))) {
  2590. /* if a line like this was already allocated, free the previous one */
  2591. free(data->state.aptr.rangeline);
  2592. data->state.aptr.rangeline = aprintf("Range: bytes=%s\r\n",
  2593. data->state.range);
  2594. }
  2595. else if((httpreq == HTTPREQ_POST || httpreq == HTTPREQ_PUT) &&
  2596. !Curl_checkheaders(data, STRCONST("Content-Range"))) {
  2597. /* if a line like this was already allocated, free the previous one */
  2598. free(data->state.aptr.rangeline);
  2599. if(data->set.set_resume_from < 0) {
  2600. /* Upload resume was asked for, but we don't know the size of the
  2601. remote part so we tell the server (and act accordingly) that we
  2602. upload the whole file (again) */
  2603. data->state.aptr.rangeline =
  2604. aprintf("Content-Range: bytes 0-%" CURL_FORMAT_CURL_OFF_T
  2605. "/%" CURL_FORMAT_CURL_OFF_T "\r\n",
  2606. data->state.infilesize - 1, data->state.infilesize);
  2607. }
  2608. else if(data->state.resume_from) {
  2609. /* This is because "resume" was selected */
  2610. curl_off_t total_expected_size =
  2611. data->state.resume_from + data->state.infilesize;
  2612. data->state.aptr.rangeline =
  2613. aprintf("Content-Range: bytes %s%" CURL_FORMAT_CURL_OFF_T
  2614. "/%" CURL_FORMAT_CURL_OFF_T "\r\n",
  2615. data->state.range, total_expected_size-1,
  2616. total_expected_size);
  2617. }
  2618. else {
  2619. /* Range was selected and then we just pass the incoming range and
  2620. append total size */
  2621. data->state.aptr.rangeline =
  2622. aprintf("Content-Range: bytes %s/%" CURL_FORMAT_CURL_OFF_T "\r\n",
  2623. data->state.range, data->state.infilesize);
  2624. }
  2625. if(!data->state.aptr.rangeline)
  2626. return CURLE_OUT_OF_MEMORY;
  2627. }
  2628. }
  2629. return CURLE_OK;
  2630. }
  2631. CURLcode Curl_http_resume(struct Curl_easy *data,
  2632. struct connectdata *conn,
  2633. Curl_HttpReq httpreq)
  2634. {
  2635. if((HTTPREQ_POST == httpreq || HTTPREQ_PUT == httpreq) &&
  2636. data->state.resume_from) {
  2637. /**********************************************************************
  2638. * Resuming upload in HTTP means that we PUT or POST and that we have
  2639. * got a resume_from value set. The resume value has already created
  2640. * a Range: header that will be passed along. We need to "fast forward"
  2641. * the file the given number of bytes and decrease the assume upload
  2642. * file size before we continue this venture in the dark lands of HTTP.
  2643. * Resuming mime/form posting at an offset > 0 has no sense and is ignored.
  2644. *********************************************************************/
  2645. if(data->state.resume_from < 0) {
  2646. /*
  2647. * This is meant to get the size of the present remote-file by itself.
  2648. * We don't support this now. Bail out!
  2649. */
  2650. data->state.resume_from = 0;
  2651. }
  2652. if(data->state.resume_from && !data->state.followlocation) {
  2653. /* only act on the first request */
  2654. /* Now, let's read off the proper amount of bytes from the
  2655. input. */
  2656. int seekerr = CURL_SEEKFUNC_CANTSEEK;
  2657. if(conn->seek_func) {
  2658. Curl_set_in_callback(data, true);
  2659. seekerr = conn->seek_func(conn->seek_client, data->state.resume_from,
  2660. SEEK_SET);
  2661. Curl_set_in_callback(data, false);
  2662. }
  2663. if(seekerr != CURL_SEEKFUNC_OK) {
  2664. curl_off_t passed = 0;
  2665. if(seekerr != CURL_SEEKFUNC_CANTSEEK) {
  2666. failf(data, "Could not seek stream");
  2667. return CURLE_READ_ERROR;
  2668. }
  2669. /* when seekerr == CURL_SEEKFUNC_CANTSEEK (can't seek to offset) */
  2670. do {
  2671. size_t readthisamountnow =
  2672. (data->state.resume_from - passed > data->set.buffer_size) ?
  2673. (size_t)data->set.buffer_size :
  2674. curlx_sotouz(data->state.resume_from - passed);
  2675. size_t actuallyread =
  2676. data->state.fread_func(data->state.buffer, 1, readthisamountnow,
  2677. data->state.in);
  2678. passed += actuallyread;
  2679. if((actuallyread == 0) || (actuallyread > readthisamountnow)) {
  2680. /* this checks for greater-than only to make sure that the
  2681. CURL_READFUNC_ABORT return code still aborts */
  2682. failf(data, "Could only read %" CURL_FORMAT_CURL_OFF_T
  2683. " bytes from the input", passed);
  2684. return CURLE_READ_ERROR;
  2685. }
  2686. } while(passed < data->state.resume_from);
  2687. }
  2688. /* now, decrease the size of the read */
  2689. if(data->state.infilesize>0) {
  2690. data->state.infilesize -= data->state.resume_from;
  2691. if(data->state.infilesize <= 0) {
  2692. failf(data, "File already completely uploaded");
  2693. return CURLE_PARTIAL_FILE;
  2694. }
  2695. }
  2696. /* we've passed, proceed as normal */
  2697. }
  2698. }
  2699. return CURLE_OK;
  2700. }
  2701. CURLcode Curl_http_firstwrite(struct Curl_easy *data,
  2702. struct connectdata *conn,
  2703. bool *done)
  2704. {
  2705. struct SingleRequest *k = &data->req;
  2706. if(data->req.newurl) {
  2707. if(conn->bits.close) {
  2708. /* Abort after the headers if "follow Location" is set
  2709. and we're set to close anyway. */
  2710. k->keepon &= ~KEEP_RECV;
  2711. *done = TRUE;
  2712. return CURLE_OK;
  2713. }
  2714. /* We have a new url to load, but since we want to be able to re-use this
  2715. connection properly, we read the full response in "ignore more" */
  2716. k->ignorebody = TRUE;
  2717. infof(data, "Ignoring the response-body");
  2718. }
  2719. if(data->state.resume_from && !k->content_range &&
  2720. (data->state.httpreq == HTTPREQ_GET) &&
  2721. !k->ignorebody) {
  2722. if(k->size == data->state.resume_from) {
  2723. /* The resume point is at the end of file, consider this fine even if it
  2724. doesn't allow resume from here. */
  2725. infof(data, "The entire document is already downloaded");
  2726. streamclose(conn, "already downloaded");
  2727. /* Abort download */
  2728. k->keepon &= ~KEEP_RECV;
  2729. *done = TRUE;
  2730. return CURLE_OK;
  2731. }
  2732. /* we wanted to resume a download, although the server doesn't seem to
  2733. * support this and we did this with a GET (if it wasn't a GET we did a
  2734. * POST or PUT resume) */
  2735. failf(data, "HTTP server doesn't seem to support "
  2736. "byte ranges. Cannot resume.");
  2737. return CURLE_RANGE_ERROR;
  2738. }
  2739. if(data->set.timecondition && !data->state.range) {
  2740. /* A time condition has been set AND no ranges have been requested. This
  2741. seems to be what chapter 13.3.4 of RFC 2616 defines to be the correct
  2742. action for an HTTP/1.1 client */
  2743. if(!Curl_meets_timecondition(data, k->timeofdoc)) {
  2744. *done = TRUE;
  2745. /* We're simulating an HTTP 304 from server so we return
  2746. what should have been returned from the server */
  2747. data->info.httpcode = 304;
  2748. infof(data, "Simulate an HTTP 304 response");
  2749. /* we abort the transfer before it is completed == we ruin the
  2750. re-use ability. Close the connection */
  2751. streamclose(conn, "Simulated 304 handling");
  2752. return CURLE_OK;
  2753. }
  2754. } /* we have a time condition */
  2755. return CURLE_OK;
  2756. }
  2757. #ifdef HAVE_LIBZ
  2758. CURLcode Curl_transferencode(struct Curl_easy *data)
  2759. {
  2760. if(!Curl_checkheaders(data, STRCONST("TE")) &&
  2761. data->set.http_transfer_encoding) {
  2762. /* When we are to insert a TE: header in the request, we must also insert
  2763. TE in a Connection: header, so we need to merge the custom provided
  2764. Connection: header and prevent the original to get sent. Note that if
  2765. the user has inserted his/her own TE: header we don't do this magic
  2766. but then assume that the user will handle it all! */
  2767. char *cptr = Curl_checkheaders(data, STRCONST("Connection"));
  2768. #define TE_HEADER "TE: gzip\r\n"
  2769. Curl_safefree(data->state.aptr.te);
  2770. if(cptr) {
  2771. cptr = Curl_copy_header_value(cptr);
  2772. if(!cptr)
  2773. return CURLE_OUT_OF_MEMORY;
  2774. }
  2775. /* Create the (updated) Connection: header */
  2776. data->state.aptr.te = aprintf("Connection: %s%sTE\r\n" TE_HEADER,
  2777. cptr ? cptr : "", (cptr && *cptr) ? ", ":"");
  2778. free(cptr);
  2779. if(!data->state.aptr.te)
  2780. return CURLE_OUT_OF_MEMORY;
  2781. }
  2782. return CURLE_OK;
  2783. }
  2784. #endif
  2785. #ifndef USE_HYPER
  2786. /*
  2787. * Curl_http() gets called from the generic multi_do() function when an HTTP
  2788. * request is to be performed. This creates and sends a properly constructed
  2789. * HTTP request.
  2790. */
  2791. CURLcode Curl_http(struct Curl_easy *data, bool *done)
  2792. {
  2793. struct connectdata *conn = data->conn;
  2794. CURLcode result = CURLE_OK;
  2795. struct HTTP *http;
  2796. Curl_HttpReq httpreq;
  2797. const char *te = ""; /* transfer-encoding */
  2798. const char *request;
  2799. const char *httpstring;
  2800. struct dynbuf req;
  2801. char *altused = NULL;
  2802. const char *p_accept; /* Accept: string */
  2803. /* Always consider the DO phase done after this function call, even if there
  2804. may be parts of the request that are not yet sent, since we can deal with
  2805. the rest of the request in the PERFORM phase. */
  2806. *done = TRUE;
  2807. switch(conn->alpn) {
  2808. case CURL_HTTP_VERSION_3:
  2809. DEBUGASSERT(Curl_conn_is_http3(data, conn, FIRSTSOCKET));
  2810. break;
  2811. case CURL_HTTP_VERSION_2:
  2812. #ifndef CURL_DISABLE_PROXY
  2813. if(!Curl_conn_is_http2(data, conn, FIRSTSOCKET) &&
  2814. conn->bits.proxy && !conn->bits.tunnel_proxy
  2815. ) {
  2816. result = Curl_http2_switch(data, conn, FIRSTSOCKET);
  2817. if(result)
  2818. return result;
  2819. }
  2820. else
  2821. #endif
  2822. DEBUGASSERT(Curl_conn_is_http2(data, conn, FIRSTSOCKET));
  2823. break;
  2824. case CURL_HTTP_VERSION_1_1:
  2825. /* continue with HTTP/1.1 when explicitly requested */
  2826. break;
  2827. default:
  2828. /* Check if user wants to use HTTP/2 with clear TCP */
  2829. if(Curl_http2_may_switch(data, conn, FIRSTSOCKET)) {
  2830. DEBUGF(infof(data, "HTTP/2 over clean TCP"));
  2831. result = Curl_http2_switch(data, conn, FIRSTSOCKET);
  2832. if(result)
  2833. return result;
  2834. }
  2835. break;
  2836. }
  2837. http = data->req.p.http;
  2838. DEBUGASSERT(http);
  2839. result = Curl_http_host(data, conn);
  2840. if(result)
  2841. return result;
  2842. result = Curl_http_useragent(data);
  2843. if(result)
  2844. return result;
  2845. Curl_http_method(data, conn, &request, &httpreq);
  2846. /* setup the authentication headers */
  2847. {
  2848. char *pq = NULL;
  2849. if(data->state.up.query) {
  2850. pq = aprintf("%s?%s", data->state.up.path, data->state.up.query);
  2851. if(!pq)
  2852. return CURLE_OUT_OF_MEMORY;
  2853. }
  2854. result = Curl_http_output_auth(data, conn, request, httpreq,
  2855. (pq ? pq : data->state.up.path), FALSE);
  2856. free(pq);
  2857. if(result)
  2858. return result;
  2859. }
  2860. Curl_safefree(data->state.aptr.ref);
  2861. if(data->state.referer && !Curl_checkheaders(data, STRCONST("Referer"))) {
  2862. data->state.aptr.ref = aprintf("Referer: %s\r\n", data->state.referer);
  2863. if(!data->state.aptr.ref)
  2864. return CURLE_OUT_OF_MEMORY;
  2865. }
  2866. if(!Curl_checkheaders(data, STRCONST("Accept-Encoding")) &&
  2867. data->set.str[STRING_ENCODING]) {
  2868. Curl_safefree(data->state.aptr.accept_encoding);
  2869. data->state.aptr.accept_encoding =
  2870. aprintf("Accept-Encoding: %s\r\n", data->set.str[STRING_ENCODING]);
  2871. if(!data->state.aptr.accept_encoding)
  2872. return CURLE_OUT_OF_MEMORY;
  2873. }
  2874. else
  2875. Curl_safefree(data->state.aptr.accept_encoding);
  2876. #ifdef HAVE_LIBZ
  2877. /* we only consider transfer-encoding magic if libz support is built-in */
  2878. result = Curl_transferencode(data);
  2879. if(result)
  2880. return result;
  2881. #endif
  2882. result = Curl_http_body(data, conn, httpreq, &te);
  2883. if(result)
  2884. return result;
  2885. p_accept = Curl_checkheaders(data,
  2886. STRCONST("Accept"))?NULL:"Accept: */*\r\n";
  2887. result = Curl_http_resume(data, conn, httpreq);
  2888. if(result)
  2889. return result;
  2890. result = Curl_http_range(data, httpreq);
  2891. if(result)
  2892. return result;
  2893. httpstring = get_http_string(data, conn);
  2894. /* initialize a dynamic send-buffer */
  2895. Curl_dyn_init(&req, DYN_HTTP_REQUEST);
  2896. /* make sure the header buffer is reset - if there are leftovers from a
  2897. previous transfer */
  2898. Curl_dyn_reset(&data->state.headerb);
  2899. /* add the main request stuff */
  2900. /* GET/HEAD/POST/PUT */
  2901. result = Curl_dyn_addf(&req, "%s ", request);
  2902. if(!result)
  2903. result = Curl_http_target(data, conn, &req);
  2904. if(result) {
  2905. Curl_dyn_free(&req);
  2906. return result;
  2907. }
  2908. #ifndef CURL_DISABLE_ALTSVC
  2909. if(conn->bits.altused && !Curl_checkheaders(data, STRCONST("Alt-Used"))) {
  2910. altused = aprintf("Alt-Used: %s:%d\r\n",
  2911. conn->conn_to_host.name, conn->conn_to_port);
  2912. if(!altused) {
  2913. Curl_dyn_free(&req);
  2914. return CURLE_OUT_OF_MEMORY;
  2915. }
  2916. }
  2917. #endif
  2918. result =
  2919. Curl_dyn_addf(&req,
  2920. " HTTP/%s\r\n" /* HTTP version */
  2921. "%s" /* host */
  2922. "%s" /* proxyuserpwd */
  2923. "%s" /* userpwd */
  2924. "%s" /* range */
  2925. "%s" /* user agent */
  2926. "%s" /* accept */
  2927. "%s" /* TE: */
  2928. "%s" /* accept-encoding */
  2929. "%s" /* referer */
  2930. "%s" /* Proxy-Connection */
  2931. "%s" /* transfer-encoding */
  2932. "%s",/* Alt-Used */
  2933. httpstring,
  2934. (data->state.aptr.host?data->state.aptr.host:""),
  2935. data->state.aptr.proxyuserpwd?
  2936. data->state.aptr.proxyuserpwd:"",
  2937. data->state.aptr.userpwd?data->state.aptr.userpwd:"",
  2938. (data->state.use_range && data->state.aptr.rangeline)?
  2939. data->state.aptr.rangeline:"",
  2940. (data->set.str[STRING_USERAGENT] &&
  2941. *data->set.str[STRING_USERAGENT] &&
  2942. data->state.aptr.uagent)?
  2943. data->state.aptr.uagent:"",
  2944. p_accept?p_accept:"",
  2945. data->state.aptr.te?data->state.aptr.te:"",
  2946. (data->set.str[STRING_ENCODING] &&
  2947. *data->set.str[STRING_ENCODING] &&
  2948. data->state.aptr.accept_encoding)?
  2949. data->state.aptr.accept_encoding:"",
  2950. (data->state.referer && data->state.aptr.ref)?
  2951. data->state.aptr.ref:"" /* Referer: <data> */,
  2952. #ifndef CURL_DISABLE_PROXY
  2953. (conn->bits.httpproxy &&
  2954. !conn->bits.tunnel_proxy &&
  2955. !Curl_checkheaders(data, STRCONST("Proxy-Connection")) &&
  2956. !Curl_checkProxyheaders(data,
  2957. conn,
  2958. STRCONST("Proxy-Connection")))?
  2959. "Proxy-Connection: Keep-Alive\r\n":"",
  2960. #else
  2961. "",
  2962. #endif
  2963. te,
  2964. altused ? altused : ""
  2965. );
  2966. /* clear userpwd and proxyuserpwd to avoid re-using old credentials
  2967. * from re-used connections */
  2968. Curl_safefree(data->state.aptr.userpwd);
  2969. Curl_safefree(data->state.aptr.proxyuserpwd);
  2970. free(altused);
  2971. if(result) {
  2972. Curl_dyn_free(&req);
  2973. return result;
  2974. }
  2975. if(!(conn->handler->flags&PROTOPT_SSL) &&
  2976. conn->httpversion < 20 &&
  2977. (data->state.httpwant == CURL_HTTP_VERSION_2)) {
  2978. /* append HTTP2 upgrade magic stuff to the HTTP request if it isn't done
  2979. over SSL */
  2980. result = Curl_http2_request_upgrade(&req, data);
  2981. if(result) {
  2982. Curl_dyn_free(&req);
  2983. return result;
  2984. }
  2985. }
  2986. result = Curl_http_cookies(data, conn, &req);
  2987. #ifdef USE_WEBSOCKETS
  2988. if(!result && conn->handler->protocol&(CURLPROTO_WS|CURLPROTO_WSS))
  2989. result = Curl_ws_request(data, &req);
  2990. #endif
  2991. if(!result)
  2992. result = Curl_add_timecondition(data, &req);
  2993. if(!result)
  2994. result = Curl_add_custom_headers(data, FALSE, &req);
  2995. if(!result) {
  2996. http->postdata = NULL; /* nothing to post at this point */
  2997. if((httpreq == HTTPREQ_GET) ||
  2998. (httpreq == HTTPREQ_HEAD))
  2999. Curl_pgrsSetUploadSize(data, 0); /* nothing */
  3000. /* bodysend takes ownership of the 'req' memory on success */
  3001. result = Curl_http_bodysend(data, conn, &req, httpreq);
  3002. }
  3003. if(result) {
  3004. Curl_dyn_free(&req);
  3005. return result;
  3006. }
  3007. if((http->postsize > -1) &&
  3008. (http->postsize <= data->req.writebytecount) &&
  3009. (http->sending != HTTPSEND_REQUEST))
  3010. data->req.upload_done = TRUE;
  3011. if(data->req.writebytecount) {
  3012. /* if a request-body has been sent off, we make sure this progress is noted
  3013. properly */
  3014. Curl_pgrsSetUploadCounter(data, data->req.writebytecount);
  3015. if(Curl_pgrsUpdate(data))
  3016. result = CURLE_ABORTED_BY_CALLBACK;
  3017. if(!http->postsize) {
  3018. /* already sent the entire request body, mark the "upload" as
  3019. complete */
  3020. infof(data, "upload completely sent off: %" CURL_FORMAT_CURL_OFF_T
  3021. " out of %" CURL_FORMAT_CURL_OFF_T " bytes",
  3022. data->req.writebytecount, http->postsize);
  3023. data->req.upload_done = TRUE;
  3024. data->req.keepon &= ~KEEP_SEND; /* we're done writing */
  3025. data->req.exp100 = EXP100_SEND_DATA; /* already sent */
  3026. Curl_expire_done(data, EXPIRE_100_TIMEOUT);
  3027. }
  3028. }
  3029. if(data->req.upload_done)
  3030. Curl_conn_ev_data_done_send(data);
  3031. if((conn->httpversion >= 20) && data->req.upload_chunky)
  3032. /* upload_chunky was set above to set up the request in a chunky fashion,
  3033. but is disabled here again to avoid that the chunked encoded version is
  3034. actually used when sending the request body over h2 */
  3035. data->req.upload_chunky = FALSE;
  3036. return result;
  3037. }
  3038. #endif /* USE_HYPER */
  3039. typedef enum {
  3040. STATUS_UNKNOWN, /* not enough data to tell yet */
  3041. STATUS_DONE, /* a status line was read */
  3042. STATUS_BAD /* not a status line */
  3043. } statusline;
  3044. /* Check a string for a prefix. Check no more than 'len' bytes */
  3045. static bool checkprefixmax(const char *prefix, const char *buffer, size_t len)
  3046. {
  3047. size_t ch = CURLMIN(strlen(prefix), len);
  3048. return curl_strnequal(prefix, buffer, ch);
  3049. }
  3050. /*
  3051. * checkhttpprefix()
  3052. *
  3053. * Returns TRUE if member of the list matches prefix of string
  3054. */
  3055. static statusline
  3056. checkhttpprefix(struct Curl_easy *data,
  3057. const char *s, size_t len)
  3058. {
  3059. struct curl_slist *head = data->set.http200aliases;
  3060. statusline rc = STATUS_BAD;
  3061. statusline onmatch = len >= 5? STATUS_DONE : STATUS_UNKNOWN;
  3062. while(head) {
  3063. if(checkprefixmax(head->data, s, len)) {
  3064. rc = onmatch;
  3065. break;
  3066. }
  3067. head = head->next;
  3068. }
  3069. if((rc != STATUS_DONE) && (checkprefixmax("HTTP/", s, len)))
  3070. rc = onmatch;
  3071. return rc;
  3072. }
  3073. #ifndef CURL_DISABLE_RTSP
  3074. static statusline
  3075. checkrtspprefix(struct Curl_easy *data,
  3076. const char *s, size_t len)
  3077. {
  3078. statusline result = STATUS_BAD;
  3079. statusline onmatch = len >= 5? STATUS_DONE : STATUS_UNKNOWN;
  3080. (void)data; /* unused */
  3081. if(checkprefixmax("RTSP/", s, len))
  3082. result = onmatch;
  3083. return result;
  3084. }
  3085. #endif /* CURL_DISABLE_RTSP */
  3086. static statusline
  3087. checkprotoprefix(struct Curl_easy *data, struct connectdata *conn,
  3088. const char *s, size_t len)
  3089. {
  3090. #ifndef CURL_DISABLE_RTSP
  3091. if(conn->handler->protocol & CURLPROTO_RTSP)
  3092. return checkrtspprefix(data, s, len);
  3093. #else
  3094. (void)conn;
  3095. #endif /* CURL_DISABLE_RTSP */
  3096. return checkhttpprefix(data, s, len);
  3097. }
  3098. /*
  3099. * Curl_http_header() parses a single response header.
  3100. */
  3101. CURLcode Curl_http_header(struct Curl_easy *data, struct connectdata *conn,
  3102. char *headp)
  3103. {
  3104. CURLcode result;
  3105. struct SingleRequest *k = &data->req;
  3106. /* Check for Content-Length: header lines to get size */
  3107. if(!k->http_bodyless &&
  3108. !data->set.ignorecl && checkprefix("Content-Length:", headp)) {
  3109. curl_off_t contentlength;
  3110. CURLofft offt = curlx_strtoofft(headp + strlen("Content-Length:"),
  3111. NULL, 10, &contentlength);
  3112. if(offt == CURL_OFFT_OK) {
  3113. k->size = contentlength;
  3114. k->maxdownload = k->size;
  3115. }
  3116. else if(offt == CURL_OFFT_FLOW) {
  3117. /* out of range */
  3118. if(data->set.max_filesize) {
  3119. failf(data, "Maximum file size exceeded");
  3120. return CURLE_FILESIZE_EXCEEDED;
  3121. }
  3122. streamclose(conn, "overflow content-length");
  3123. infof(data, "Overflow Content-Length: value");
  3124. }
  3125. else {
  3126. /* negative or just rubbish - bad HTTP */
  3127. failf(data, "Invalid Content-Length: value");
  3128. return CURLE_WEIRD_SERVER_REPLY;
  3129. }
  3130. }
  3131. /* check for Content-Type: header lines to get the MIME-type */
  3132. else if(checkprefix("Content-Type:", headp)) {
  3133. char *contenttype = Curl_copy_header_value(headp);
  3134. if(!contenttype)
  3135. return CURLE_OUT_OF_MEMORY;
  3136. if(!*contenttype)
  3137. /* ignore empty data */
  3138. free(contenttype);
  3139. else {
  3140. Curl_safefree(data->info.contenttype);
  3141. data->info.contenttype = contenttype;
  3142. }
  3143. }
  3144. #ifndef CURL_DISABLE_PROXY
  3145. else if((conn->httpversion == 10) &&
  3146. conn->bits.httpproxy &&
  3147. Curl_compareheader(headp,
  3148. STRCONST("Proxy-Connection:"),
  3149. STRCONST("keep-alive"))) {
  3150. /*
  3151. * When an HTTP/1.0 reply comes when using a proxy, the
  3152. * 'Proxy-Connection: keep-alive' line tells us the
  3153. * connection will be kept alive for our pleasure.
  3154. * Default action for 1.0 is to close.
  3155. */
  3156. connkeep(conn, "Proxy-Connection keep-alive"); /* don't close */
  3157. infof(data, "HTTP/1.0 proxy connection set to keep alive");
  3158. }
  3159. else if((conn->httpversion == 11) &&
  3160. conn->bits.httpproxy &&
  3161. Curl_compareheader(headp,
  3162. STRCONST("Proxy-Connection:"),
  3163. STRCONST("close"))) {
  3164. /*
  3165. * We get an HTTP/1.1 response from a proxy and it says it'll
  3166. * close down after this transfer.
  3167. */
  3168. connclose(conn, "Proxy-Connection: asked to close after done");
  3169. infof(data, "HTTP/1.1 proxy connection set close");
  3170. }
  3171. #endif
  3172. else if((conn->httpversion == 10) &&
  3173. Curl_compareheader(headp,
  3174. STRCONST("Connection:"),
  3175. STRCONST("keep-alive"))) {
  3176. /*
  3177. * An HTTP/1.0 reply with the 'Connection: keep-alive' line
  3178. * tells us the connection will be kept alive for our
  3179. * pleasure. Default action for 1.0 is to close.
  3180. *
  3181. * [RFC2068, section 19.7.1] */
  3182. connkeep(conn, "Connection keep-alive");
  3183. infof(data, "HTTP/1.0 connection set to keep alive");
  3184. }
  3185. else if(Curl_compareheader(headp,
  3186. STRCONST("Connection:"), STRCONST("close"))) {
  3187. /*
  3188. * [RFC 2616, section 8.1.2.1]
  3189. * "Connection: close" is HTTP/1.1 language and means that
  3190. * the connection will close when this request has been
  3191. * served.
  3192. */
  3193. streamclose(conn, "Connection: close used");
  3194. }
  3195. else if(!k->http_bodyless && checkprefix("Transfer-Encoding:", headp)) {
  3196. /* One or more encodings. We check for chunked and/or a compression
  3197. algorithm. */
  3198. /*
  3199. * [RFC 2616, section 3.6.1] A 'chunked' transfer encoding
  3200. * means that the server will send a series of "chunks". Each
  3201. * chunk starts with line with info (including size of the
  3202. * coming block) (terminated with CRLF), then a block of data
  3203. * with the previously mentioned size. There can be any amount
  3204. * of chunks, and a chunk-data set to zero signals the
  3205. * end-of-chunks. */
  3206. result = Curl_build_unencoding_stack(data,
  3207. headp + strlen("Transfer-Encoding:"),
  3208. TRUE);
  3209. if(result)
  3210. return result;
  3211. if(!k->chunk && data->set.http_transfer_encoding) {
  3212. /* if this isn't chunked, only close can signal the end of this transfer
  3213. as Content-Length is said not to be trusted for transfer-encoding! */
  3214. connclose(conn, "HTTP/1.1 transfer-encoding without chunks");
  3215. k->ignore_cl = TRUE;
  3216. }
  3217. }
  3218. else if(!k->http_bodyless && checkprefix("Content-Encoding:", headp) &&
  3219. data->set.str[STRING_ENCODING]) {
  3220. /*
  3221. * Process Content-Encoding. Look for the values: identity,
  3222. * gzip, deflate, compress, x-gzip and x-compress. x-gzip and
  3223. * x-compress are the same as gzip and compress. (Sec 3.5 RFC
  3224. * 2616). zlib cannot handle compress. However, errors are
  3225. * handled further down when the response body is processed
  3226. */
  3227. result = Curl_build_unencoding_stack(data,
  3228. headp + strlen("Content-Encoding:"),
  3229. FALSE);
  3230. if(result)
  3231. return result;
  3232. }
  3233. else if(checkprefix("Retry-After:", headp)) {
  3234. /* Retry-After = HTTP-date / delay-seconds */
  3235. curl_off_t retry_after = 0; /* zero for unknown or "now" */
  3236. /* Try it as a decimal number, if it works it is not a date */
  3237. (void)curlx_strtoofft(headp + strlen("Retry-After:"),
  3238. NULL, 10, &retry_after);
  3239. if(!retry_after) {
  3240. time_t date = Curl_getdate_capped(headp + strlen("Retry-After:"));
  3241. if(-1 != date)
  3242. /* convert date to number of seconds into the future */
  3243. retry_after = date - time(NULL);
  3244. }
  3245. data->info.retry_after = retry_after; /* store it */
  3246. }
  3247. else if(!k->http_bodyless && checkprefix("Content-Range:", headp)) {
  3248. /* Content-Range: bytes [num]-
  3249. Content-Range: bytes: [num]-
  3250. Content-Range: [num]-
  3251. Content-Range: [asterisk]/[total]
  3252. The second format was added since Sun's webserver
  3253. JavaWebServer/1.1.1 obviously sends the header this way!
  3254. The third added since some servers use that!
  3255. The fourth means the requested range was unsatisfied.
  3256. */
  3257. char *ptr = headp + strlen("Content-Range:");
  3258. /* Move forward until first digit or asterisk */
  3259. while(*ptr && !ISDIGIT(*ptr) && *ptr != '*')
  3260. ptr++;
  3261. /* if it truly stopped on a digit */
  3262. if(ISDIGIT(*ptr)) {
  3263. if(!curlx_strtoofft(ptr, NULL, 10, &k->offset)) {
  3264. if(data->state.resume_from == k->offset)
  3265. /* we asked for a resume and we got it */
  3266. k->content_range = TRUE;
  3267. }
  3268. }
  3269. else
  3270. data->state.resume_from = 0; /* get everything */
  3271. }
  3272. #if !defined(CURL_DISABLE_COOKIES)
  3273. else if(data->cookies && data->state.cookie_engine &&
  3274. checkprefix("Set-Cookie:", headp)) {
  3275. /* If there is a custom-set Host: name, use it here, or else use real peer
  3276. host name. */
  3277. const char *host = data->state.aptr.cookiehost?
  3278. data->state.aptr.cookiehost:conn->host.name;
  3279. const bool secure_context =
  3280. conn->handler->protocol&(CURLPROTO_HTTPS|CURLPROTO_WSS) ||
  3281. strcasecompare("localhost", host) ||
  3282. !strcmp(host, "127.0.0.1") ||
  3283. !strcmp(host, "::1") ? TRUE : FALSE;
  3284. Curl_share_lock(data, CURL_LOCK_DATA_COOKIE,
  3285. CURL_LOCK_ACCESS_SINGLE);
  3286. Curl_cookie_add(data, data->cookies, TRUE, FALSE,
  3287. headp + strlen("Set-Cookie:"), host,
  3288. data->state.up.path, secure_context);
  3289. Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
  3290. }
  3291. #endif
  3292. else if(!k->http_bodyless && checkprefix("Last-Modified:", headp) &&
  3293. (data->set.timecondition || data->set.get_filetime) ) {
  3294. k->timeofdoc = Curl_getdate_capped(headp + strlen("Last-Modified:"));
  3295. if(data->set.get_filetime)
  3296. data->info.filetime = k->timeofdoc;
  3297. }
  3298. else if((checkprefix("WWW-Authenticate:", headp) &&
  3299. (401 == k->httpcode)) ||
  3300. (checkprefix("Proxy-authenticate:", headp) &&
  3301. (407 == k->httpcode))) {
  3302. bool proxy = (k->httpcode == 407) ? TRUE : FALSE;
  3303. char *auth = Curl_copy_header_value(headp);
  3304. if(!auth)
  3305. return CURLE_OUT_OF_MEMORY;
  3306. result = Curl_http_input_auth(data, proxy, auth);
  3307. free(auth);
  3308. if(result)
  3309. return result;
  3310. }
  3311. #ifdef USE_SPNEGO
  3312. else if(checkprefix("Persistent-Auth:", headp)) {
  3313. struct negotiatedata *negdata = &conn->negotiate;
  3314. struct auth *authp = &data->state.authhost;
  3315. if(authp->picked == CURLAUTH_NEGOTIATE) {
  3316. char *persistentauth = Curl_copy_header_value(headp);
  3317. if(!persistentauth)
  3318. return CURLE_OUT_OF_MEMORY;
  3319. negdata->noauthpersist = checkprefix("false", persistentauth)?
  3320. TRUE:FALSE;
  3321. negdata->havenoauthpersist = TRUE;
  3322. infof(data, "Negotiate: noauthpersist -> %d, header part: %s",
  3323. negdata->noauthpersist, persistentauth);
  3324. free(persistentauth);
  3325. }
  3326. }
  3327. #endif
  3328. else if((k->httpcode >= 300 && k->httpcode < 400) &&
  3329. checkprefix("Location:", headp) &&
  3330. !data->req.location) {
  3331. /* this is the URL that the server advises us to use instead */
  3332. char *location = Curl_copy_header_value(headp);
  3333. if(!location)
  3334. return CURLE_OUT_OF_MEMORY;
  3335. if(!*location)
  3336. /* ignore empty data */
  3337. free(location);
  3338. else {
  3339. data->req.location = location;
  3340. if(data->set.http_follow_location) {
  3341. DEBUGASSERT(!data->req.newurl);
  3342. data->req.newurl = strdup(data->req.location); /* clone */
  3343. if(!data->req.newurl)
  3344. return CURLE_OUT_OF_MEMORY;
  3345. /* some cases of POST and PUT etc needs to rewind the data
  3346. stream at this point */
  3347. result = http_perhapsrewind(data, conn);
  3348. if(result)
  3349. return result;
  3350. /* mark the next request as a followed location: */
  3351. data->state.this_is_a_follow = TRUE;
  3352. }
  3353. }
  3354. }
  3355. #ifndef CURL_DISABLE_HSTS
  3356. /* If enabled, the header is incoming and this is over HTTPS */
  3357. else if(data->hsts && checkprefix("Strict-Transport-Security:", headp) &&
  3358. ((conn->handler->flags & PROTOPT_SSL) ||
  3359. #ifdef CURLDEBUG
  3360. /* allow debug builds to circumvent the HTTPS restriction */
  3361. getenv("CURL_HSTS_HTTP")
  3362. #else
  3363. 0
  3364. #endif
  3365. )) {
  3366. CURLcode check =
  3367. Curl_hsts_parse(data->hsts, conn->host.name,
  3368. headp + strlen("Strict-Transport-Security:"));
  3369. if(check)
  3370. infof(data, "Illegal STS header skipped");
  3371. #ifdef DEBUGBUILD
  3372. else
  3373. infof(data, "Parsed STS header fine (%zu entries)",
  3374. data->hsts->list.size);
  3375. #endif
  3376. }
  3377. #endif
  3378. #ifndef CURL_DISABLE_ALTSVC
  3379. /* If enabled, the header is incoming and this is over HTTPS */
  3380. else if(data->asi && checkprefix("Alt-Svc:", headp) &&
  3381. ((conn->handler->flags & PROTOPT_SSL) ||
  3382. #ifdef CURLDEBUG
  3383. /* allow debug builds to circumvent the HTTPS restriction */
  3384. getenv("CURL_ALTSVC_HTTP")
  3385. #else
  3386. 0
  3387. #endif
  3388. )) {
  3389. /* the ALPN of the current request */
  3390. enum alpnid id = (conn->httpversion == 30)? ALPN_h3 :
  3391. (conn->httpversion == 20) ? ALPN_h2 : ALPN_h1;
  3392. result = Curl_altsvc_parse(data, data->asi,
  3393. headp + strlen("Alt-Svc:"),
  3394. id, conn->host.name,
  3395. curlx_uitous((unsigned int)conn->remote_port));
  3396. if(result)
  3397. return result;
  3398. }
  3399. #endif
  3400. else if(conn->handler->protocol & CURLPROTO_RTSP) {
  3401. result = Curl_rtsp_parseheader(data, headp);
  3402. if(result)
  3403. return result;
  3404. }
  3405. return CURLE_OK;
  3406. }
  3407. /*
  3408. * Called after the first HTTP response line (the status line) has been
  3409. * received and parsed.
  3410. */
  3411. CURLcode Curl_http_statusline(struct Curl_easy *data,
  3412. struct connectdata *conn)
  3413. {
  3414. struct SingleRequest *k = &data->req;
  3415. data->info.httpcode = k->httpcode;
  3416. data->info.httpversion = conn->httpversion;
  3417. if(!data->state.httpversion ||
  3418. data->state.httpversion > conn->httpversion)
  3419. /* store the lowest server version we encounter */
  3420. data->state.httpversion = conn->httpversion;
  3421. /*
  3422. * This code executes as part of processing the header. As a
  3423. * result, it's not totally clear how to interpret the
  3424. * response code yet as that depends on what other headers may
  3425. * be present. 401 and 407 may be errors, but may be OK
  3426. * depending on how authentication is working. Other codes
  3427. * are definitely errors, so give up here.
  3428. */
  3429. if(data->state.resume_from && data->state.httpreq == HTTPREQ_GET &&
  3430. k->httpcode == 416) {
  3431. /* "Requested Range Not Satisfiable", just proceed and
  3432. pretend this is no error */
  3433. k->ignorebody = TRUE; /* Avoid appending error msg to good data. */
  3434. }
  3435. if(conn->httpversion == 10) {
  3436. /* Default action for HTTP/1.0 must be to close, unless
  3437. we get one of those fancy headers that tell us the
  3438. server keeps it open for us! */
  3439. infof(data, "HTTP 1.0, assume close after body");
  3440. connclose(conn, "HTTP/1.0 close after body");
  3441. }
  3442. else if(conn->httpversion == 20 ||
  3443. (k->upgr101 == UPGR101_H2 && k->httpcode == 101)) {
  3444. DEBUGF(infof(data, "HTTP/2 found, allow multiplexing"));
  3445. /* HTTP/2 cannot avoid multiplexing since it is a core functionality
  3446. of the protocol */
  3447. conn->bundle->multiuse = BUNDLE_MULTIPLEX;
  3448. }
  3449. else if(conn->httpversion >= 11 &&
  3450. !conn->bits.close) {
  3451. /* If HTTP version is >= 1.1 and connection is persistent */
  3452. DEBUGF(infof(data,
  3453. "HTTP 1.1 or later with persistent connection"));
  3454. }
  3455. k->http_bodyless = k->httpcode >= 100 && k->httpcode < 200;
  3456. switch(k->httpcode) {
  3457. case 304:
  3458. /* (quote from RFC2616, section 10.3.5): The 304 response
  3459. * MUST NOT contain a message-body, and thus is always
  3460. * terminated by the first empty line after the header
  3461. * fields. */
  3462. if(data->set.timecondition)
  3463. data->info.timecond = TRUE;
  3464. /* FALLTHROUGH */
  3465. case 204:
  3466. /* (quote from RFC2616, section 10.2.5): The server has
  3467. * fulfilled the request but does not need to return an
  3468. * entity-body ... The 204 response MUST NOT include a
  3469. * message-body, and thus is always terminated by the first
  3470. * empty line after the header fields. */
  3471. k->size = 0;
  3472. k->maxdownload = 0;
  3473. k->http_bodyless = TRUE;
  3474. break;
  3475. default:
  3476. break;
  3477. }
  3478. return CURLE_OK;
  3479. }
  3480. /* Content-Length must be ignored if any Transfer-Encoding is present in the
  3481. response. Refer to RFC 7230 section 3.3.3 and RFC2616 section 4.4. This is
  3482. figured out here after all headers have been received but before the final
  3483. call to the user's header callback, so that a valid content length can be
  3484. retrieved by the user in the final call. */
  3485. CURLcode Curl_http_size(struct Curl_easy *data)
  3486. {
  3487. struct SingleRequest *k = &data->req;
  3488. if(data->req.ignore_cl || k->chunk) {
  3489. k->size = k->maxdownload = -1;
  3490. }
  3491. else if(k->size != -1) {
  3492. if(data->set.max_filesize &&
  3493. k->size > data->set.max_filesize) {
  3494. failf(data, "Maximum file size exceeded");
  3495. return CURLE_FILESIZE_EXCEEDED;
  3496. }
  3497. Curl_pgrsSetDownloadSize(data, k->size);
  3498. k->maxdownload = k->size;
  3499. }
  3500. return CURLE_OK;
  3501. }
  3502. static CURLcode verify_header(struct Curl_easy *data)
  3503. {
  3504. struct SingleRequest *k = &data->req;
  3505. const char *header = Curl_dyn_ptr(&data->state.headerb);
  3506. size_t hlen = Curl_dyn_len(&data->state.headerb);
  3507. char *ptr = memchr(header, 0x00, hlen);
  3508. if(ptr) {
  3509. /* this is bad, bail out */
  3510. failf(data, "Nul byte in header");
  3511. return CURLE_WEIRD_SERVER_REPLY;
  3512. }
  3513. if(k->headerline < 2)
  3514. /* the first "header" is the status-line and it has no colon */
  3515. return CURLE_OK;
  3516. if(((header[0] == ' ') || (header[0] == '\t')) && k->headerline > 2)
  3517. /* line folding, can't happen on line 2 */
  3518. ;
  3519. else {
  3520. ptr = memchr(header, ':', hlen);
  3521. if(!ptr) {
  3522. /* this is bad, bail out */
  3523. failf(data, "Header without colon");
  3524. return CURLE_WEIRD_SERVER_REPLY;
  3525. }
  3526. }
  3527. return CURLE_OK;
  3528. }
  3529. /*
  3530. * Read any HTTP header lines from the server and pass them to the client app.
  3531. */
  3532. CURLcode Curl_http_readwrite_headers(struct Curl_easy *data,
  3533. struct connectdata *conn,
  3534. ssize_t *nread,
  3535. bool *stop_reading)
  3536. {
  3537. CURLcode result;
  3538. struct SingleRequest *k = &data->req;
  3539. ssize_t onread = *nread;
  3540. char *ostr = k->str;
  3541. char *headp;
  3542. char *str_start;
  3543. char *end_ptr;
  3544. /* header line within buffer loop */
  3545. do {
  3546. size_t rest_length;
  3547. size_t full_length;
  3548. int writetype;
  3549. /* str_start is start of line within buf */
  3550. str_start = k->str;
  3551. /* data is in network encoding so use 0x0a instead of '\n' */
  3552. end_ptr = memchr(str_start, 0x0a, *nread);
  3553. if(!end_ptr) {
  3554. /* Not a complete header line within buffer, append the data to
  3555. the end of the headerbuff. */
  3556. result = Curl_dyn_addn(&data->state.headerb, str_start, *nread);
  3557. if(result)
  3558. return result;
  3559. if(!k->headerline) {
  3560. /* check if this looks like a protocol header */
  3561. statusline st =
  3562. checkprotoprefix(data, conn,
  3563. Curl_dyn_ptr(&data->state.headerb),
  3564. Curl_dyn_len(&data->state.headerb));
  3565. if(st == STATUS_BAD) {
  3566. /* this is not the beginning of a protocol first header line */
  3567. k->header = FALSE;
  3568. k->badheader = HEADER_ALLBAD;
  3569. streamclose(conn, "bad HTTP: No end-of-message indicator");
  3570. if(!data->set.http09_allowed) {
  3571. failf(data, "Received HTTP/0.9 when not allowed");
  3572. return CURLE_UNSUPPORTED_PROTOCOL;
  3573. }
  3574. break;
  3575. }
  3576. }
  3577. break; /* read more and try again */
  3578. }
  3579. /* decrease the size of the remaining (supposed) header line */
  3580. rest_length = (end_ptr - k->str) + 1;
  3581. *nread -= (ssize_t)rest_length;
  3582. k->str = end_ptr + 1; /* move past new line */
  3583. full_length = k->str - str_start;
  3584. result = Curl_dyn_addn(&data->state.headerb, str_start, full_length);
  3585. if(result)
  3586. return result;
  3587. /****
  3588. * We now have a FULL header line in 'headerb'.
  3589. *****/
  3590. if(!k->headerline) {
  3591. /* the first read header */
  3592. statusline st = checkprotoprefix(data, conn,
  3593. Curl_dyn_ptr(&data->state.headerb),
  3594. Curl_dyn_len(&data->state.headerb));
  3595. if(st == STATUS_BAD) {
  3596. streamclose(conn, "bad HTTP: No end-of-message indicator");
  3597. /* this is not the beginning of a protocol first header line */
  3598. if(!data->set.http09_allowed) {
  3599. failf(data, "Received HTTP/0.9 when not allowed");
  3600. return CURLE_UNSUPPORTED_PROTOCOL;
  3601. }
  3602. k->header = FALSE;
  3603. if(*nread)
  3604. /* since there's more, this is a partial bad header */
  3605. k->badheader = HEADER_PARTHEADER;
  3606. else {
  3607. /* this was all we read so it's all a bad header */
  3608. k->badheader = HEADER_ALLBAD;
  3609. *nread = onread;
  3610. k->str = ostr;
  3611. return CURLE_OK;
  3612. }
  3613. break;
  3614. }
  3615. }
  3616. /* headers are in network encoding so use 0x0a and 0x0d instead of '\n'
  3617. and '\r' */
  3618. headp = Curl_dyn_ptr(&data->state.headerb);
  3619. if((0x0a == *headp) || (0x0d == *headp)) {
  3620. size_t headerlen;
  3621. /* Zero-length header line means end of headers! */
  3622. if('\r' == *headp)
  3623. headp++; /* pass the \r byte */
  3624. if('\n' == *headp)
  3625. headp++; /* pass the \n byte */
  3626. if(100 <= k->httpcode && 199 >= k->httpcode) {
  3627. /* "A user agent MAY ignore unexpected 1xx status responses." */
  3628. switch(k->httpcode) {
  3629. case 100:
  3630. /*
  3631. * We have made an HTTP PUT or POST and this is 1.1-lingo
  3632. * that tells us that the server is OK with this and ready
  3633. * to receive the data.
  3634. * However, we'll get more headers now so we must get
  3635. * back into the header-parsing state!
  3636. */
  3637. k->header = TRUE;
  3638. k->headerline = 0; /* restart the header line counter */
  3639. /* if we did wait for this do enable write now! */
  3640. if(k->exp100 > EXP100_SEND_DATA) {
  3641. k->exp100 = EXP100_SEND_DATA;
  3642. k->keepon |= KEEP_SEND;
  3643. Curl_expire_done(data, EXPIRE_100_TIMEOUT);
  3644. }
  3645. break;
  3646. case 101:
  3647. /* Switching Protocols */
  3648. if(k->upgr101 == UPGR101_H2) {
  3649. /* Switching to HTTP/2 */
  3650. DEBUGASSERT(conn->httpversion < 20);
  3651. infof(data, "Received 101, Switching to HTTP/2");
  3652. k->upgr101 = UPGR101_RECEIVED;
  3653. /* we'll get more headers (HTTP/2 response) */
  3654. k->header = TRUE;
  3655. k->headerline = 0; /* restart the header line counter */
  3656. /* switch to http2 now. The bytes after response headers
  3657. are also processed here, otherwise they are lost. */
  3658. result = Curl_http2_upgrade(data, conn, FIRSTSOCKET,
  3659. k->str, *nread);
  3660. if(result)
  3661. return result;
  3662. *nread = 0;
  3663. }
  3664. #ifdef USE_WEBSOCKETS
  3665. else if(k->upgr101 == UPGR101_WS) {
  3666. /* verify the response */
  3667. result = Curl_ws_accept(data, k->str, *nread);
  3668. if(result)
  3669. return result;
  3670. k->header = FALSE; /* no more header to parse! */
  3671. if(data->set.connect_only) {
  3672. k->keepon &= ~KEEP_RECV; /* read no more content */
  3673. *nread = 0;
  3674. }
  3675. }
  3676. #endif
  3677. else {
  3678. /* Not switching to another protocol */
  3679. k->header = FALSE; /* no more header to parse! */
  3680. }
  3681. break;
  3682. default:
  3683. /* the status code 1xx indicates a provisional response, so
  3684. we'll get another set of headers */
  3685. k->header = TRUE;
  3686. k->headerline = 0; /* restart the header line counter */
  3687. break;
  3688. }
  3689. }
  3690. else {
  3691. if(k->upgr101 == UPGR101_H2) {
  3692. /* A requested upgrade was denied, poke the multi handle to possibly
  3693. allow a pending pipewait to continue */
  3694. Curl_multi_connchanged(data->multi);
  3695. }
  3696. k->header = FALSE; /* no more header to parse! */
  3697. if((k->size == -1) && !k->chunk && !conn->bits.close &&
  3698. (conn->httpversion == 11) &&
  3699. !(conn->handler->protocol & CURLPROTO_RTSP) &&
  3700. data->state.httpreq != HTTPREQ_HEAD) {
  3701. /* On HTTP 1.1, when connection is not to get closed, but no
  3702. Content-Length nor Transfer-Encoding chunked have been
  3703. received, according to RFC2616 section 4.4 point 5, we
  3704. assume that the server will close the connection to
  3705. signal the end of the document. */
  3706. infof(data, "no chunk, no close, no size. Assume close to "
  3707. "signal end");
  3708. streamclose(conn, "HTTP: No end-of-message indicator");
  3709. }
  3710. }
  3711. if(!k->header) {
  3712. result = Curl_http_size(data);
  3713. if(result)
  3714. return result;
  3715. }
  3716. /* At this point we have some idea about the fate of the connection.
  3717. If we are closing the connection it may result auth failure. */
  3718. #if defined(USE_NTLM)
  3719. if(conn->bits.close &&
  3720. (((data->req.httpcode == 401) &&
  3721. (conn->http_ntlm_state == NTLMSTATE_TYPE2)) ||
  3722. ((data->req.httpcode == 407) &&
  3723. (conn->proxy_ntlm_state == NTLMSTATE_TYPE2)))) {
  3724. infof(data, "Connection closure while negotiating auth (HTTP 1.0?)");
  3725. data->state.authproblem = TRUE;
  3726. }
  3727. #endif
  3728. #if defined(USE_SPNEGO)
  3729. if(conn->bits.close &&
  3730. (((data->req.httpcode == 401) &&
  3731. (conn->http_negotiate_state == GSS_AUTHRECV)) ||
  3732. ((data->req.httpcode == 407) &&
  3733. (conn->proxy_negotiate_state == GSS_AUTHRECV)))) {
  3734. infof(data, "Connection closure while negotiating auth (HTTP 1.0?)");
  3735. data->state.authproblem = TRUE;
  3736. }
  3737. if((conn->http_negotiate_state == GSS_AUTHDONE) &&
  3738. (data->req.httpcode != 401)) {
  3739. conn->http_negotiate_state = GSS_AUTHSUCC;
  3740. }
  3741. if((conn->proxy_negotiate_state == GSS_AUTHDONE) &&
  3742. (data->req.httpcode != 407)) {
  3743. conn->proxy_negotiate_state = GSS_AUTHSUCC;
  3744. }
  3745. #endif
  3746. /* now, only output this if the header AND body are requested:
  3747. */
  3748. writetype = CLIENTWRITE_HEADER |
  3749. (data->set.include_header ? CLIENTWRITE_BODY : 0) |
  3750. ((k->httpcode/100 == 1) ? CLIENTWRITE_1XX : 0);
  3751. headerlen = Curl_dyn_len(&data->state.headerb);
  3752. result = Curl_client_write(data, writetype,
  3753. Curl_dyn_ptr(&data->state.headerb),
  3754. headerlen);
  3755. if(result)
  3756. return result;
  3757. data->info.header_size += (long)headerlen;
  3758. data->req.headerbytecount += (long)headerlen;
  3759. /*
  3760. * When all the headers have been parsed, see if we should give
  3761. * up and return an error.
  3762. */
  3763. if(http_should_fail(data)) {
  3764. failf(data, "The requested URL returned error: %d",
  3765. k->httpcode);
  3766. return CURLE_HTTP_RETURNED_ERROR;
  3767. }
  3768. #ifdef USE_WEBSOCKETS
  3769. /* All non-101 HTTP status codes are bad when wanting to upgrade to
  3770. websockets */
  3771. if(data->req.upgr101 == UPGR101_WS) {
  3772. failf(data, "Refused WebSockets upgrade: %d", k->httpcode);
  3773. return CURLE_HTTP_RETURNED_ERROR;
  3774. }
  3775. #endif
  3776. data->req.deductheadercount =
  3777. (100 <= k->httpcode && 199 >= k->httpcode)?data->req.headerbytecount:0;
  3778. /* Curl_http_auth_act() checks what authentication methods
  3779. * that are available and decides which one (if any) to
  3780. * use. It will set 'newurl' if an auth method was picked. */
  3781. result = Curl_http_auth_act(data);
  3782. if(result)
  3783. return result;
  3784. if(k->httpcode >= 300) {
  3785. if((!conn->bits.authneg) && !conn->bits.close &&
  3786. !data->state.rewindbeforesend) {
  3787. /*
  3788. * General treatment of errors when about to send data. Including :
  3789. * "417 Expectation Failed", while waiting for 100-continue.
  3790. *
  3791. * The check for close above is done simply because of something
  3792. * else has already deemed the connection to get closed then
  3793. * something else should've considered the big picture and we
  3794. * avoid this check.
  3795. *
  3796. * rewindbeforesend indicates that something has told libcurl to
  3797. * continue sending even if it gets discarded
  3798. */
  3799. switch(data->state.httpreq) {
  3800. case HTTPREQ_PUT:
  3801. case HTTPREQ_POST:
  3802. case HTTPREQ_POST_FORM:
  3803. case HTTPREQ_POST_MIME:
  3804. /* We got an error response. If this happened before the whole
  3805. * request body has been sent we stop sending and mark the
  3806. * connection for closure after we've read the entire response.
  3807. */
  3808. Curl_expire_done(data, EXPIRE_100_TIMEOUT);
  3809. if(!k->upload_done) {
  3810. if((k->httpcode == 417) && data->state.expect100header) {
  3811. /* 417 Expectation Failed - try again without the Expect
  3812. header */
  3813. infof(data, "Got 417 while waiting for a 100");
  3814. data->state.disableexpect = TRUE;
  3815. DEBUGASSERT(!data->req.newurl);
  3816. data->req.newurl = strdup(data->state.url);
  3817. Curl_done_sending(data, k);
  3818. }
  3819. else if(data->set.http_keep_sending_on_error) {
  3820. infof(data, "HTTP error before end of send, keep sending");
  3821. if(k->exp100 > EXP100_SEND_DATA) {
  3822. k->exp100 = EXP100_SEND_DATA;
  3823. k->keepon |= KEEP_SEND;
  3824. }
  3825. }
  3826. else {
  3827. infof(data, "HTTP error before end of send, stop sending");
  3828. streamclose(conn, "Stop sending data before everything sent");
  3829. result = Curl_done_sending(data, k);
  3830. if(result)
  3831. return result;
  3832. k->upload_done = TRUE;
  3833. if(data->state.expect100header)
  3834. k->exp100 = EXP100_FAILED;
  3835. }
  3836. }
  3837. break;
  3838. default: /* default label present to avoid compiler warnings */
  3839. break;
  3840. }
  3841. }
  3842. if(data->state.rewindbeforesend &&
  3843. (conn->writesockfd != CURL_SOCKET_BAD)) {
  3844. /* We rewind before next send, continue sending now */
  3845. infof(data, "Keep sending data to get tossed away");
  3846. k->keepon |= KEEP_SEND;
  3847. }
  3848. }
  3849. if(!k->header) {
  3850. /*
  3851. * really end-of-headers.
  3852. *
  3853. * If we requested a "no body", this is a good time to get
  3854. * out and return home.
  3855. */
  3856. if(data->req.no_body)
  3857. *stop_reading = TRUE;
  3858. #ifndef CURL_DISABLE_RTSP
  3859. else if((conn->handler->protocol & CURLPROTO_RTSP) &&
  3860. (data->set.rtspreq == RTSPREQ_DESCRIBE) &&
  3861. (k->size <= -1))
  3862. /* Respect section 4.4 of rfc2326: If the Content-Length header is
  3863. absent, a length 0 must be assumed. It will prevent libcurl from
  3864. hanging on DESCRIBE request that got refused for whatever
  3865. reason */
  3866. *stop_reading = TRUE;
  3867. #endif
  3868. /* If max download size is *zero* (nothing) we already have
  3869. nothing and can safely return ok now! But for HTTP/2, we'd
  3870. like to call http2_handle_stream_close to properly close a
  3871. stream. In order to do this, we keep reading until we
  3872. close the stream. */
  3873. if(0 == k->maxdownload
  3874. && !Curl_conn_is_http2(data, conn, FIRSTSOCKET)
  3875. && !Curl_conn_is_http3(data, conn, FIRSTSOCKET))
  3876. *stop_reading = TRUE;
  3877. if(*stop_reading) {
  3878. /* we make sure that this socket isn't read more now */
  3879. k->keepon &= ~KEEP_RECV;
  3880. }
  3881. Curl_debug(data, CURLINFO_HEADER_IN, str_start, headerlen);
  3882. break; /* exit header line loop */
  3883. }
  3884. /* We continue reading headers, reset the line-based header */
  3885. Curl_dyn_reset(&data->state.headerb);
  3886. continue;
  3887. }
  3888. /*
  3889. * Checks for special headers coming up.
  3890. */
  3891. writetype = CLIENTWRITE_HEADER;
  3892. if(!k->headerline++) {
  3893. /* This is the first header, it MUST be the error code line
  3894. or else we consider this to be the body right away! */
  3895. bool fine_statusline = FALSE;
  3896. if(conn->handler->protocol & PROTO_FAMILY_HTTP) {
  3897. /*
  3898. * https://datatracker.ietf.org/doc/html/rfc7230#section-3.1.2
  3899. *
  3900. * The response code is always a three-digit number in HTTP as the spec
  3901. * says. We allow any three-digit number here, but we cannot make
  3902. * guarantees on future behaviors since it isn't within the protocol.
  3903. */
  3904. int httpversion = 0;
  3905. char *p = headp;
  3906. while(*p && ISBLANK(*p))
  3907. p++;
  3908. if(!strncmp(p, "HTTP/", 5)) {
  3909. p += 5;
  3910. switch(*p) {
  3911. case '1':
  3912. p++;
  3913. if((p[0] == '.') && (p[1] == '0' || p[1] == '1')) {
  3914. if(ISBLANK(p[2])) {
  3915. httpversion = 10 + (p[1] - '0');
  3916. p += 3;
  3917. if(ISDIGIT(p[0]) && ISDIGIT(p[1]) && ISDIGIT(p[2])) {
  3918. k->httpcode = (p[0] - '0') * 100 + (p[1] - '0') * 10 +
  3919. (p[2] - '0');
  3920. p += 3;
  3921. if(ISSPACE(*p))
  3922. fine_statusline = TRUE;
  3923. }
  3924. }
  3925. }
  3926. if(!fine_statusline) {
  3927. failf(data, "Unsupported HTTP/1 subversion in response");
  3928. return CURLE_UNSUPPORTED_PROTOCOL;
  3929. }
  3930. break;
  3931. case '2':
  3932. case '3':
  3933. if(!ISBLANK(p[1]))
  3934. break;
  3935. httpversion = (*p - '0') * 10;
  3936. p += 2;
  3937. if(ISDIGIT(p[0]) && ISDIGIT(p[1]) && ISDIGIT(p[2])) {
  3938. k->httpcode = (p[0] - '0') * 100 + (p[1] - '0') * 10 +
  3939. (p[2] - '0');
  3940. p += 3;
  3941. if(!ISSPACE(*p))
  3942. break;
  3943. fine_statusline = TRUE;
  3944. }
  3945. break;
  3946. default: /* unsupported */
  3947. failf(data, "Unsupported HTTP version in response");
  3948. return CURLE_UNSUPPORTED_PROTOCOL;
  3949. }
  3950. }
  3951. if(fine_statusline) {
  3952. if(k->httpcode < 100) {
  3953. failf(data, "Unsupported response code in HTTP response");
  3954. return CURLE_UNSUPPORTED_PROTOCOL;
  3955. }
  3956. switch(httpversion) {
  3957. case 10:
  3958. case 11:
  3959. #ifdef USE_HTTP2
  3960. case 20:
  3961. #endif
  3962. #ifdef ENABLE_QUIC
  3963. case 30:
  3964. #endif
  3965. conn->httpversion = (unsigned char)httpversion;
  3966. break;
  3967. default:
  3968. failf(data, "Unsupported HTTP version (%u.%d) in response",
  3969. httpversion/10, httpversion%10);
  3970. return CURLE_UNSUPPORTED_PROTOCOL;
  3971. }
  3972. if(k->upgr101 == UPGR101_RECEIVED) {
  3973. /* supposedly upgraded to http2 now */
  3974. if(conn->httpversion != 20)
  3975. infof(data, "Lying server, not serving HTTP/2");
  3976. }
  3977. if(conn->httpversion < 20) {
  3978. conn->bundle->multiuse = BUNDLE_NO_MULTIUSE;
  3979. }
  3980. }
  3981. else {
  3982. /* If user has set option HTTP200ALIASES,
  3983. compare header line against list of aliases
  3984. */
  3985. statusline check =
  3986. checkhttpprefix(data,
  3987. Curl_dyn_ptr(&data->state.headerb),
  3988. Curl_dyn_len(&data->state.headerb));
  3989. if(check == STATUS_DONE) {
  3990. fine_statusline = TRUE;
  3991. k->httpcode = 200;
  3992. conn->httpversion = 10;
  3993. }
  3994. }
  3995. }
  3996. else if(conn->handler->protocol & CURLPROTO_RTSP) {
  3997. char *p = headp;
  3998. while(*p && ISBLANK(*p))
  3999. p++;
  4000. if(!strncmp(p, "RTSP/", 5)) {
  4001. p += 5;
  4002. if(ISDIGIT(*p)) {
  4003. p++;
  4004. if((p[0] == '.') && ISDIGIT(p[1])) {
  4005. if(ISBLANK(p[2])) {
  4006. p += 3;
  4007. if(ISDIGIT(p[0]) && ISDIGIT(p[1]) && ISDIGIT(p[2])) {
  4008. k->httpcode = (p[0] - '0') * 100 + (p[1] - '0') * 10 +
  4009. (p[2] - '0');
  4010. p += 3;
  4011. if(ISSPACE(*p)) {
  4012. fine_statusline = TRUE;
  4013. conn->httpversion = 11; /* RTSP acts like HTTP 1.1 */
  4014. }
  4015. }
  4016. }
  4017. }
  4018. }
  4019. if(!fine_statusline)
  4020. return CURLE_WEIRD_SERVER_REPLY;
  4021. }
  4022. }
  4023. if(fine_statusline) {
  4024. result = Curl_http_statusline(data, conn);
  4025. if(result)
  4026. return result;
  4027. writetype |= CLIENTWRITE_STATUS;
  4028. }
  4029. else {
  4030. k->header = FALSE; /* this is not a header line */
  4031. break;
  4032. }
  4033. }
  4034. result = verify_header(data);
  4035. if(result)
  4036. return result;
  4037. result = Curl_http_header(data, conn, headp);
  4038. if(result)
  4039. return result;
  4040. /*
  4041. * End of header-checks. Write them to the client.
  4042. */
  4043. if(data->set.include_header)
  4044. writetype |= CLIENTWRITE_BODY;
  4045. if(k->httpcode/100 == 1)
  4046. writetype |= CLIENTWRITE_1XX;
  4047. Curl_debug(data, CURLINFO_HEADER_IN, headp,
  4048. Curl_dyn_len(&data->state.headerb));
  4049. result = Curl_client_write(data, writetype, headp,
  4050. Curl_dyn_len(&data->state.headerb));
  4051. if(result)
  4052. return result;
  4053. data->info.header_size += Curl_dyn_len(&data->state.headerb);
  4054. data->req.headerbytecount += Curl_dyn_len(&data->state.headerb);
  4055. Curl_dyn_reset(&data->state.headerb);
  4056. }
  4057. while(*k->str); /* header line within buffer */
  4058. /* We might have reached the end of the header part here, but
  4059. there might be a non-header part left in the end of the read
  4060. buffer. */
  4061. return CURLE_OK;
  4062. }
  4063. /* Decode HTTP status code string. */
  4064. CURLcode Curl_http_decode_status(int *pstatus, const char *s, size_t len)
  4065. {
  4066. CURLcode result = CURLE_BAD_FUNCTION_ARGUMENT;
  4067. int status = 0;
  4068. int i;
  4069. if(len != 3)
  4070. goto out;
  4071. for(i = 0; i < 3; ++i) {
  4072. char c = s[i];
  4073. if(c < '0' || c > '9')
  4074. goto out;
  4075. status *= 10;
  4076. status += c - '0';
  4077. }
  4078. result = CURLE_OK;
  4079. out:
  4080. *pstatus = result? -1 : status;
  4081. return result;
  4082. }
  4083. /* simple implementation of strndup(), which isn't portable */
  4084. static char *my_strndup(const char *ptr, size_t len)
  4085. {
  4086. char *copy = malloc(len + 1);
  4087. if(!copy)
  4088. return NULL;
  4089. memcpy(copy, ptr, len);
  4090. copy[len] = '\0';
  4091. return copy;
  4092. }
  4093. CURLcode Curl_http_req_make(struct httpreq **preq,
  4094. const char *method, size_t m_len,
  4095. const char *scheme, size_t s_len,
  4096. const char *authority, size_t a_len,
  4097. const char *path, size_t p_len)
  4098. {
  4099. struct httpreq *req;
  4100. CURLcode result = CURLE_OUT_OF_MEMORY;
  4101. DEBUGASSERT(method);
  4102. if(m_len + 1 >= sizeof(req->method))
  4103. return CURLE_BAD_FUNCTION_ARGUMENT;
  4104. req = calloc(1, sizeof(*req));
  4105. if(!req)
  4106. goto out;
  4107. memcpy(req->method, method, m_len);
  4108. if(scheme) {
  4109. req->scheme = my_strndup(scheme, s_len);
  4110. if(!req->scheme)
  4111. goto out;
  4112. }
  4113. if(authority) {
  4114. req->authority = my_strndup(authority, a_len);
  4115. if(!req->authority)
  4116. goto out;
  4117. }
  4118. if(path) {
  4119. req->path = my_strndup(path, p_len);
  4120. if(!req->path)
  4121. goto out;
  4122. }
  4123. Curl_dynhds_init(&req->headers, 0, DYN_HTTP_REQUEST);
  4124. Curl_dynhds_init(&req->trailers, 0, DYN_HTTP_REQUEST);
  4125. result = CURLE_OK;
  4126. out:
  4127. if(result && req)
  4128. Curl_http_req_free(req);
  4129. *preq = result? NULL : req;
  4130. return result;
  4131. }
  4132. static CURLcode req_assign_url_authority(struct httpreq *req, CURLU *url)
  4133. {
  4134. char *user, *pass, *host, *port;
  4135. struct dynbuf buf;
  4136. CURLUcode uc;
  4137. CURLcode result = CURLE_URL_MALFORMAT;
  4138. user = pass = host = port = NULL;
  4139. Curl_dyn_init(&buf, DYN_HTTP_REQUEST);
  4140. uc = curl_url_get(url, CURLUPART_HOST, &host, 0);
  4141. if(uc && uc != CURLUE_NO_HOST)
  4142. goto out;
  4143. if(!host) {
  4144. req->authority = NULL;
  4145. result = CURLE_OK;
  4146. goto out;
  4147. }
  4148. uc = curl_url_get(url, CURLUPART_PORT, &port, CURLU_NO_DEFAULT_PORT);
  4149. if(uc && uc != CURLUE_NO_PORT)
  4150. goto out;
  4151. uc = curl_url_get(url, CURLUPART_USER, &user, 0);
  4152. if(uc && uc != CURLUE_NO_USER)
  4153. goto out;
  4154. if(user) {
  4155. uc = curl_url_get(url, CURLUPART_PASSWORD, &pass, 0);
  4156. if(uc && uc != CURLUE_NO_PASSWORD)
  4157. goto out;
  4158. }
  4159. if(user) {
  4160. result = Curl_dyn_add(&buf, user);
  4161. if(result)
  4162. goto out;
  4163. if(pass) {
  4164. result = Curl_dyn_addf(&buf, ":%s", pass);
  4165. if(result)
  4166. goto out;
  4167. }
  4168. result = Curl_dyn_add(&buf, "@");
  4169. if(result)
  4170. goto out;
  4171. }
  4172. result = Curl_dyn_add(&buf, host);
  4173. if(result)
  4174. goto out;
  4175. if(port) {
  4176. result = Curl_dyn_addf(&buf, ":%s", port);
  4177. if(result)
  4178. goto out;
  4179. }
  4180. req->authority = strdup(Curl_dyn_ptr(&buf));
  4181. if(!req->authority)
  4182. goto out;
  4183. result = CURLE_OK;
  4184. out:
  4185. free(user);
  4186. free(pass);
  4187. free(host);
  4188. free(port);
  4189. Curl_dyn_free(&buf);
  4190. return result;
  4191. }
  4192. static CURLcode req_assign_url_path(struct httpreq *req, CURLU *url)
  4193. {
  4194. char *path, *query;
  4195. struct dynbuf buf;
  4196. CURLUcode uc;
  4197. CURLcode result = CURLE_URL_MALFORMAT;
  4198. path = query = NULL;
  4199. Curl_dyn_init(&buf, DYN_HTTP_REQUEST);
  4200. uc = curl_url_get(url, CURLUPART_PATH, &path, CURLU_PATH_AS_IS);
  4201. if(uc)
  4202. goto out;
  4203. uc = curl_url_get(url, CURLUPART_QUERY, &query, 0);
  4204. if(uc && uc != CURLUE_NO_QUERY)
  4205. goto out;
  4206. if(!path && !query) {
  4207. req->path = NULL;
  4208. }
  4209. else if(path && !query) {
  4210. req->path = path;
  4211. path = NULL;
  4212. }
  4213. else {
  4214. if(path) {
  4215. result = Curl_dyn_add(&buf, path);
  4216. if(result)
  4217. goto out;
  4218. }
  4219. if(query) {
  4220. result = Curl_dyn_addf(&buf, "?%s", query);
  4221. if(result)
  4222. goto out;
  4223. }
  4224. req->path = strdup(Curl_dyn_ptr(&buf));
  4225. if(!req->path)
  4226. goto out;
  4227. }
  4228. result = CURLE_OK;
  4229. out:
  4230. free(path);
  4231. free(query);
  4232. Curl_dyn_free(&buf);
  4233. return result;
  4234. }
  4235. CURLcode Curl_http_req_make2(struct httpreq **preq,
  4236. const char *method, size_t m_len,
  4237. CURLU *url, const char *scheme_default)
  4238. {
  4239. struct httpreq *req;
  4240. CURLcode result = CURLE_OUT_OF_MEMORY;
  4241. CURLUcode uc;
  4242. DEBUGASSERT(method);
  4243. if(m_len + 1 >= sizeof(req->method))
  4244. return CURLE_BAD_FUNCTION_ARGUMENT;
  4245. req = calloc(1, sizeof(*req));
  4246. if(!req)
  4247. goto out;
  4248. memcpy(req->method, method, m_len);
  4249. uc = curl_url_get(url, CURLUPART_SCHEME, &req->scheme, 0);
  4250. if(uc && uc != CURLUE_NO_SCHEME)
  4251. goto out;
  4252. if(!req->scheme && scheme_default) {
  4253. req->scheme = strdup(scheme_default);
  4254. if(!req->scheme)
  4255. goto out;
  4256. }
  4257. result = req_assign_url_authority(req, url);
  4258. if(result)
  4259. goto out;
  4260. result = req_assign_url_path(req, url);
  4261. if(result)
  4262. goto out;
  4263. Curl_dynhds_init(&req->headers, 0, DYN_HTTP_REQUEST);
  4264. Curl_dynhds_init(&req->trailers, 0, DYN_HTTP_REQUEST);
  4265. result = CURLE_OK;
  4266. out:
  4267. if(result && req)
  4268. Curl_http_req_free(req);
  4269. *preq = result? NULL : req;
  4270. return result;
  4271. }
  4272. void Curl_http_req_free(struct httpreq *req)
  4273. {
  4274. if(req) {
  4275. free(req->scheme);
  4276. free(req->authority);
  4277. free(req->path);
  4278. Curl_dynhds_free(&req->headers);
  4279. Curl_dynhds_free(&req->trailers);
  4280. free(req);
  4281. }
  4282. }
  4283. struct name_const {
  4284. const char *name;
  4285. size_t namelen;
  4286. };
  4287. static struct name_const H2_NON_FIELD[] = {
  4288. { STRCONST("Host") },
  4289. { STRCONST("Upgrade") },
  4290. { STRCONST("Connection") },
  4291. { STRCONST("Keep-Alive") },
  4292. { STRCONST("Proxy-Connection") },
  4293. { STRCONST("Transfer-Encoding") },
  4294. };
  4295. static bool h2_non_field(const char *name, size_t namelen)
  4296. {
  4297. size_t i;
  4298. for(i = 0; i < sizeof(H2_NON_FIELD)/sizeof(H2_NON_FIELD[0]); ++i) {
  4299. if(namelen < H2_NON_FIELD[i].namelen)
  4300. return FALSE;
  4301. if(namelen == H2_NON_FIELD[i].namelen &&
  4302. strcasecompare(H2_NON_FIELD[i].name, name))
  4303. return TRUE;
  4304. }
  4305. return FALSE;
  4306. }
  4307. CURLcode Curl_http_req_to_h2(struct dynhds *h2_headers,
  4308. struct httpreq *req, struct Curl_easy *data)
  4309. {
  4310. const char *scheme = NULL, *authority = NULL;
  4311. struct dynhds_entry *e;
  4312. size_t i;
  4313. CURLcode result;
  4314. DEBUGASSERT(req);
  4315. DEBUGASSERT(h2_headers);
  4316. if(req->scheme) {
  4317. scheme = req->scheme;
  4318. }
  4319. else if(strcmp("CONNECT", req->method)) {
  4320. scheme = Curl_checkheaders(data, STRCONST(HTTP_PSEUDO_SCHEME));
  4321. if(scheme) {
  4322. scheme += sizeof(HTTP_PSEUDO_SCHEME);
  4323. while(*scheme && ISBLANK(*scheme))
  4324. scheme++;
  4325. infof(data, "set pseudo header %s to %s", HTTP_PSEUDO_SCHEME, scheme);
  4326. }
  4327. else {
  4328. scheme = (data->conn && data->conn->handler->flags & PROTOPT_SSL)?
  4329. "https" : "http";
  4330. }
  4331. }
  4332. if(req->authority) {
  4333. authority = req->authority;
  4334. }
  4335. else {
  4336. e = Curl_dynhds_get(&req->headers, STRCONST("Host"));
  4337. if(e)
  4338. authority = e->value;
  4339. }
  4340. Curl_dynhds_reset(h2_headers);
  4341. Curl_dynhds_set_opts(h2_headers, DYNHDS_OPT_LOWERCASE);
  4342. result = Curl_dynhds_add(h2_headers, STRCONST(HTTP_PSEUDO_METHOD),
  4343. req->method, strlen(req->method));
  4344. if(!result && scheme) {
  4345. result = Curl_dynhds_add(h2_headers, STRCONST(HTTP_PSEUDO_SCHEME),
  4346. scheme, strlen(scheme));
  4347. }
  4348. if(!result && authority) {
  4349. result = Curl_dynhds_add(h2_headers, STRCONST(HTTP_PSEUDO_AUTHORITY),
  4350. authority, strlen(authority));
  4351. }
  4352. if(!result && req->path) {
  4353. result = Curl_dynhds_add(h2_headers, STRCONST(HTTP_PSEUDO_PATH),
  4354. req->path, strlen(req->path));
  4355. }
  4356. for(i = 0; !result && i < Curl_dynhds_count(&req->headers); ++i) {
  4357. e = Curl_dynhds_getn(&req->headers, i);
  4358. if(!h2_non_field(e->name, e->namelen)) {
  4359. result = Curl_dynhds_add(h2_headers, e->name, e->namelen,
  4360. e->value, e->valuelen);
  4361. }
  4362. }
  4363. return result;
  4364. }
  4365. CURLcode Curl_http_resp_make(struct http_resp **presp,
  4366. int status,
  4367. const char *description)
  4368. {
  4369. struct http_resp *resp;
  4370. CURLcode result = CURLE_OUT_OF_MEMORY;
  4371. resp = calloc(1, sizeof(*resp));
  4372. if(!resp)
  4373. goto out;
  4374. resp->status = status;
  4375. if(description) {
  4376. resp->description = strdup(description);
  4377. if(!resp->description)
  4378. goto out;
  4379. }
  4380. Curl_dynhds_init(&resp->headers, 0, DYN_HTTP_REQUEST);
  4381. Curl_dynhds_init(&resp->trailers, 0, DYN_HTTP_REQUEST);
  4382. result = CURLE_OK;
  4383. out:
  4384. if(result && resp)
  4385. Curl_http_resp_free(resp);
  4386. *presp = result? NULL : resp;
  4387. return result;
  4388. }
  4389. void Curl_http_resp_free(struct http_resp *resp)
  4390. {
  4391. if(resp) {
  4392. free(resp->description);
  4393. Curl_dynhds_free(&resp->headers);
  4394. Curl_dynhds_free(&resp->trailers);
  4395. if(resp->prev)
  4396. Curl_http_resp_free(resp->prev);
  4397. free(resp);
  4398. }
  4399. }
  4400. #endif /* CURL_DISABLE_HTTP */