httplib.cpp 343 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540
  1. #include "httplib.h"
  2. namespace httplib {
  3. /*
  4. * Implementation that will be part of the .cc file if split into .h + .cc.
  5. */
  6. namespace detail {
  7. bool is_hex(char c, int &v) {
  8. if (isdigit(c)) {
  9. v = c - '0';
  10. return true;
  11. } else if ('A' <= c && c <= 'F') {
  12. v = c - 'A' + 10;
  13. return true;
  14. } else if ('a' <= c && c <= 'f') {
  15. v = c - 'a' + 10;
  16. return true;
  17. }
  18. return false;
  19. }
  20. bool from_hex_to_i(const std::string &s, size_t i, size_t cnt,
  21. int &val) {
  22. if (i >= s.size()) { return false; }
  23. val = 0;
  24. for (; cnt; i++, cnt--) {
  25. if (!s[i]) { return false; }
  26. auto v = 0;
  27. if (is_hex(s[i], v)) {
  28. val = val * 16 + v;
  29. } else {
  30. return false;
  31. }
  32. }
  33. return true;
  34. }
  35. std::string from_i_to_hex(size_t n) {
  36. static const auto charset = "0123456789abcdef";
  37. std::string ret;
  38. do {
  39. ret = charset[n & 15] + ret;
  40. n >>= 4;
  41. } while (n > 0);
  42. return ret;
  43. }
  44. std::string compute_etag(const FileStat &fs) {
  45. if (!fs.is_file()) { return std::string(); }
  46. // If mtime cannot be determined (negative value indicates an error
  47. // or sentinel), do not generate an ETag. Returning a neutral / fixed
  48. // value like 0 could collide with a real file that legitimately has
  49. // mtime == 0 (epoch) and lead to misleading validators.
  50. auto mtime_raw = fs.mtime();
  51. if (mtime_raw < 0) { return std::string(); }
  52. auto mtime = static_cast<size_t>(mtime_raw);
  53. auto size = fs.size();
  54. return std::string("W/\"") + from_i_to_hex(mtime) + "-" +
  55. from_i_to_hex(size) + "\"";
  56. }
  57. // Format time_t as HTTP-date (RFC 9110 Section 5.6.7): "Sun, 06 Nov 1994
  58. // 08:49:37 GMT" This implementation is defensive: it validates `mtime`, checks
  59. // return values from `gmtime_r`/`gmtime_s`, and ensures `strftime` succeeds.
  60. std::string file_mtime_to_http_date(time_t mtime) {
  61. if (mtime < 0) { return std::string(); }
  62. struct tm tm_buf;
  63. #ifdef _WIN32
  64. if (gmtime_s(&tm_buf, &mtime) != 0) { return std::string(); }
  65. #else
  66. if (gmtime_r(&mtime, &tm_buf) == nullptr) { return std::string(); }
  67. #endif
  68. char buf[64];
  69. if (strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S GMT", &tm_buf) == 0) {
  70. return std::string();
  71. }
  72. return std::string(buf);
  73. }
  74. // Parse HTTP-date (RFC 9110 Section 5.6.7) to time_t. Returns -1 on failure.
  75. time_t parse_http_date(const std::string &date_str) {
  76. struct tm tm_buf;
  77. // Create a classic locale object once for all parsing attempts
  78. const std::locale classic_locale = std::locale::classic();
  79. // Try to parse using std::get_time (C++11, cross-platform)
  80. auto try_parse = [&](const char *fmt) -> bool {
  81. std::istringstream ss(date_str);
  82. ss.imbue(classic_locale);
  83. memset(&tm_buf, 0, sizeof(tm_buf));
  84. ss >> std::get_time(&tm_buf, fmt);
  85. return !ss.fail();
  86. };
  87. // RFC 9110 preferred format (HTTP-date): "Sun, 06 Nov 1994 08:49:37 GMT"
  88. if (!try_parse("%a, %d %b %Y %H:%M:%S")) {
  89. // RFC 850 format: "Sunday, 06-Nov-94 08:49:37 GMT"
  90. if (!try_parse("%A, %d-%b-%y %H:%M:%S")) {
  91. // asctime format: "Sun Nov 6 08:49:37 1994"
  92. if (!try_parse("%a %b %d %H:%M:%S %Y")) {
  93. return static_cast<time_t>(-1);
  94. }
  95. }
  96. }
  97. #ifdef _WIN32
  98. return _mkgmtime(&tm_buf);
  99. #else
  100. return timegm(&tm_buf);
  101. #endif
  102. }
  103. bool is_weak_etag(const std::string &s) {
  104. // Check if the string is a weak ETag (starts with 'W/"')
  105. return s.size() > 3 && s[0] == 'W' && s[1] == '/' && s[2] == '"';
  106. }
  107. bool is_strong_etag(const std::string &s) {
  108. // Check if the string is a strong ETag (starts and ends with '"', at least 2
  109. // chars)
  110. return s.size() >= 2 && s[0] == '"' && s.back() == '"';
  111. }
  112. size_t to_utf8(int code, char *buff) {
  113. if (code < 0x0080) {
  114. buff[0] = static_cast<char>(code & 0x7F);
  115. return 1;
  116. } else if (code < 0x0800) {
  117. buff[0] = static_cast<char>(0xC0 | ((code >> 6) & 0x1F));
  118. buff[1] = static_cast<char>(0x80 | (code & 0x3F));
  119. return 2;
  120. } else if (code < 0xD800) {
  121. buff[0] = static_cast<char>(0xE0 | ((code >> 12) & 0xF));
  122. buff[1] = static_cast<char>(0x80 | ((code >> 6) & 0x3F));
  123. buff[2] = static_cast<char>(0x80 | (code & 0x3F));
  124. return 3;
  125. } else if (code < 0xE000) { // D800 - DFFF is invalid...
  126. return 0;
  127. } else if (code < 0x10000) {
  128. buff[0] = static_cast<char>(0xE0 | ((code >> 12) & 0xF));
  129. buff[1] = static_cast<char>(0x80 | ((code >> 6) & 0x3F));
  130. buff[2] = static_cast<char>(0x80 | (code & 0x3F));
  131. return 3;
  132. } else if (code < 0x110000) {
  133. buff[0] = static_cast<char>(0xF0 | ((code >> 18) & 0x7));
  134. buff[1] = static_cast<char>(0x80 | ((code >> 12) & 0x3F));
  135. buff[2] = static_cast<char>(0x80 | ((code >> 6) & 0x3F));
  136. buff[3] = static_cast<char>(0x80 | (code & 0x3F));
  137. return 4;
  138. }
  139. // NOTREACHED
  140. return 0;
  141. }
  142. // NOTE: This code came up with the following stackoverflow post:
  143. // https://stackoverflow.com/questions/180947/base64-decode-snippet-in-c
  144. std::string base64_encode(const std::string &in) {
  145. static const auto lookup =
  146. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  147. std::string out;
  148. out.reserve(in.size());
  149. auto val = 0;
  150. auto valb = -6;
  151. for (auto c : in) {
  152. val = (val << 8) + static_cast<uint8_t>(c);
  153. valb += 8;
  154. while (valb >= 0) {
  155. out.push_back(lookup[(val >> valb) & 0x3F]);
  156. valb -= 6;
  157. }
  158. }
  159. if (valb > -6) { out.push_back(lookup[((val << 8) >> (valb + 8)) & 0x3F]); }
  160. while (out.size() % 4) {
  161. out.push_back('=');
  162. }
  163. return out;
  164. }
  165. bool is_valid_path(const std::string &path) {
  166. size_t level = 0;
  167. size_t i = 0;
  168. // Skip slash
  169. while (i < path.size() && path[i] == '/') {
  170. i++;
  171. }
  172. while (i < path.size()) {
  173. // Read component
  174. auto beg = i;
  175. while (i < path.size() && path[i] != '/') {
  176. if (path[i] == '\0') {
  177. return false;
  178. } else if (path[i] == '\\') {
  179. return false;
  180. }
  181. i++;
  182. }
  183. auto len = i - beg;
  184. assert(len > 0);
  185. if (!path.compare(beg, len, ".")) {
  186. ;
  187. } else if (!path.compare(beg, len, "..")) {
  188. if (level == 0) { return false; }
  189. level--;
  190. } else {
  191. level++;
  192. }
  193. // Skip slash
  194. while (i < path.size() && path[i] == '/') {
  195. i++;
  196. }
  197. }
  198. return true;
  199. }
  200. FileStat::FileStat(const std::string &path) {
  201. #if defined(_WIN32)
  202. auto wpath = u8string_to_wstring(path.c_str());
  203. ret_ = _wstat(wpath.c_str(), &st_);
  204. #else
  205. ret_ = stat(path.c_str(), &st_);
  206. #endif
  207. }
  208. bool FileStat::is_file() const {
  209. return ret_ >= 0 && S_ISREG(st_.st_mode);
  210. }
  211. bool FileStat::is_dir() const {
  212. return ret_ >= 0 && S_ISDIR(st_.st_mode);
  213. }
  214. time_t FileStat::mtime() const {
  215. return ret_ >= 0 ? static_cast<time_t>(st_.st_mtime)
  216. : static_cast<time_t>(-1);
  217. }
  218. size_t FileStat::size() const {
  219. return ret_ >= 0 ? static_cast<size_t>(st_.st_size) : 0;
  220. }
  221. std::string encode_path(const std::string &s) {
  222. std::string result;
  223. result.reserve(s.size());
  224. for (size_t i = 0; s[i]; i++) {
  225. switch (s[i]) {
  226. case ' ': result += "%20"; break;
  227. case '+': result += "%2B"; break;
  228. case '\r': result += "%0D"; break;
  229. case '\n': result += "%0A"; break;
  230. case '\'': result += "%27"; break;
  231. case ',': result += "%2C"; break;
  232. // case ':': result += "%3A"; break; // ok? probably...
  233. case ';': result += "%3B"; break;
  234. default:
  235. auto c = static_cast<uint8_t>(s[i]);
  236. if (c >= 0x80) {
  237. result += '%';
  238. char hex[4];
  239. auto len = snprintf(hex, sizeof(hex) - 1, "%02X", c);
  240. assert(len == 2);
  241. result.append(hex, static_cast<size_t>(len));
  242. } else {
  243. result += s[i];
  244. }
  245. break;
  246. }
  247. }
  248. return result;
  249. }
  250. std::string file_extension(const std::string &path) {
  251. std::smatch m;
  252. thread_local auto re = std::regex("\\.([a-zA-Z0-9]+)$");
  253. if (std::regex_search(path, m, re)) { return m[1].str(); }
  254. return std::string();
  255. }
  256. bool is_space_or_tab(char c) { return c == ' ' || c == '\t'; }
  257. template <typename T>
  258. bool parse_header(const char *beg, const char *end, T fn);
  259. template <typename T>
  260. bool parse_header(const char *beg, const char *end, T fn) {
  261. // Skip trailing spaces and tabs.
  262. while (beg < end && is_space_or_tab(end[-1])) {
  263. end--;
  264. }
  265. auto p = beg;
  266. while (p < end && *p != ':') {
  267. p++;
  268. }
  269. auto name = std::string(beg, p);
  270. if (!detail::fields::is_field_name(name)) { return false; }
  271. if (p == end) { return false; }
  272. auto key_end = p;
  273. if (*p++ != ':') { return false; }
  274. while (p < end && is_space_or_tab(*p)) {
  275. p++;
  276. }
  277. if (p <= end) {
  278. auto key_len = key_end - beg;
  279. if (!key_len) { return false; }
  280. auto key = std::string(beg, key_end);
  281. auto val = std::string(p, end);
  282. if (!detail::fields::is_field_value(val)) { return false; }
  283. if (case_ignore::equal(key, "Location") ||
  284. case_ignore::equal(key, "Referer")) {
  285. fn(key, val);
  286. } else {
  287. fn(key, decode_path_component(val));
  288. }
  289. return true;
  290. }
  291. return false;
  292. }
  293. bool parse_trailers(stream_line_reader &line_reader, Headers &dest,
  294. const Headers &src_headers) {
  295. // NOTE: In RFC 9112, '7.1 Chunked Transfer Coding' mentions "The chunked
  296. // transfer coding is complete when a chunk with a chunk-size of zero is
  297. // received, possibly followed by a trailer section, and finally terminated by
  298. // an empty line". https://www.rfc-editor.org/rfc/rfc9112.html#section-7.1
  299. //
  300. // In '7.1.3. Decoding Chunked', however, the pseudo-code in the section
  301. // doesn't care for the existence of the final CRLF. In other words, it seems
  302. // to be ok whether the final CRLF exists or not in the chunked data.
  303. // https://www.rfc-editor.org/rfc/rfc9112.html#section-7.1.3
  304. //
  305. // According to the reference code in RFC 9112, cpp-httplib now allows
  306. // chunked transfer coding data without the final CRLF.
  307. // RFC 7230 Section 4.1.2 - Headers prohibited in trailers
  308. thread_local case_ignore::unordered_set<std::string> prohibited_trailers = {
  309. "transfer-encoding",
  310. "content-length",
  311. "host",
  312. "authorization",
  313. "www-authenticate",
  314. "proxy-authenticate",
  315. "proxy-authorization",
  316. "cookie",
  317. "set-cookie",
  318. "cache-control",
  319. "expect",
  320. "max-forwards",
  321. "pragma",
  322. "range",
  323. "te",
  324. "age",
  325. "expires",
  326. "date",
  327. "location",
  328. "retry-after",
  329. "vary",
  330. "warning",
  331. "content-encoding",
  332. "content-type",
  333. "content-range",
  334. "trailer"};
  335. case_ignore::unordered_set<std::string> declared_trailers;
  336. auto trailer_header = get_header_value(src_headers, "Trailer", "", 0);
  337. if (trailer_header && std::strlen(trailer_header)) {
  338. auto len = std::strlen(trailer_header);
  339. split(trailer_header, trailer_header + len, ',',
  340. [&](const char *b, const char *e) {
  341. const char *kbeg = b;
  342. const char *kend = e;
  343. while (kbeg < kend && (*kbeg == ' ' || *kbeg == '\t')) {
  344. ++kbeg;
  345. }
  346. while (kend > kbeg && (kend[-1] == ' ' || kend[-1] == '\t')) {
  347. --kend;
  348. }
  349. std::string key(kbeg, static_cast<size_t>(kend - kbeg));
  350. if (!key.empty() &&
  351. prohibited_trailers.find(key) == prohibited_trailers.end()) {
  352. declared_trailers.insert(key);
  353. }
  354. });
  355. }
  356. size_t trailer_header_count = 0;
  357. while (strcmp(line_reader.ptr(), "\r\n") != 0) {
  358. if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; }
  359. if (trailer_header_count >= CPPHTTPLIB_HEADER_MAX_COUNT) { return false; }
  360. constexpr auto line_terminator_len = 2;
  361. auto line_beg = line_reader.ptr();
  362. auto line_end =
  363. line_reader.ptr() + line_reader.size() - line_terminator_len;
  364. if (!parse_header(line_beg, line_end,
  365. [&](const std::string &key, const std::string &val) {
  366. if (declared_trailers.find(key) !=
  367. declared_trailers.end()) {
  368. dest.emplace(key, val);
  369. trailer_header_count++;
  370. }
  371. })) {
  372. return false;
  373. }
  374. if (!line_reader.getline()) { return false; }
  375. }
  376. return true;
  377. }
  378. std::pair<size_t, size_t> trim(const char *b, const char *e, size_t left,
  379. size_t right) {
  380. while (b + left < e && is_space_or_tab(b[left])) {
  381. left++;
  382. }
  383. while (right > 0 && is_space_or_tab(b[right - 1])) {
  384. right--;
  385. }
  386. return std::make_pair(left, right);
  387. }
  388. std::string trim_copy(const std::string &s) {
  389. auto r = trim(s.data(), s.data() + s.size(), 0, s.size());
  390. return s.substr(r.first, r.second - r.first);
  391. }
  392. std::string trim_double_quotes_copy(const std::string &s) {
  393. if (s.length() >= 2 && s.front() == '"' && s.back() == '"') {
  394. return s.substr(1, s.size() - 2);
  395. }
  396. return s;
  397. }
  398. void
  399. divide(const char *data, std::size_t size, char d,
  400. std::function<void(const char *, std::size_t, const char *, std::size_t)>
  401. fn) {
  402. const auto it = std::find(data, data + size, d);
  403. const auto found = static_cast<std::size_t>(it != data + size);
  404. const auto lhs_data = data;
  405. const auto lhs_size = static_cast<std::size_t>(it - data);
  406. const auto rhs_data = it + found;
  407. const auto rhs_size = size - lhs_size - found;
  408. fn(lhs_data, lhs_size, rhs_data, rhs_size);
  409. }
  410. void
  411. divide(const std::string &str, char d,
  412. std::function<void(const char *, std::size_t, const char *, std::size_t)>
  413. fn) {
  414. divide(str.data(), str.size(), d, std::move(fn));
  415. }
  416. void split(const char *b, const char *e, char d,
  417. std::function<void(const char *, const char *)> fn) {
  418. return split(b, e, d, (std::numeric_limits<size_t>::max)(), std::move(fn));
  419. }
  420. void split(const char *b, const char *e, char d, size_t m,
  421. std::function<void(const char *, const char *)> fn) {
  422. size_t i = 0;
  423. size_t beg = 0;
  424. size_t count = 1;
  425. while (e ? (b + i < e) : (b[i] != '\0')) {
  426. if (b[i] == d && count < m) {
  427. auto r = trim(b, e, beg, i);
  428. if (r.first < r.second) { fn(&b[r.first], &b[r.second]); }
  429. beg = i + 1;
  430. count++;
  431. }
  432. i++;
  433. }
  434. if (i) {
  435. auto r = trim(b, e, beg, i);
  436. if (r.first < r.second) { fn(&b[r.first], &b[r.second]); }
  437. }
  438. }
  439. bool split_find(const char *b, const char *e, char d, size_t m,
  440. std::function<bool(const char *, const char *)> fn) {
  441. size_t i = 0;
  442. size_t beg = 0;
  443. size_t count = 1;
  444. while (e ? (b + i < e) : (b[i] != '\0')) {
  445. if (b[i] == d && count < m) {
  446. auto r = trim(b, e, beg, i);
  447. if (r.first < r.second) {
  448. auto found = fn(&b[r.first], &b[r.second]);
  449. if (found) { return true; }
  450. }
  451. beg = i + 1;
  452. count++;
  453. }
  454. i++;
  455. }
  456. if (i) {
  457. auto r = trim(b, e, beg, i);
  458. if (r.first < r.second) {
  459. auto found = fn(&b[r.first], &b[r.second]);
  460. if (found) { return true; }
  461. }
  462. }
  463. return false;
  464. }
  465. bool split_find(const char *b, const char *e, char d,
  466. std::function<bool(const char *, const char *)> fn) {
  467. return split_find(b, e, d, (std::numeric_limits<size_t>::max)(),
  468. std::move(fn));
  469. }
  470. stream_line_reader::stream_line_reader(Stream &strm, char *fixed_buffer,
  471. size_t fixed_buffer_size)
  472. : strm_(strm), fixed_buffer_(fixed_buffer),
  473. fixed_buffer_size_(fixed_buffer_size) {}
  474. const char *stream_line_reader::ptr() const {
  475. if (growable_buffer_.empty()) {
  476. return fixed_buffer_;
  477. } else {
  478. return growable_buffer_.data();
  479. }
  480. }
  481. size_t stream_line_reader::size() const {
  482. if (growable_buffer_.empty()) {
  483. return fixed_buffer_used_size_;
  484. } else {
  485. return growable_buffer_.size();
  486. }
  487. }
  488. bool stream_line_reader::end_with_crlf() const {
  489. auto end = ptr() + size();
  490. return size() >= 2 && end[-2] == '\r' && end[-1] == '\n';
  491. }
  492. bool stream_line_reader::getline() {
  493. fixed_buffer_used_size_ = 0;
  494. growable_buffer_.clear();
  495. #ifndef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR
  496. char prev_byte = 0;
  497. #endif
  498. for (size_t i = 0;; i++) {
  499. if (size() >= CPPHTTPLIB_MAX_LINE_LENGTH) {
  500. // Treat exceptionally long lines as an error to
  501. // prevent infinite loops/memory exhaustion
  502. return false;
  503. }
  504. char byte;
  505. auto n = strm_.read(&byte, 1);
  506. if (n < 0) {
  507. return false;
  508. } else if (n == 0) {
  509. if (i == 0) {
  510. return false;
  511. } else {
  512. break;
  513. }
  514. }
  515. append(byte);
  516. #ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR
  517. if (byte == '\n') { break; }
  518. #else
  519. if (prev_byte == '\r' && byte == '\n') { break; }
  520. prev_byte = byte;
  521. #endif
  522. }
  523. return true;
  524. }
  525. void stream_line_reader::append(char c) {
  526. if (fixed_buffer_used_size_ < fixed_buffer_size_ - 1) {
  527. fixed_buffer_[fixed_buffer_used_size_++] = c;
  528. fixed_buffer_[fixed_buffer_used_size_] = '\0';
  529. } else {
  530. if (growable_buffer_.empty()) {
  531. assert(fixed_buffer_[fixed_buffer_used_size_] == '\0');
  532. growable_buffer_.assign(fixed_buffer_, fixed_buffer_used_size_);
  533. }
  534. growable_buffer_ += c;
  535. }
  536. }
  537. mmap::mmap(const char *path) { open(path); }
  538. mmap::~mmap() { close(); }
  539. bool mmap::open(const char *path) {
  540. close();
  541. #if defined(_WIN32)
  542. auto wpath = u8string_to_wstring(path);
  543. if (wpath.empty()) { return false; }
  544. hFile_ = ::CreateFile2(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ,
  545. OPEN_EXISTING, NULL);
  546. if (hFile_ == INVALID_HANDLE_VALUE) { return false; }
  547. LARGE_INTEGER size{};
  548. if (!::GetFileSizeEx(hFile_, &size)) { return false; }
  549. // If the following line doesn't compile due to QuadPart, update Windows SDK.
  550. // See:
  551. // https://github.com/yhirose/cpp-httplib/issues/1903#issuecomment-2316520721
  552. if (static_cast<ULONGLONG>(size.QuadPart) >
  553. (std::numeric_limits<decltype(size_)>::max)()) {
  554. // `size_t` might be 32-bits, on 32-bits Windows.
  555. return false;
  556. }
  557. size_ = static_cast<size_t>(size.QuadPart);
  558. hMapping_ =
  559. ::CreateFileMappingFromApp(hFile_, NULL, PAGE_READONLY, size_, NULL);
  560. // Special treatment for an empty file...
  561. if (hMapping_ == NULL && size_ == 0) {
  562. close();
  563. is_open_empty_file = true;
  564. return true;
  565. }
  566. if (hMapping_ == NULL) {
  567. close();
  568. return false;
  569. }
  570. addr_ = ::MapViewOfFileFromApp(hMapping_, FILE_MAP_READ, 0, 0);
  571. if (addr_ == nullptr) {
  572. close();
  573. return false;
  574. }
  575. #else
  576. fd_ = ::open(path, O_RDONLY);
  577. if (fd_ == -1) { return false; }
  578. struct stat sb;
  579. if (fstat(fd_, &sb) == -1) {
  580. close();
  581. return false;
  582. }
  583. size_ = static_cast<size_t>(sb.st_size);
  584. addr_ = ::mmap(NULL, size_, PROT_READ, MAP_PRIVATE, fd_, 0);
  585. // Special treatment for an empty file...
  586. if (addr_ == MAP_FAILED && size_ == 0) {
  587. close();
  588. is_open_empty_file = true;
  589. return false;
  590. }
  591. #endif
  592. return true;
  593. }
  594. bool mmap::is_open() const {
  595. return is_open_empty_file ? true : addr_ != nullptr;
  596. }
  597. size_t mmap::size() const { return size_; }
  598. const char *mmap::data() const {
  599. return is_open_empty_file ? "" : static_cast<const char *>(addr_);
  600. }
  601. void mmap::close() {
  602. #if defined(_WIN32)
  603. if (addr_) {
  604. ::UnmapViewOfFile(addr_);
  605. addr_ = nullptr;
  606. }
  607. if (hMapping_) {
  608. ::CloseHandle(hMapping_);
  609. hMapping_ = NULL;
  610. }
  611. if (hFile_ != INVALID_HANDLE_VALUE) {
  612. ::CloseHandle(hFile_);
  613. hFile_ = INVALID_HANDLE_VALUE;
  614. }
  615. is_open_empty_file = false;
  616. #else
  617. if (addr_ != nullptr) {
  618. munmap(addr_, size_);
  619. addr_ = nullptr;
  620. }
  621. if (fd_ != -1) {
  622. ::close(fd_);
  623. fd_ = -1;
  624. }
  625. #endif
  626. size_ = 0;
  627. }
  628. int close_socket(socket_t sock) {
  629. #ifdef _WIN32
  630. return closesocket(sock);
  631. #else
  632. return close(sock);
  633. #endif
  634. }
  635. template <typename T> inline ssize_t handle_EINTR(T fn) {
  636. ssize_t res = 0;
  637. while (true) {
  638. res = fn();
  639. if (res < 0 && errno == EINTR) {
  640. std::this_thread::sleep_for(std::chrono::microseconds{1});
  641. continue;
  642. }
  643. break;
  644. }
  645. return res;
  646. }
  647. ssize_t read_socket(socket_t sock, void *ptr, size_t size, int flags) {
  648. return handle_EINTR([&]() {
  649. return recv(sock,
  650. #ifdef _WIN32
  651. static_cast<char *>(ptr), static_cast<int>(size),
  652. #else
  653. ptr, size,
  654. #endif
  655. flags);
  656. });
  657. }
  658. ssize_t send_socket(socket_t sock, const void *ptr, size_t size,
  659. int flags) {
  660. return handle_EINTR([&]() {
  661. return send(sock,
  662. #ifdef _WIN32
  663. static_cast<const char *>(ptr), static_cast<int>(size),
  664. #else
  665. ptr, size,
  666. #endif
  667. flags);
  668. });
  669. }
  670. int poll_wrapper(struct pollfd *fds, nfds_t nfds, int timeout) {
  671. #ifdef _WIN32
  672. return ::WSAPoll(fds, nfds, timeout);
  673. #else
  674. return ::poll(fds, nfds, timeout);
  675. #endif
  676. }
  677. template <bool Read>
  678. ssize_t select_impl(socket_t sock, time_t sec, time_t usec) {
  679. #ifdef __APPLE__
  680. if (sock >= FD_SETSIZE) { return -1; }
  681. fd_set fds, *rfds, *wfds;
  682. FD_ZERO(&fds);
  683. FD_SET(sock, &fds);
  684. rfds = (Read ? &fds : nullptr);
  685. wfds = (Read ? nullptr : &fds);
  686. timeval tv;
  687. tv.tv_sec = static_cast<long>(sec);
  688. tv.tv_usec = static_cast<decltype(tv.tv_usec)>(usec);
  689. return handle_EINTR([&]() {
  690. return select(static_cast<int>(sock + 1), rfds, wfds, nullptr, &tv);
  691. });
  692. #else
  693. struct pollfd pfd;
  694. pfd.fd = sock;
  695. pfd.events = (Read ? POLLIN : POLLOUT);
  696. auto timeout = static_cast<int>(sec * 1000 + usec / 1000);
  697. return handle_EINTR([&]() { return poll_wrapper(&pfd, 1, timeout); });
  698. #endif
  699. }
  700. ssize_t select_read(socket_t sock, time_t sec, time_t usec) {
  701. return select_impl<true>(sock, sec, usec);
  702. }
  703. ssize_t select_write(socket_t sock, time_t sec, time_t usec) {
  704. return select_impl<false>(sock, sec, usec);
  705. }
  706. Error wait_until_socket_is_ready(socket_t sock, time_t sec,
  707. time_t usec) {
  708. #ifdef __APPLE__
  709. if (sock >= FD_SETSIZE) { return Error::Connection; }
  710. fd_set fdsr, fdsw;
  711. FD_ZERO(&fdsr);
  712. FD_ZERO(&fdsw);
  713. FD_SET(sock, &fdsr);
  714. FD_SET(sock, &fdsw);
  715. timeval tv;
  716. tv.tv_sec = static_cast<long>(sec);
  717. tv.tv_usec = static_cast<decltype(tv.tv_usec)>(usec);
  718. auto ret = handle_EINTR([&]() {
  719. return select(static_cast<int>(sock + 1), &fdsr, &fdsw, nullptr, &tv);
  720. });
  721. if (ret == 0) { return Error::ConnectionTimeout; }
  722. if (ret > 0 && (FD_ISSET(sock, &fdsr) || FD_ISSET(sock, &fdsw))) {
  723. auto error = 0;
  724. socklen_t len = sizeof(error);
  725. auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR,
  726. reinterpret_cast<char *>(&error), &len);
  727. auto successful = res >= 0 && !error;
  728. return successful ? Error::Success : Error::Connection;
  729. }
  730. return Error::Connection;
  731. #else
  732. struct pollfd pfd_read;
  733. pfd_read.fd = sock;
  734. pfd_read.events = POLLIN | POLLOUT;
  735. auto timeout = static_cast<int>(sec * 1000 + usec / 1000);
  736. auto poll_res =
  737. handle_EINTR([&]() { return poll_wrapper(&pfd_read, 1, timeout); });
  738. if (poll_res == 0) { return Error::ConnectionTimeout; }
  739. if (poll_res > 0 && pfd_read.revents & (POLLIN | POLLOUT)) {
  740. auto error = 0;
  741. socklen_t len = sizeof(error);
  742. auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR,
  743. reinterpret_cast<char *>(&error), &len);
  744. auto successful = res >= 0 && !error;
  745. return successful ? Error::Success : Error::Connection;
  746. }
  747. return Error::Connection;
  748. #endif
  749. }
  750. bool is_socket_alive(socket_t sock) {
  751. const auto val = detail::select_read(sock, 0, 0);
  752. if (val == 0) {
  753. return true;
  754. } else if (val < 0 && errno == EBADF) {
  755. return false;
  756. }
  757. char buf[1];
  758. return detail::read_socket(sock, &buf[0], sizeof(buf), MSG_PEEK) > 0;
  759. }
  760. class SocketStream final : public Stream {
  761. public:
  762. SocketStream(socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec,
  763. time_t write_timeout_sec, time_t write_timeout_usec,
  764. time_t max_timeout_msec = 0,
  765. std::chrono::time_point<std::chrono::steady_clock> start_time =
  766. (std::chrono::steady_clock::time_point::min)());
  767. ~SocketStream() override;
  768. bool is_readable() const override;
  769. bool wait_readable() const override;
  770. bool wait_writable() const override;
  771. ssize_t read(char *ptr, size_t size) override;
  772. ssize_t write(const char *ptr, size_t size) override;
  773. void get_remote_ip_and_port(std::string &ip, int &port) const override;
  774. void get_local_ip_and_port(std::string &ip, int &port) const override;
  775. socket_t socket() const override;
  776. time_t duration() const override;
  777. private:
  778. socket_t sock_;
  779. time_t read_timeout_sec_;
  780. time_t read_timeout_usec_;
  781. time_t write_timeout_sec_;
  782. time_t write_timeout_usec_;
  783. time_t max_timeout_msec_;
  784. const std::chrono::time_point<std::chrono::steady_clock> start_time_;
  785. std::vector<char> read_buff_;
  786. size_t read_buff_off_ = 0;
  787. size_t read_buff_content_size_ = 0;
  788. static const size_t read_buff_size_ = 1024l * 4;
  789. };
  790. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  791. class SSLSocketStream final : public Stream {
  792. public:
  793. SSLSocketStream(
  794. socket_t sock, SSL *ssl, time_t read_timeout_sec,
  795. time_t read_timeout_usec, time_t write_timeout_sec,
  796. time_t write_timeout_usec, time_t max_timeout_msec = 0,
  797. std::chrono::time_point<std::chrono::steady_clock> start_time =
  798. (std::chrono::steady_clock::time_point::min)());
  799. ~SSLSocketStream() override;
  800. bool is_readable() const override;
  801. bool wait_readable() const override;
  802. bool wait_writable() const override;
  803. ssize_t read(char *ptr, size_t size) override;
  804. ssize_t write(const char *ptr, size_t size) override;
  805. void get_remote_ip_and_port(std::string &ip, int &port) const override;
  806. void get_local_ip_and_port(std::string &ip, int &port) const override;
  807. socket_t socket() const override;
  808. time_t duration() const override;
  809. private:
  810. socket_t sock_;
  811. SSL *ssl_;
  812. time_t read_timeout_sec_;
  813. time_t read_timeout_usec_;
  814. time_t write_timeout_sec_;
  815. time_t write_timeout_usec_;
  816. time_t max_timeout_msec_;
  817. const std::chrono::time_point<std::chrono::steady_clock> start_time_;
  818. };
  819. #endif
  820. bool keep_alive(const std::atomic<socket_t> &svr_sock, socket_t sock,
  821. time_t keep_alive_timeout_sec) {
  822. using namespace std::chrono;
  823. const auto interval_usec =
  824. CPPHTTPLIB_KEEPALIVE_TIMEOUT_CHECK_INTERVAL_USECOND;
  825. // Avoid expensive `steady_clock::now()` call for the first time
  826. if (select_read(sock, 0, interval_usec) > 0) { return true; }
  827. const auto start = steady_clock::now() - microseconds{interval_usec};
  828. const auto timeout = seconds{keep_alive_timeout_sec};
  829. while (true) {
  830. if (svr_sock == INVALID_SOCKET) {
  831. break; // Server socket is closed
  832. }
  833. auto val = select_read(sock, 0, interval_usec);
  834. if (val < 0) {
  835. break; // Ssocket error
  836. } else if (val == 0) {
  837. if (steady_clock::now() - start > timeout) {
  838. break; // Timeout
  839. }
  840. } else {
  841. return true; // Ready for read
  842. }
  843. }
  844. return false;
  845. }
  846. template <typename T>
  847. bool
  848. process_server_socket_core(const std::atomic<socket_t> &svr_sock, socket_t sock,
  849. size_t keep_alive_max_count,
  850. time_t keep_alive_timeout_sec, T callback) {
  851. assert(keep_alive_max_count > 0);
  852. auto ret = false;
  853. auto count = keep_alive_max_count;
  854. while (count > 0 && keep_alive(svr_sock, sock, keep_alive_timeout_sec)) {
  855. auto close_connection = count == 1;
  856. auto connection_closed = false;
  857. ret = callback(close_connection, connection_closed);
  858. if (!ret || connection_closed) { break; }
  859. count--;
  860. }
  861. return ret;
  862. }
  863. template <typename T>
  864. bool
  865. process_server_socket(const std::atomic<socket_t> &svr_sock, socket_t sock,
  866. size_t keep_alive_max_count,
  867. time_t keep_alive_timeout_sec, time_t read_timeout_sec,
  868. time_t read_timeout_usec, time_t write_timeout_sec,
  869. time_t write_timeout_usec, T callback) {
  870. return process_server_socket_core(
  871. svr_sock, sock, keep_alive_max_count, keep_alive_timeout_sec,
  872. [&](bool close_connection, bool &connection_closed) {
  873. SocketStream strm(sock, read_timeout_sec, read_timeout_usec,
  874. write_timeout_sec, write_timeout_usec);
  875. return callback(strm, close_connection, connection_closed);
  876. });
  877. }
  878. bool process_client_socket(
  879. socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec,
  880. time_t write_timeout_sec, time_t write_timeout_usec,
  881. time_t max_timeout_msec,
  882. std::chrono::time_point<std::chrono::steady_clock> start_time,
  883. std::function<bool(Stream &)> callback) {
  884. SocketStream strm(sock, read_timeout_sec, read_timeout_usec,
  885. write_timeout_sec, write_timeout_usec, max_timeout_msec,
  886. start_time);
  887. return callback(strm);
  888. }
  889. int shutdown_socket(socket_t sock) {
  890. #ifdef _WIN32
  891. return shutdown(sock, SD_BOTH);
  892. #else
  893. return shutdown(sock, SHUT_RDWR);
  894. #endif
  895. }
  896. std::string escape_abstract_namespace_unix_domain(const std::string &s) {
  897. if (s.size() > 1 && s[0] == '\0') {
  898. auto ret = s;
  899. ret[0] = '@';
  900. return ret;
  901. }
  902. return s;
  903. }
  904. std::string
  905. unescape_abstract_namespace_unix_domain(const std::string &s) {
  906. if (s.size() > 1 && s[0] == '@') {
  907. auto ret = s;
  908. ret[0] = '\0';
  909. return ret;
  910. }
  911. return s;
  912. }
  913. int getaddrinfo_with_timeout(const char *node, const char *service,
  914. const struct addrinfo *hints,
  915. struct addrinfo **res, time_t timeout_sec) {
  916. #ifdef CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO
  917. if (timeout_sec <= 0) {
  918. // No timeout specified, use standard getaddrinfo
  919. return getaddrinfo(node, service, hints, res);
  920. }
  921. #ifdef _WIN32
  922. // Windows-specific implementation using GetAddrInfoEx with overlapped I/O
  923. OVERLAPPED overlapped = {0};
  924. HANDLE event = CreateEventW(nullptr, TRUE, FALSE, nullptr);
  925. if (!event) { return EAI_FAIL; }
  926. overlapped.hEvent = event;
  927. PADDRINFOEXW result_addrinfo = nullptr;
  928. HANDLE cancel_handle = nullptr;
  929. ADDRINFOEXW hints_ex = {0};
  930. if (hints) {
  931. hints_ex.ai_flags = hints->ai_flags;
  932. hints_ex.ai_family = hints->ai_family;
  933. hints_ex.ai_socktype = hints->ai_socktype;
  934. hints_ex.ai_protocol = hints->ai_protocol;
  935. }
  936. auto wnode = u8string_to_wstring(node);
  937. auto wservice = u8string_to_wstring(service);
  938. auto ret = ::GetAddrInfoExW(wnode.data(), wservice.data(), NS_DNS, nullptr,
  939. hints ? &hints_ex : nullptr, &result_addrinfo,
  940. nullptr, &overlapped, nullptr, &cancel_handle);
  941. if (ret == WSA_IO_PENDING) {
  942. auto wait_result =
  943. ::WaitForSingleObject(event, static_cast<DWORD>(timeout_sec * 1000));
  944. if (wait_result == WAIT_TIMEOUT) {
  945. if (cancel_handle) { ::GetAddrInfoExCancel(&cancel_handle); }
  946. ::CloseHandle(event);
  947. return EAI_AGAIN;
  948. }
  949. DWORD bytes_returned;
  950. if (!::GetOverlappedResult((HANDLE)INVALID_SOCKET, &overlapped,
  951. &bytes_returned, FALSE)) {
  952. ::CloseHandle(event);
  953. return ::WSAGetLastError();
  954. }
  955. }
  956. ::CloseHandle(event);
  957. if (ret == NO_ERROR || ret == WSA_IO_PENDING) {
  958. *res = reinterpret_cast<struct addrinfo *>(result_addrinfo);
  959. return 0;
  960. }
  961. return ret;
  962. #elif TARGET_OS_MAC
  963. // macOS implementation using CFHost API for asynchronous DNS resolution
  964. CFStringRef hostname_ref = CFStringCreateWithCString(
  965. kCFAllocatorDefault, node, kCFStringEncodingUTF8);
  966. if (!hostname_ref) { return EAI_MEMORY; }
  967. CFHostRef host_ref = CFHostCreateWithName(kCFAllocatorDefault, hostname_ref);
  968. CFRelease(hostname_ref);
  969. if (!host_ref) { return EAI_MEMORY; }
  970. // Set up context for callback
  971. struct CFHostContext {
  972. bool completed = false;
  973. bool success = false;
  974. CFArrayRef addresses = nullptr;
  975. std::mutex mutex;
  976. std::condition_variable cv;
  977. } context;
  978. CFHostClientContext client_context;
  979. memset(&client_context, 0, sizeof(client_context));
  980. client_context.info = &context;
  981. // Set callback
  982. auto callback = [](CFHostRef theHost, CFHostInfoType /*typeInfo*/,
  983. const CFStreamError *error, void *info) {
  984. auto ctx = static_cast<CFHostContext *>(info);
  985. std::lock_guard<std::mutex> lock(ctx->mutex);
  986. if (error && error->error != 0) {
  987. ctx->success = false;
  988. } else {
  989. Boolean hasBeenResolved;
  990. ctx->addresses = CFHostGetAddressing(theHost, &hasBeenResolved);
  991. if (ctx->addresses && hasBeenResolved) {
  992. CFRetain(ctx->addresses);
  993. ctx->success = true;
  994. } else {
  995. ctx->success = false;
  996. }
  997. }
  998. ctx->completed = true;
  999. ctx->cv.notify_one();
  1000. };
  1001. if (!CFHostSetClient(host_ref, callback, &client_context)) {
  1002. CFRelease(host_ref);
  1003. return EAI_SYSTEM;
  1004. }
  1005. // Schedule on run loop
  1006. CFRunLoopRef run_loop = CFRunLoopGetCurrent();
  1007. CFHostScheduleWithRunLoop(host_ref, run_loop, kCFRunLoopDefaultMode);
  1008. // Start resolution
  1009. CFStreamError stream_error;
  1010. if (!CFHostStartInfoResolution(host_ref, kCFHostAddresses, &stream_error)) {
  1011. CFHostUnscheduleFromRunLoop(host_ref, run_loop, kCFRunLoopDefaultMode);
  1012. CFRelease(host_ref);
  1013. return EAI_FAIL;
  1014. }
  1015. // Wait for completion with timeout
  1016. auto timeout_time =
  1017. std::chrono::steady_clock::now() + std::chrono::seconds(timeout_sec);
  1018. bool timed_out = false;
  1019. {
  1020. std::unique_lock<std::mutex> lock(context.mutex);
  1021. while (!context.completed) {
  1022. auto now = std::chrono::steady_clock::now();
  1023. if (now >= timeout_time) {
  1024. timed_out = true;
  1025. break;
  1026. }
  1027. // Run the runloop for a short time
  1028. lock.unlock();
  1029. CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, true);
  1030. lock.lock();
  1031. }
  1032. }
  1033. // Clean up
  1034. CFHostUnscheduleFromRunLoop(host_ref, run_loop, kCFRunLoopDefaultMode);
  1035. CFHostSetClient(host_ref, nullptr, nullptr);
  1036. if (timed_out || !context.completed) {
  1037. CFHostCancelInfoResolution(host_ref, kCFHostAddresses);
  1038. CFRelease(host_ref);
  1039. return EAI_AGAIN;
  1040. }
  1041. if (!context.success || !context.addresses) {
  1042. CFRelease(host_ref);
  1043. return EAI_NODATA;
  1044. }
  1045. // Convert CFArray to addrinfo
  1046. CFIndex count = CFArrayGetCount(context.addresses);
  1047. if (count == 0) {
  1048. CFRelease(context.addresses);
  1049. CFRelease(host_ref);
  1050. return EAI_NODATA;
  1051. }
  1052. struct addrinfo *result_addrinfo = nullptr;
  1053. struct addrinfo **current = &result_addrinfo;
  1054. for (CFIndex i = 0; i < count; i++) {
  1055. CFDataRef addr_data =
  1056. static_cast<CFDataRef>(CFArrayGetValueAtIndex(context.addresses, i));
  1057. if (!addr_data) continue;
  1058. const struct sockaddr *sockaddr_ptr =
  1059. reinterpret_cast<const struct sockaddr *>(CFDataGetBytePtr(addr_data));
  1060. socklen_t sockaddr_len = static_cast<socklen_t>(CFDataGetLength(addr_data));
  1061. // Allocate addrinfo structure
  1062. *current = static_cast<struct addrinfo *>(malloc(sizeof(struct addrinfo)));
  1063. if (!*current) {
  1064. freeaddrinfo(result_addrinfo);
  1065. CFRelease(context.addresses);
  1066. CFRelease(host_ref);
  1067. return EAI_MEMORY;
  1068. }
  1069. memset(*current, 0, sizeof(struct addrinfo));
  1070. // Set up addrinfo fields
  1071. (*current)->ai_family = sockaddr_ptr->sa_family;
  1072. (*current)->ai_socktype = hints ? hints->ai_socktype : SOCK_STREAM;
  1073. (*current)->ai_protocol = hints ? hints->ai_protocol : IPPROTO_TCP;
  1074. (*current)->ai_addrlen = sockaddr_len;
  1075. // Copy sockaddr
  1076. (*current)->ai_addr = static_cast<struct sockaddr *>(malloc(sockaddr_len));
  1077. if (!(*current)->ai_addr) {
  1078. freeaddrinfo(result_addrinfo);
  1079. CFRelease(context.addresses);
  1080. CFRelease(host_ref);
  1081. return EAI_MEMORY;
  1082. }
  1083. memcpy((*current)->ai_addr, sockaddr_ptr, sockaddr_len);
  1084. // Set port if service is specified
  1085. if (service && strlen(service) > 0) {
  1086. int port = atoi(service);
  1087. if (port > 0) {
  1088. if (sockaddr_ptr->sa_family == AF_INET) {
  1089. reinterpret_cast<struct sockaddr_in *>((*current)->ai_addr)
  1090. ->sin_port = htons(static_cast<uint16_t>(port));
  1091. } else if (sockaddr_ptr->sa_family == AF_INET6) {
  1092. reinterpret_cast<struct sockaddr_in6 *>((*current)->ai_addr)
  1093. ->sin6_port = htons(static_cast<uint16_t>(port));
  1094. }
  1095. }
  1096. }
  1097. current = &((*current)->ai_next);
  1098. }
  1099. CFRelease(context.addresses);
  1100. CFRelease(host_ref);
  1101. *res = result_addrinfo;
  1102. return 0;
  1103. #elif defined(_GNU_SOURCE) && defined(__GLIBC__) && \
  1104. (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2))
  1105. // Linux implementation using getaddrinfo_a for asynchronous DNS resolution
  1106. struct gaicb request;
  1107. struct gaicb *requests[1] = {&request};
  1108. struct sigevent sevp;
  1109. struct timespec timeout;
  1110. // Initialize the request structure
  1111. memset(&request, 0, sizeof(request));
  1112. request.ar_name = node;
  1113. request.ar_service = service;
  1114. request.ar_request = hints;
  1115. // Set up timeout
  1116. timeout.tv_sec = timeout_sec;
  1117. timeout.tv_nsec = 0;
  1118. // Initialize sigevent structure (not used, but required)
  1119. memset(&sevp, 0, sizeof(sevp));
  1120. sevp.sigev_notify = SIGEV_NONE;
  1121. // Start asynchronous resolution
  1122. int start_result = getaddrinfo_a(GAI_NOWAIT, requests, 1, &sevp);
  1123. if (start_result != 0) { return start_result; }
  1124. // Wait for completion with timeout
  1125. int wait_result =
  1126. gai_suspend((const struct gaicb *const *)requests, 1, &timeout);
  1127. if (wait_result == 0 || wait_result == EAI_ALLDONE) {
  1128. // Completed successfully, get the result
  1129. int gai_result = gai_error(&request);
  1130. if (gai_result == 0) {
  1131. *res = request.ar_result;
  1132. return 0;
  1133. } else {
  1134. // Clean up on error
  1135. if (request.ar_result) { freeaddrinfo(request.ar_result); }
  1136. return gai_result;
  1137. }
  1138. } else if (wait_result == EAI_AGAIN) {
  1139. // Timeout occurred, cancel the request
  1140. gai_cancel(&request);
  1141. return EAI_AGAIN;
  1142. } else {
  1143. // Other error occurred
  1144. gai_cancel(&request);
  1145. return wait_result;
  1146. }
  1147. #else
  1148. // Fallback implementation using thread-based timeout for other Unix systems
  1149. struct GetAddrInfoState {
  1150. ~GetAddrInfoState() {
  1151. if (info) { freeaddrinfo(info); }
  1152. }
  1153. std::mutex mutex;
  1154. std::condition_variable result_cv;
  1155. bool completed = false;
  1156. int result = EAI_SYSTEM;
  1157. std::string node;
  1158. std::string service;
  1159. struct addrinfo hints;
  1160. struct addrinfo *info = nullptr;
  1161. };
  1162. // Allocate on the heap, so the resolver thread can keep using the data.
  1163. auto state = std::make_shared<GetAddrInfoState>();
  1164. state->node = node;
  1165. state->service = service;
  1166. state->hints = *hints;
  1167. std::thread resolve_thread([state]() {
  1168. auto thread_result =
  1169. getaddrinfo(state->node.c_str(), state->service.c_str(), &state->hints,
  1170. &state->info);
  1171. std::lock_guard<std::mutex> lock(state->mutex);
  1172. state->result = thread_result;
  1173. state->completed = true;
  1174. state->result_cv.notify_one();
  1175. });
  1176. // Wait for completion or timeout
  1177. std::unique_lock<std::mutex> lock(state->mutex);
  1178. auto finished =
  1179. state->result_cv.wait_for(lock, std::chrono::seconds(timeout_sec),
  1180. [&] { return state->completed; });
  1181. if (finished) {
  1182. // Operation completed within timeout
  1183. resolve_thread.join();
  1184. *res = state->info;
  1185. state->info = nullptr; // Pass ownership to caller
  1186. return state->result;
  1187. } else {
  1188. // Timeout occurred
  1189. resolve_thread.detach(); // Let the thread finish in background
  1190. return EAI_AGAIN; // Return timeout error
  1191. }
  1192. #endif
  1193. #else
  1194. (void)(timeout_sec); // Unused parameter for non-blocking getaddrinfo
  1195. return getaddrinfo(node, service, hints, res);
  1196. #endif
  1197. }
  1198. template <typename BindOrConnect>
  1199. socket_t create_socket(const std::string &host, const std::string &ip, int port,
  1200. int address_family, int socket_flags, bool tcp_nodelay,
  1201. bool ipv6_v6only, SocketOptions socket_options,
  1202. BindOrConnect bind_or_connect, time_t timeout_sec = 0) {
  1203. // Get address info
  1204. const char *node = nullptr;
  1205. struct addrinfo hints;
  1206. struct addrinfo *result;
  1207. memset(&hints, 0, sizeof(struct addrinfo));
  1208. hints.ai_socktype = SOCK_STREAM;
  1209. hints.ai_protocol = IPPROTO_IP;
  1210. if (!ip.empty()) {
  1211. node = ip.c_str();
  1212. // Ask getaddrinfo to convert IP in c-string to address
  1213. hints.ai_family = AF_UNSPEC;
  1214. hints.ai_flags = AI_NUMERICHOST;
  1215. } else {
  1216. if (!host.empty()) { node = host.c_str(); }
  1217. hints.ai_family = address_family;
  1218. hints.ai_flags = socket_flags;
  1219. }
  1220. #if !defined(_WIN32) || defined(CPPHTTPLIB_HAVE_AFUNIX_H)
  1221. if (hints.ai_family == AF_UNIX) {
  1222. const auto addrlen = host.length();
  1223. if (addrlen > sizeof(sockaddr_un::sun_path)) { return INVALID_SOCKET; }
  1224. #ifdef SOCK_CLOEXEC
  1225. auto sock = socket(hints.ai_family, hints.ai_socktype | SOCK_CLOEXEC,
  1226. hints.ai_protocol);
  1227. #else
  1228. auto sock = socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol);
  1229. #endif
  1230. if (sock != INVALID_SOCKET) {
  1231. sockaddr_un addr{};
  1232. addr.sun_family = AF_UNIX;
  1233. auto unescaped_host = unescape_abstract_namespace_unix_domain(host);
  1234. std::copy(unescaped_host.begin(), unescaped_host.end(), addr.sun_path);
  1235. hints.ai_addr = reinterpret_cast<sockaddr *>(&addr);
  1236. hints.ai_addrlen = static_cast<socklen_t>(
  1237. sizeof(addr) - sizeof(addr.sun_path) + addrlen);
  1238. #ifndef SOCK_CLOEXEC
  1239. #ifndef _WIN32
  1240. fcntl(sock, F_SETFD, FD_CLOEXEC);
  1241. #endif
  1242. #endif
  1243. if (socket_options) { socket_options(sock); }
  1244. #ifdef _WIN32
  1245. // Setting SO_REUSEADDR seems not to work well with AF_UNIX on windows, so
  1246. // remove the option.
  1247. detail::set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 0);
  1248. #endif
  1249. bool dummy;
  1250. if (!bind_or_connect(sock, hints, dummy)) {
  1251. close_socket(sock);
  1252. sock = INVALID_SOCKET;
  1253. }
  1254. }
  1255. return sock;
  1256. }
  1257. #endif
  1258. auto service = std::to_string(port);
  1259. if (getaddrinfo_with_timeout(node, service.c_str(), &hints, &result,
  1260. timeout_sec)) {
  1261. #if defined __linux__ && !defined __ANDROID__
  1262. res_init();
  1263. #endif
  1264. return INVALID_SOCKET;
  1265. }
  1266. auto se = detail::scope_exit([&] { freeaddrinfo(result); });
  1267. for (auto rp = result; rp; rp = rp->ai_next) {
  1268. // Create a socket
  1269. #ifdef _WIN32
  1270. auto sock =
  1271. WSASocketW(rp->ai_family, rp->ai_socktype, rp->ai_protocol, nullptr, 0,
  1272. WSA_FLAG_NO_HANDLE_INHERIT | WSA_FLAG_OVERLAPPED);
  1273. /**
  1274. * Since the WSA_FLAG_NO_HANDLE_INHERIT is only supported on Windows 7 SP1
  1275. * and above the socket creation fails on older Windows Systems.
  1276. *
  1277. * Let's try to create a socket the old way in this case.
  1278. *
  1279. * Reference:
  1280. * https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsasocketa
  1281. *
  1282. * WSA_FLAG_NO_HANDLE_INHERIT:
  1283. * This flag is supported on Windows 7 with SP1, Windows Server 2008 R2 with
  1284. * SP1, and later
  1285. *
  1286. */
  1287. if (sock == INVALID_SOCKET) {
  1288. sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  1289. }
  1290. #else
  1291. #ifdef SOCK_CLOEXEC
  1292. auto sock =
  1293. socket(rp->ai_family, rp->ai_socktype | SOCK_CLOEXEC, rp->ai_protocol);
  1294. #else
  1295. auto sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  1296. #endif
  1297. #endif
  1298. if (sock == INVALID_SOCKET) { continue; }
  1299. #if !defined _WIN32 && !defined SOCK_CLOEXEC
  1300. if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1) {
  1301. close_socket(sock);
  1302. continue;
  1303. }
  1304. #endif
  1305. if (tcp_nodelay) { set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1); }
  1306. if (rp->ai_family == AF_INET6) {
  1307. set_socket_opt(sock, IPPROTO_IPV6, IPV6_V6ONLY, ipv6_v6only ? 1 : 0);
  1308. }
  1309. if (socket_options) { socket_options(sock); }
  1310. // bind or connect
  1311. auto quit = false;
  1312. if (bind_or_connect(sock, *rp, quit)) { return sock; }
  1313. close_socket(sock);
  1314. if (quit) { break; }
  1315. }
  1316. return INVALID_SOCKET;
  1317. }
  1318. void set_nonblocking(socket_t sock, bool nonblocking) {
  1319. #ifdef _WIN32
  1320. auto flags = nonblocking ? 1UL : 0UL;
  1321. ioctlsocket(sock, FIONBIO, &flags);
  1322. #else
  1323. auto flags = fcntl(sock, F_GETFL, 0);
  1324. fcntl(sock, F_SETFL,
  1325. nonblocking ? (flags | O_NONBLOCK) : (flags & (~O_NONBLOCK)));
  1326. #endif
  1327. }
  1328. bool is_connection_error() {
  1329. #ifdef _WIN32
  1330. return WSAGetLastError() != WSAEWOULDBLOCK;
  1331. #else
  1332. return errno != EINPROGRESS;
  1333. #endif
  1334. }
  1335. bool bind_ip_address(socket_t sock, const std::string &host) {
  1336. struct addrinfo hints;
  1337. struct addrinfo *result;
  1338. memset(&hints, 0, sizeof(struct addrinfo));
  1339. hints.ai_family = AF_UNSPEC;
  1340. hints.ai_socktype = SOCK_STREAM;
  1341. hints.ai_protocol = 0;
  1342. if (getaddrinfo_with_timeout(host.c_str(), "0", &hints, &result, 0)) {
  1343. return false;
  1344. }
  1345. auto se = detail::scope_exit([&] { freeaddrinfo(result); });
  1346. auto ret = false;
  1347. for (auto rp = result; rp; rp = rp->ai_next) {
  1348. const auto &ai = *rp;
  1349. if (!::bind(sock, ai.ai_addr, static_cast<socklen_t>(ai.ai_addrlen))) {
  1350. ret = true;
  1351. break;
  1352. }
  1353. }
  1354. return ret;
  1355. }
  1356. #if !defined _WIN32 && !defined ANDROID && !defined _AIX && !defined __MVS__
  1357. #define USE_IF2IP
  1358. #endif
  1359. #ifdef USE_IF2IP
  1360. std::string if2ip(int address_family, const std::string &ifn) {
  1361. struct ifaddrs *ifap;
  1362. getifaddrs(&ifap);
  1363. auto se = detail::scope_exit([&] { freeifaddrs(ifap); });
  1364. std::string addr_candidate;
  1365. for (auto ifa = ifap; ifa; ifa = ifa->ifa_next) {
  1366. if (ifa->ifa_addr && ifn == ifa->ifa_name &&
  1367. (AF_UNSPEC == address_family ||
  1368. ifa->ifa_addr->sa_family == address_family)) {
  1369. if (ifa->ifa_addr->sa_family == AF_INET) {
  1370. auto sa = reinterpret_cast<struct sockaddr_in *>(ifa->ifa_addr);
  1371. char buf[INET_ADDRSTRLEN];
  1372. if (inet_ntop(AF_INET, &sa->sin_addr, buf, INET_ADDRSTRLEN)) {
  1373. return std::string(buf, INET_ADDRSTRLEN);
  1374. }
  1375. } else if (ifa->ifa_addr->sa_family == AF_INET6) {
  1376. auto sa = reinterpret_cast<struct sockaddr_in6 *>(ifa->ifa_addr);
  1377. if (!IN6_IS_ADDR_LINKLOCAL(&sa->sin6_addr)) {
  1378. char buf[INET6_ADDRSTRLEN] = {};
  1379. if (inet_ntop(AF_INET6, &sa->sin6_addr, buf, INET6_ADDRSTRLEN)) {
  1380. // equivalent to mac's IN6_IS_ADDR_UNIQUE_LOCAL
  1381. auto s6_addr_head = sa->sin6_addr.s6_addr[0];
  1382. if (s6_addr_head == 0xfc || s6_addr_head == 0xfd) {
  1383. addr_candidate = std::string(buf, INET6_ADDRSTRLEN);
  1384. } else {
  1385. return std::string(buf, INET6_ADDRSTRLEN);
  1386. }
  1387. }
  1388. }
  1389. }
  1390. }
  1391. }
  1392. return addr_candidate;
  1393. }
  1394. #endif
  1395. socket_t create_client_socket(
  1396. const std::string &host, const std::string &ip, int port,
  1397. int address_family, bool tcp_nodelay, bool ipv6_v6only,
  1398. SocketOptions socket_options, time_t connection_timeout_sec,
  1399. time_t connection_timeout_usec, time_t read_timeout_sec,
  1400. time_t read_timeout_usec, time_t write_timeout_sec,
  1401. time_t write_timeout_usec, const std::string &intf, Error &error) {
  1402. auto sock = create_socket(
  1403. host, ip, port, address_family, 0, tcp_nodelay, ipv6_v6only,
  1404. std::move(socket_options),
  1405. [&](socket_t sock2, struct addrinfo &ai, bool &quit) -> bool {
  1406. if (!intf.empty()) {
  1407. #ifdef USE_IF2IP
  1408. auto ip_from_if = if2ip(address_family, intf);
  1409. if (ip_from_if.empty()) { ip_from_if = intf; }
  1410. if (!bind_ip_address(sock2, ip_from_if)) {
  1411. error = Error::BindIPAddress;
  1412. return false;
  1413. }
  1414. #endif
  1415. }
  1416. set_nonblocking(sock2, true);
  1417. auto ret =
  1418. ::connect(sock2, ai.ai_addr, static_cast<socklen_t>(ai.ai_addrlen));
  1419. if (ret < 0) {
  1420. if (is_connection_error()) {
  1421. error = Error::Connection;
  1422. return false;
  1423. }
  1424. error = wait_until_socket_is_ready(sock2, connection_timeout_sec,
  1425. connection_timeout_usec);
  1426. if (error != Error::Success) {
  1427. if (error == Error::ConnectionTimeout) { quit = true; }
  1428. return false;
  1429. }
  1430. }
  1431. set_nonblocking(sock2, false);
  1432. set_socket_opt_time(sock2, SOL_SOCKET, SO_RCVTIMEO, read_timeout_sec,
  1433. read_timeout_usec);
  1434. set_socket_opt_time(sock2, SOL_SOCKET, SO_SNDTIMEO, write_timeout_sec,
  1435. write_timeout_usec);
  1436. error = Error::Success;
  1437. return true;
  1438. },
  1439. connection_timeout_sec); // Pass DNS timeout
  1440. if (sock != INVALID_SOCKET) {
  1441. error = Error::Success;
  1442. } else {
  1443. if (error == Error::Success) { error = Error::Connection; }
  1444. }
  1445. return sock;
  1446. }
  1447. bool get_ip_and_port(const struct sockaddr_storage &addr,
  1448. socklen_t addr_len, std::string &ip, int &port) {
  1449. if (addr.ss_family == AF_INET) {
  1450. port = ntohs(reinterpret_cast<const struct sockaddr_in *>(&addr)->sin_port);
  1451. } else if (addr.ss_family == AF_INET6) {
  1452. port =
  1453. ntohs(reinterpret_cast<const struct sockaddr_in6 *>(&addr)->sin6_port);
  1454. } else {
  1455. return false;
  1456. }
  1457. std::array<char, NI_MAXHOST> ipstr{};
  1458. if (getnameinfo(reinterpret_cast<const struct sockaddr *>(&addr), addr_len,
  1459. ipstr.data(), static_cast<socklen_t>(ipstr.size()), nullptr,
  1460. 0, NI_NUMERICHOST)) {
  1461. return false;
  1462. }
  1463. ip = ipstr.data();
  1464. return true;
  1465. }
  1466. void get_local_ip_and_port(socket_t sock, std::string &ip, int &port) {
  1467. struct sockaddr_storage addr;
  1468. socklen_t addr_len = sizeof(addr);
  1469. if (!getsockname(sock, reinterpret_cast<struct sockaddr *>(&addr),
  1470. &addr_len)) {
  1471. get_ip_and_port(addr, addr_len, ip, port);
  1472. }
  1473. }
  1474. void get_remote_ip_and_port(socket_t sock, std::string &ip, int &port) {
  1475. struct sockaddr_storage addr;
  1476. socklen_t addr_len = sizeof(addr);
  1477. if (!getpeername(sock, reinterpret_cast<struct sockaddr *>(&addr),
  1478. &addr_len)) {
  1479. #ifndef _WIN32
  1480. if (addr.ss_family == AF_UNIX) {
  1481. #if defined(__linux__)
  1482. struct ucred ucred;
  1483. socklen_t len = sizeof(ucred);
  1484. if (getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &len) == 0) {
  1485. port = ucred.pid;
  1486. }
  1487. #elif defined(SOL_LOCAL) && defined(SO_PEERPID)
  1488. pid_t pid;
  1489. socklen_t len = sizeof(pid);
  1490. if (getsockopt(sock, SOL_LOCAL, SO_PEERPID, &pid, &len) == 0) {
  1491. port = pid;
  1492. }
  1493. #endif
  1494. return;
  1495. }
  1496. #endif
  1497. get_ip_and_port(addr, addr_len, ip, port);
  1498. }
  1499. }
  1500. constexpr unsigned int str2tag_core(const char *s, size_t l,
  1501. unsigned int h) {
  1502. return (l == 0)
  1503. ? h
  1504. : str2tag_core(
  1505. s + 1, l - 1,
  1506. // Unsets the 6 high bits of h, therefore no overflow happens
  1507. (((std::numeric_limits<unsigned int>::max)() >> 6) &
  1508. h * 33) ^
  1509. static_cast<unsigned char>(*s));
  1510. }
  1511. unsigned int str2tag(const std::string &s) {
  1512. return str2tag_core(s.data(), s.size(), 0);
  1513. }
  1514. namespace udl {
  1515. constexpr unsigned int operator""_t(const char *s, size_t l) {
  1516. return str2tag_core(s, l, 0);
  1517. }
  1518. } // namespace udl
  1519. std::string
  1520. find_content_type(const std::string &path,
  1521. const std::map<std::string, std::string> &user_data,
  1522. const std::string &default_content_type) {
  1523. auto ext = file_extension(path);
  1524. auto it = user_data.find(ext);
  1525. if (it != user_data.end()) { return it->second; }
  1526. using udl::operator""_t;
  1527. switch (str2tag(ext)) {
  1528. default: return default_content_type;
  1529. case "css"_t: return "text/css";
  1530. case "csv"_t: return "text/csv";
  1531. case "htm"_t:
  1532. case "html"_t: return "text/html";
  1533. case "js"_t:
  1534. case "mjs"_t: return "text/javascript";
  1535. case "txt"_t: return "text/plain";
  1536. case "vtt"_t: return "text/vtt";
  1537. case "apng"_t: return "image/apng";
  1538. case "avif"_t: return "image/avif";
  1539. case "bmp"_t: return "image/bmp";
  1540. case "gif"_t: return "image/gif";
  1541. case "png"_t: return "image/png";
  1542. case "svg"_t: return "image/svg+xml";
  1543. case "webp"_t: return "image/webp";
  1544. case "ico"_t: return "image/x-icon";
  1545. case "tif"_t: return "image/tiff";
  1546. case "tiff"_t: return "image/tiff";
  1547. case "jpg"_t:
  1548. case "jpeg"_t: return "image/jpeg";
  1549. case "mp4"_t: return "video/mp4";
  1550. case "mpeg"_t: return "video/mpeg";
  1551. case "webm"_t: return "video/webm";
  1552. case "mp3"_t: return "audio/mp3";
  1553. case "mpga"_t: return "audio/mpeg";
  1554. case "weba"_t: return "audio/webm";
  1555. case "wav"_t: return "audio/wave";
  1556. case "otf"_t: return "font/otf";
  1557. case "ttf"_t: return "font/ttf";
  1558. case "woff"_t: return "font/woff";
  1559. case "woff2"_t: return "font/woff2";
  1560. case "7z"_t: return "application/x-7z-compressed";
  1561. case "atom"_t: return "application/atom+xml";
  1562. case "pdf"_t: return "application/pdf";
  1563. case "json"_t: return "application/json";
  1564. case "rss"_t: return "application/rss+xml";
  1565. case "tar"_t: return "application/x-tar";
  1566. case "xht"_t:
  1567. case "xhtml"_t: return "application/xhtml+xml";
  1568. case "xslt"_t: return "application/xslt+xml";
  1569. case "xml"_t: return "application/xml";
  1570. case "gz"_t: return "application/gzip";
  1571. case "zip"_t: return "application/zip";
  1572. case "wasm"_t: return "application/wasm";
  1573. }
  1574. }
  1575. bool can_compress_content_type(const std::string &content_type) {
  1576. using udl::operator""_t;
  1577. auto tag = str2tag(content_type);
  1578. switch (tag) {
  1579. case "image/svg+xml"_t:
  1580. case "application/javascript"_t:
  1581. case "application/json"_t:
  1582. case "application/xml"_t:
  1583. case "application/protobuf"_t:
  1584. case "application/xhtml+xml"_t: return true;
  1585. case "text/event-stream"_t: return false;
  1586. default: return !content_type.rfind("text/", 0);
  1587. }
  1588. }
  1589. EncodingType encoding_type(const Request &req, const Response &res) {
  1590. auto ret =
  1591. detail::can_compress_content_type(res.get_header_value("Content-Type"));
  1592. if (!ret) { return EncodingType::None; }
  1593. const auto &s = req.get_header_value("Accept-Encoding");
  1594. (void)(s);
  1595. #ifdef CPPHTTPLIB_BROTLI_SUPPORT
  1596. // TODO: 'Accept-Encoding' has br, not br;q=0
  1597. ret = s.find("br") != std::string::npos;
  1598. if (ret) { return EncodingType::Brotli; }
  1599. #endif
  1600. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  1601. // TODO: 'Accept-Encoding' has gzip, not gzip;q=0
  1602. ret = s.find("gzip") != std::string::npos;
  1603. if (ret) { return EncodingType::Gzip; }
  1604. #endif
  1605. #ifdef CPPHTTPLIB_ZSTD_SUPPORT
  1606. // TODO: 'Accept-Encoding' has zstd, not zstd;q=0
  1607. ret = s.find("zstd") != std::string::npos;
  1608. if (ret) { return EncodingType::Zstd; }
  1609. #endif
  1610. return EncodingType::None;
  1611. }
  1612. bool nocompressor::compress(const char *data, size_t data_length,
  1613. bool /*last*/, Callback callback) {
  1614. if (!data_length) { return true; }
  1615. return callback(data, data_length);
  1616. }
  1617. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  1618. gzip_compressor::gzip_compressor() {
  1619. std::memset(&strm_, 0, sizeof(strm_));
  1620. strm_.zalloc = Z_NULL;
  1621. strm_.zfree = Z_NULL;
  1622. strm_.opaque = Z_NULL;
  1623. is_valid_ = deflateInit2(&strm_, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 31, 8,
  1624. Z_DEFAULT_STRATEGY) == Z_OK;
  1625. }
  1626. gzip_compressor::~gzip_compressor() { deflateEnd(&strm_); }
  1627. bool gzip_compressor::compress(const char *data, size_t data_length,
  1628. bool last, Callback callback) {
  1629. assert(is_valid_);
  1630. do {
  1631. constexpr size_t max_avail_in =
  1632. (std::numeric_limits<decltype(strm_.avail_in)>::max)();
  1633. strm_.avail_in = static_cast<decltype(strm_.avail_in)>(
  1634. (std::min)(data_length, max_avail_in));
  1635. strm_.next_in = const_cast<Bytef *>(reinterpret_cast<const Bytef *>(data));
  1636. data_length -= strm_.avail_in;
  1637. data += strm_.avail_in;
  1638. auto flush = (last && data_length == 0) ? Z_FINISH : Z_NO_FLUSH;
  1639. auto ret = Z_OK;
  1640. std::array<char, CPPHTTPLIB_COMPRESSION_BUFSIZ> buff{};
  1641. do {
  1642. strm_.avail_out = static_cast<uInt>(buff.size());
  1643. strm_.next_out = reinterpret_cast<Bytef *>(buff.data());
  1644. ret = deflate(&strm_, flush);
  1645. if (ret == Z_STREAM_ERROR) { return false; }
  1646. if (!callback(buff.data(), buff.size() - strm_.avail_out)) {
  1647. return false;
  1648. }
  1649. } while (strm_.avail_out == 0);
  1650. assert((flush == Z_FINISH && ret == Z_STREAM_END) ||
  1651. (flush == Z_NO_FLUSH && ret == Z_OK));
  1652. assert(strm_.avail_in == 0);
  1653. } while (data_length > 0);
  1654. return true;
  1655. }
  1656. gzip_decompressor::gzip_decompressor() {
  1657. std::memset(&strm_, 0, sizeof(strm_));
  1658. strm_.zalloc = Z_NULL;
  1659. strm_.zfree = Z_NULL;
  1660. strm_.opaque = Z_NULL;
  1661. // 15 is the value of wbits, which should be at the maximum possible value
  1662. // to ensure that any gzip stream can be decoded. The offset of 32 specifies
  1663. // that the stream type should be automatically detected either gzip or
  1664. // deflate.
  1665. is_valid_ = inflateInit2(&strm_, 32 + 15) == Z_OK;
  1666. }
  1667. gzip_decompressor::~gzip_decompressor() { inflateEnd(&strm_); }
  1668. bool gzip_decompressor::is_valid() const { return is_valid_; }
  1669. bool gzip_decompressor::decompress(const char *data, size_t data_length,
  1670. Callback callback) {
  1671. assert(is_valid_);
  1672. auto ret = Z_OK;
  1673. do {
  1674. constexpr size_t max_avail_in =
  1675. (std::numeric_limits<decltype(strm_.avail_in)>::max)();
  1676. strm_.avail_in = static_cast<decltype(strm_.avail_in)>(
  1677. (std::min)(data_length, max_avail_in));
  1678. strm_.next_in = const_cast<Bytef *>(reinterpret_cast<const Bytef *>(data));
  1679. data_length -= strm_.avail_in;
  1680. data += strm_.avail_in;
  1681. std::array<char, CPPHTTPLIB_COMPRESSION_BUFSIZ> buff{};
  1682. while (strm_.avail_in > 0 && ret == Z_OK) {
  1683. strm_.avail_out = static_cast<uInt>(buff.size());
  1684. strm_.next_out = reinterpret_cast<Bytef *>(buff.data());
  1685. ret = inflate(&strm_, Z_NO_FLUSH);
  1686. assert(ret != Z_STREAM_ERROR);
  1687. switch (ret) {
  1688. case Z_NEED_DICT:
  1689. case Z_DATA_ERROR:
  1690. case Z_MEM_ERROR: inflateEnd(&strm_); return false;
  1691. }
  1692. if (!callback(buff.data(), buff.size() - strm_.avail_out)) {
  1693. return false;
  1694. }
  1695. }
  1696. if (ret != Z_OK && ret != Z_STREAM_END) { return false; }
  1697. } while (data_length > 0);
  1698. return true;
  1699. }
  1700. #endif
  1701. #ifdef CPPHTTPLIB_BROTLI_SUPPORT
  1702. brotli_compressor::brotli_compressor() {
  1703. state_ = BrotliEncoderCreateInstance(nullptr, nullptr, nullptr);
  1704. }
  1705. brotli_compressor::~brotli_compressor() {
  1706. BrotliEncoderDestroyInstance(state_);
  1707. }
  1708. bool brotli_compressor::compress(const char *data, size_t data_length,
  1709. bool last, Callback callback) {
  1710. std::array<uint8_t, CPPHTTPLIB_COMPRESSION_BUFSIZ> buff{};
  1711. auto operation = last ? BROTLI_OPERATION_FINISH : BROTLI_OPERATION_PROCESS;
  1712. auto available_in = data_length;
  1713. auto next_in = reinterpret_cast<const uint8_t *>(data);
  1714. for (;;) {
  1715. if (last) {
  1716. if (BrotliEncoderIsFinished(state_)) { break; }
  1717. } else {
  1718. if (!available_in) { break; }
  1719. }
  1720. auto available_out = buff.size();
  1721. auto next_out = buff.data();
  1722. if (!BrotliEncoderCompressStream(state_, operation, &available_in, &next_in,
  1723. &available_out, &next_out, nullptr)) {
  1724. return false;
  1725. }
  1726. auto output_bytes = buff.size() - available_out;
  1727. if (output_bytes) {
  1728. callback(reinterpret_cast<const char *>(buff.data()), output_bytes);
  1729. }
  1730. }
  1731. return true;
  1732. }
  1733. brotli_decompressor::brotli_decompressor() {
  1734. decoder_s = BrotliDecoderCreateInstance(0, 0, 0);
  1735. decoder_r = decoder_s ? BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT
  1736. : BROTLI_DECODER_RESULT_ERROR;
  1737. }
  1738. brotli_decompressor::~brotli_decompressor() {
  1739. if (decoder_s) { BrotliDecoderDestroyInstance(decoder_s); }
  1740. }
  1741. bool brotli_decompressor::is_valid() const { return decoder_s; }
  1742. bool brotli_decompressor::decompress(const char *data,
  1743. size_t data_length,
  1744. Callback callback) {
  1745. if (decoder_r == BROTLI_DECODER_RESULT_SUCCESS ||
  1746. decoder_r == BROTLI_DECODER_RESULT_ERROR) {
  1747. return 0;
  1748. }
  1749. auto next_in = reinterpret_cast<const uint8_t *>(data);
  1750. size_t avail_in = data_length;
  1751. size_t total_out;
  1752. decoder_r = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT;
  1753. std::array<char, CPPHTTPLIB_COMPRESSION_BUFSIZ> buff{};
  1754. while (decoder_r == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) {
  1755. char *next_out = buff.data();
  1756. size_t avail_out = buff.size();
  1757. decoder_r = BrotliDecoderDecompressStream(
  1758. decoder_s, &avail_in, &next_in, &avail_out,
  1759. reinterpret_cast<uint8_t **>(&next_out), &total_out);
  1760. if (decoder_r == BROTLI_DECODER_RESULT_ERROR) { return false; }
  1761. if (!callback(buff.data(), buff.size() - avail_out)) { return false; }
  1762. }
  1763. return decoder_r == BROTLI_DECODER_RESULT_SUCCESS ||
  1764. decoder_r == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT;
  1765. }
  1766. #endif
  1767. #ifdef CPPHTTPLIB_ZSTD_SUPPORT
  1768. zstd_compressor::zstd_compressor() {
  1769. ctx_ = ZSTD_createCCtx();
  1770. ZSTD_CCtx_setParameter(ctx_, ZSTD_c_compressionLevel, ZSTD_fast);
  1771. }
  1772. zstd_compressor::~zstd_compressor() { ZSTD_freeCCtx(ctx_); }
  1773. bool zstd_compressor::compress(const char *data, size_t data_length,
  1774. bool last, Callback callback) {
  1775. std::array<char, CPPHTTPLIB_COMPRESSION_BUFSIZ> buff{};
  1776. ZSTD_EndDirective mode = last ? ZSTD_e_end : ZSTD_e_continue;
  1777. ZSTD_inBuffer input = {data, data_length, 0};
  1778. bool finished;
  1779. do {
  1780. ZSTD_outBuffer output = {buff.data(), CPPHTTPLIB_COMPRESSION_BUFSIZ, 0};
  1781. size_t const remaining = ZSTD_compressStream2(ctx_, &output, &input, mode);
  1782. if (ZSTD_isError(remaining)) { return false; }
  1783. if (!callback(buff.data(), output.pos)) { return false; }
  1784. finished = last ? (remaining == 0) : (input.pos == input.size);
  1785. } while (!finished);
  1786. return true;
  1787. }
  1788. zstd_decompressor::zstd_decompressor() { ctx_ = ZSTD_createDCtx(); }
  1789. zstd_decompressor::~zstd_decompressor() { ZSTD_freeDCtx(ctx_); }
  1790. bool zstd_decompressor::is_valid() const { return ctx_ != nullptr; }
  1791. bool zstd_decompressor::decompress(const char *data, size_t data_length,
  1792. Callback callback) {
  1793. std::array<char, CPPHTTPLIB_COMPRESSION_BUFSIZ> buff{};
  1794. ZSTD_inBuffer input = {data, data_length, 0};
  1795. while (input.pos < input.size) {
  1796. ZSTD_outBuffer output = {buff.data(), CPPHTTPLIB_COMPRESSION_BUFSIZ, 0};
  1797. size_t const remaining = ZSTD_decompressStream(ctx_, &output, &input);
  1798. if (ZSTD_isError(remaining)) { return false; }
  1799. if (!callback(buff.data(), output.pos)) { return false; }
  1800. }
  1801. return true;
  1802. }
  1803. #endif
  1804. std::unique_ptr<decompressor>
  1805. create_decompressor(const std::string &encoding) {
  1806. std::unique_ptr<decompressor> decompressor;
  1807. if (encoding == "gzip" || encoding == "deflate") {
  1808. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  1809. decompressor = detail::make_unique<gzip_decompressor>();
  1810. #endif
  1811. } else if (encoding.find("br") != std::string::npos) {
  1812. #ifdef CPPHTTPLIB_BROTLI_SUPPORT
  1813. decompressor = detail::make_unique<brotli_decompressor>();
  1814. #endif
  1815. } else if (encoding == "zstd" || encoding.find("zstd") != std::string::npos) {
  1816. #ifdef CPPHTTPLIB_ZSTD_SUPPORT
  1817. decompressor = detail::make_unique<zstd_decompressor>();
  1818. #endif
  1819. }
  1820. return decompressor;
  1821. }
  1822. bool is_prohibited_header_name(const std::string &name) {
  1823. using udl::operator""_t;
  1824. switch (str2tag(name)) {
  1825. case "REMOTE_ADDR"_t:
  1826. case "REMOTE_PORT"_t:
  1827. case "LOCAL_ADDR"_t:
  1828. case "LOCAL_PORT"_t: return true;
  1829. default: return false;
  1830. }
  1831. }
  1832. bool has_header(const Headers &headers, const std::string &key) {
  1833. if (is_prohibited_header_name(key)) { return false; }
  1834. return headers.find(key) != headers.end();
  1835. }
  1836. const char *get_header_value(const Headers &headers,
  1837. const std::string &key, const char *def,
  1838. size_t id) {
  1839. if (is_prohibited_header_name(key)) {
  1840. #ifndef CPPHTTPLIB_NO_EXCEPTIONS
  1841. std::string msg = "Prohibited header name '" + key + "' is specified.";
  1842. throw std::invalid_argument(msg);
  1843. #else
  1844. return "";
  1845. #endif
  1846. }
  1847. auto rng = headers.equal_range(key);
  1848. auto it = rng.first;
  1849. std::advance(it, static_cast<ssize_t>(id));
  1850. if (it != rng.second) { return it->second.c_str(); }
  1851. return def;
  1852. }
  1853. bool read_headers(Stream &strm, Headers &headers) {
  1854. const auto bufsiz = 2048;
  1855. char buf[bufsiz];
  1856. stream_line_reader line_reader(strm, buf, bufsiz);
  1857. size_t header_count = 0;
  1858. for (;;) {
  1859. if (!line_reader.getline()) { return false; }
  1860. // Check if the line ends with CRLF.
  1861. auto line_terminator_len = 2;
  1862. if (line_reader.end_with_crlf()) {
  1863. // Blank line indicates end of headers.
  1864. if (line_reader.size() == 2) { break; }
  1865. } else {
  1866. #ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR
  1867. // Blank line indicates end of headers.
  1868. if (line_reader.size() == 1) { break; }
  1869. line_terminator_len = 1;
  1870. #else
  1871. continue; // Skip invalid line.
  1872. #endif
  1873. }
  1874. if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; }
  1875. // Check header count limit
  1876. if (header_count >= CPPHTTPLIB_HEADER_MAX_COUNT) { return false; }
  1877. // Exclude line terminator
  1878. auto end = line_reader.ptr() + line_reader.size() - line_terminator_len;
  1879. if (!parse_header(line_reader.ptr(), end,
  1880. [&](const std::string &key, const std::string &val) {
  1881. headers.emplace(key, val);
  1882. })) {
  1883. return false;
  1884. }
  1885. header_count++;
  1886. }
  1887. return true;
  1888. }
  1889. bool read_content_with_length(Stream &strm, size_t len,
  1890. DownloadProgress progress,
  1891. ContentReceiverWithProgress out) {
  1892. char buf[CPPHTTPLIB_RECV_BUFSIZ];
  1893. detail::BodyReader br;
  1894. br.stream = &strm;
  1895. br.content_length = len;
  1896. br.chunked = false;
  1897. br.bytes_read = 0;
  1898. br.last_error = Error::Success;
  1899. size_t r = 0;
  1900. while (r < len) {
  1901. auto read_len = static_cast<size_t>(len - r);
  1902. auto to_read = (std::min)(read_len, CPPHTTPLIB_RECV_BUFSIZ);
  1903. auto n = detail::read_body_content(&strm, br, buf, to_read);
  1904. if (n <= 0) { return false; }
  1905. if (!out(buf, static_cast<size_t>(n), r, len)) { return false; }
  1906. r += static_cast<size_t>(n);
  1907. if (progress) {
  1908. if (!progress(r, len)) { return false; }
  1909. }
  1910. }
  1911. return true;
  1912. }
  1913. void skip_content_with_length(Stream &strm, size_t len) {
  1914. char buf[CPPHTTPLIB_RECV_BUFSIZ];
  1915. size_t r = 0;
  1916. while (r < len) {
  1917. auto read_len = static_cast<size_t>(len - r);
  1918. auto n = strm.read(buf, (std::min)(read_len, CPPHTTPLIB_RECV_BUFSIZ));
  1919. if (n <= 0) { return; }
  1920. r += static_cast<size_t>(n);
  1921. }
  1922. }
  1923. enum class ReadContentResult {
  1924. Success, // Successfully read the content
  1925. PayloadTooLarge, // The content exceeds the specified payload limit
  1926. Error // An error occurred while reading the content
  1927. };
  1928. ReadContentResult
  1929. read_content_without_length(Stream &strm, size_t payload_max_length,
  1930. ContentReceiverWithProgress out) {
  1931. char buf[CPPHTTPLIB_RECV_BUFSIZ];
  1932. size_t r = 0;
  1933. for (;;) {
  1934. auto n = strm.read(buf, CPPHTTPLIB_RECV_BUFSIZ);
  1935. if (n == 0) { return ReadContentResult::Success; }
  1936. if (n < 0) { return ReadContentResult::Error; }
  1937. // Check if adding this data would exceed the payload limit
  1938. if (r > payload_max_length ||
  1939. payload_max_length - r < static_cast<size_t>(n)) {
  1940. return ReadContentResult::PayloadTooLarge;
  1941. }
  1942. if (!out(buf, static_cast<size_t>(n), r, 0)) {
  1943. return ReadContentResult::Error;
  1944. }
  1945. r += static_cast<size_t>(n);
  1946. }
  1947. return ReadContentResult::Success;
  1948. }
  1949. template <typename T>
  1950. ReadContentResult read_content_chunked(Stream &strm, T &x,
  1951. size_t payload_max_length,
  1952. ContentReceiverWithProgress out) {
  1953. detail::ChunkedDecoder dec(strm);
  1954. char buf[CPPHTTPLIB_RECV_BUFSIZ];
  1955. size_t total_len = 0;
  1956. for (;;) {
  1957. size_t chunk_offset = 0;
  1958. size_t chunk_total = 0;
  1959. auto n = dec.read_payload(buf, sizeof(buf), chunk_offset, chunk_total);
  1960. if (n < 0) { return ReadContentResult::Error; }
  1961. if (n == 0) {
  1962. if (!dec.parse_trailers_into(x.trailers, x.headers)) {
  1963. return ReadContentResult::Error;
  1964. }
  1965. return ReadContentResult::Success;
  1966. }
  1967. if (total_len > payload_max_length ||
  1968. payload_max_length - total_len < static_cast<size_t>(n)) {
  1969. return ReadContentResult::PayloadTooLarge;
  1970. }
  1971. if (!out(buf, static_cast<size_t>(n), chunk_offset, chunk_total)) {
  1972. return ReadContentResult::Error;
  1973. }
  1974. total_len += static_cast<size_t>(n);
  1975. }
  1976. }
  1977. bool is_chunked_transfer_encoding(const Headers &headers) {
  1978. return case_ignore::equal(
  1979. get_header_value(headers, "Transfer-Encoding", "", 0), "chunked");
  1980. }
  1981. template <typename T, typename U>
  1982. bool prepare_content_receiver(T &x, int &status,
  1983. ContentReceiverWithProgress receiver,
  1984. bool decompress, U callback) {
  1985. if (decompress) {
  1986. std::string encoding = x.get_header_value("Content-Encoding");
  1987. std::unique_ptr<decompressor> decompressor;
  1988. if (!encoding.empty()) {
  1989. decompressor = detail::create_decompressor(encoding);
  1990. if (!decompressor) {
  1991. // Unsupported encoding or no support compiled in
  1992. status = StatusCode::UnsupportedMediaType_415;
  1993. return false;
  1994. }
  1995. }
  1996. if (decompressor) {
  1997. if (decompressor->is_valid()) {
  1998. ContentReceiverWithProgress out = [&](const char *buf, size_t n,
  1999. size_t off, size_t len) {
  2000. return decompressor->decompress(buf, n,
  2001. [&](const char *buf2, size_t n2) {
  2002. return receiver(buf2, n2, off, len);
  2003. });
  2004. };
  2005. return callback(std::move(out));
  2006. } else {
  2007. status = StatusCode::InternalServerError_500;
  2008. return false;
  2009. }
  2010. }
  2011. }
  2012. ContentReceiverWithProgress out = [&](const char *buf, size_t n, size_t off,
  2013. size_t len) {
  2014. return receiver(buf, n, off, len);
  2015. };
  2016. return callback(std::move(out));
  2017. }
  2018. template <typename T>
  2019. bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status,
  2020. DownloadProgress progress,
  2021. ContentReceiverWithProgress receiver, bool decompress) {
  2022. return prepare_content_receiver(
  2023. x, status, std::move(receiver), decompress,
  2024. [&](const ContentReceiverWithProgress &out) {
  2025. auto ret = true;
  2026. auto exceed_payload_max_length = false;
  2027. if (is_chunked_transfer_encoding(x.headers)) {
  2028. auto result = read_content_chunked(strm, x, payload_max_length, out);
  2029. if (result == ReadContentResult::Success) {
  2030. ret = true;
  2031. } else if (result == ReadContentResult::PayloadTooLarge) {
  2032. exceed_payload_max_length = true;
  2033. ret = false;
  2034. } else {
  2035. ret = false;
  2036. }
  2037. } else if (!has_header(x.headers, "Content-Length")) {
  2038. auto result =
  2039. read_content_without_length(strm, payload_max_length, out);
  2040. if (result == ReadContentResult::Success) {
  2041. ret = true;
  2042. } else if (result == ReadContentResult::PayloadTooLarge) {
  2043. exceed_payload_max_length = true;
  2044. ret = false;
  2045. } else {
  2046. ret = false;
  2047. }
  2048. } else {
  2049. auto is_invalid_value = false;
  2050. auto len = get_header_value_u64(x.headers, "Content-Length",
  2051. (std::numeric_limits<size_t>::max)(),
  2052. 0, is_invalid_value);
  2053. if (is_invalid_value) {
  2054. ret = false;
  2055. } else if (len > payload_max_length) {
  2056. exceed_payload_max_length = true;
  2057. skip_content_with_length(strm, len);
  2058. ret = false;
  2059. } else if (len > 0) {
  2060. ret = read_content_with_length(strm, len, std::move(progress), out);
  2061. }
  2062. }
  2063. if (!ret) {
  2064. status = exceed_payload_max_length ? StatusCode::PayloadTooLarge_413
  2065. : StatusCode::BadRequest_400;
  2066. }
  2067. return ret;
  2068. });
  2069. }
  2070. ssize_t write_request_line(Stream &strm, const std::string &method,
  2071. const std::string &path) {
  2072. std::string s = method;
  2073. s += ' ';
  2074. s += path;
  2075. s += " HTTP/1.1\r\n";
  2076. return strm.write(s.data(), s.size());
  2077. }
  2078. ssize_t write_response_line(Stream &strm, int status) {
  2079. std::string s = "HTTP/1.1 ";
  2080. s += std::to_string(status);
  2081. s += ' ';
  2082. s += httplib::status_message(status);
  2083. s += "\r\n";
  2084. return strm.write(s.data(), s.size());
  2085. }
  2086. ssize_t write_headers(Stream &strm, const Headers &headers) {
  2087. ssize_t write_len = 0;
  2088. for (const auto &x : headers) {
  2089. std::string s;
  2090. s = x.first;
  2091. s += ": ";
  2092. s += x.second;
  2093. s += "\r\n";
  2094. auto len = strm.write(s.data(), s.size());
  2095. if (len < 0) { return len; }
  2096. write_len += len;
  2097. }
  2098. auto len = strm.write("\r\n");
  2099. if (len < 0) { return len; }
  2100. write_len += len;
  2101. return write_len;
  2102. }
  2103. bool write_data(Stream &strm, const char *d, size_t l) {
  2104. size_t offset = 0;
  2105. while (offset < l) {
  2106. auto length = strm.write(d + offset, l - offset);
  2107. if (length < 0) { return false; }
  2108. offset += static_cast<size_t>(length);
  2109. }
  2110. return true;
  2111. }
  2112. template <typename T>
  2113. bool write_content_with_progress(Stream &strm,
  2114. const ContentProvider &content_provider,
  2115. size_t offset, size_t length,
  2116. T is_shutting_down,
  2117. const UploadProgress &upload_progress,
  2118. Error &error) {
  2119. size_t end_offset = offset + length;
  2120. size_t start_offset = offset;
  2121. auto ok = true;
  2122. DataSink data_sink;
  2123. data_sink.write = [&](const char *d, size_t l) -> bool {
  2124. if (ok) {
  2125. if (write_data(strm, d, l)) {
  2126. offset += l;
  2127. if (upload_progress && length > 0) {
  2128. size_t current_written = offset - start_offset;
  2129. if (!upload_progress(current_written, length)) {
  2130. ok = false;
  2131. return false;
  2132. }
  2133. }
  2134. } else {
  2135. ok = false;
  2136. }
  2137. }
  2138. return ok;
  2139. };
  2140. data_sink.is_writable = [&]() -> bool { return strm.wait_writable(); };
  2141. while (offset < end_offset && !is_shutting_down()) {
  2142. if (!strm.wait_writable()) {
  2143. error = Error::Write;
  2144. return false;
  2145. } else if (!content_provider(offset, end_offset - offset, data_sink)) {
  2146. error = Error::Canceled;
  2147. return false;
  2148. } else if (!ok) {
  2149. error = Error::Write;
  2150. return false;
  2151. }
  2152. }
  2153. error = Error::Success;
  2154. return true;
  2155. }
  2156. template <typename T>
  2157. bool write_content(Stream &strm, const ContentProvider &content_provider,
  2158. size_t offset, size_t length, T is_shutting_down,
  2159. Error &error) {
  2160. return write_content_with_progress<T>(strm, content_provider, offset, length,
  2161. is_shutting_down, nullptr, error);
  2162. }
  2163. template <typename T>
  2164. bool write_content(Stream &strm, const ContentProvider &content_provider,
  2165. size_t offset, size_t length,
  2166. const T &is_shutting_down) {
  2167. auto error = Error::Success;
  2168. return write_content(strm, content_provider, offset, length, is_shutting_down,
  2169. error);
  2170. }
  2171. template <typename T>
  2172. bool
  2173. write_content_without_length(Stream &strm,
  2174. const ContentProvider &content_provider,
  2175. const T &is_shutting_down) {
  2176. size_t offset = 0;
  2177. auto data_available = true;
  2178. auto ok = true;
  2179. DataSink data_sink;
  2180. data_sink.write = [&](const char *d, size_t l) -> bool {
  2181. if (ok) {
  2182. offset += l;
  2183. if (!write_data(strm, d, l)) { ok = false; }
  2184. }
  2185. return ok;
  2186. };
  2187. data_sink.is_writable = [&]() -> bool { return strm.wait_writable(); };
  2188. data_sink.done = [&](void) { data_available = false; };
  2189. while (data_available && !is_shutting_down()) {
  2190. if (!strm.wait_writable()) {
  2191. return false;
  2192. } else if (!content_provider(offset, 0, data_sink)) {
  2193. return false;
  2194. } else if (!ok) {
  2195. return false;
  2196. }
  2197. }
  2198. return true;
  2199. }
  2200. template <typename T, typename U>
  2201. bool
  2202. write_content_chunked(Stream &strm, const ContentProvider &content_provider,
  2203. const T &is_shutting_down, U &compressor, Error &error) {
  2204. size_t offset = 0;
  2205. auto data_available = true;
  2206. auto ok = true;
  2207. DataSink data_sink;
  2208. data_sink.write = [&](const char *d, size_t l) -> bool {
  2209. if (ok) {
  2210. data_available = l > 0;
  2211. offset += l;
  2212. std::string payload;
  2213. if (compressor.compress(d, l, false,
  2214. [&](const char *data, size_t data_len) {
  2215. payload.append(data, data_len);
  2216. return true;
  2217. })) {
  2218. if (!payload.empty()) {
  2219. // Emit chunked response header and footer for each chunk
  2220. auto chunk =
  2221. from_i_to_hex(payload.size()) + "\r\n" + payload + "\r\n";
  2222. if (!write_data(strm, chunk.data(), chunk.size())) { ok = false; }
  2223. }
  2224. } else {
  2225. ok = false;
  2226. }
  2227. }
  2228. return ok;
  2229. };
  2230. data_sink.is_writable = [&]() -> bool { return strm.wait_writable(); };
  2231. auto done_with_trailer = [&](const Headers *trailer) {
  2232. if (!ok) { return; }
  2233. data_available = false;
  2234. std::string payload;
  2235. if (!compressor.compress(nullptr, 0, true,
  2236. [&](const char *data, size_t data_len) {
  2237. payload.append(data, data_len);
  2238. return true;
  2239. })) {
  2240. ok = false;
  2241. return;
  2242. }
  2243. if (!payload.empty()) {
  2244. // Emit chunked response header and footer for each chunk
  2245. auto chunk = from_i_to_hex(payload.size()) + "\r\n" + payload + "\r\n";
  2246. if (!write_data(strm, chunk.data(), chunk.size())) {
  2247. ok = false;
  2248. return;
  2249. }
  2250. }
  2251. constexpr const char done_marker[] = "0\r\n";
  2252. if (!write_data(strm, done_marker, str_len(done_marker))) { ok = false; }
  2253. // Trailer
  2254. if (trailer) {
  2255. for (const auto &kv : *trailer) {
  2256. std::string field_line = kv.first + ": " + kv.second + "\r\n";
  2257. if (!write_data(strm, field_line.data(), field_line.size())) {
  2258. ok = false;
  2259. }
  2260. }
  2261. }
  2262. constexpr const char crlf[] = "\r\n";
  2263. if (!write_data(strm, crlf, str_len(crlf))) { ok = false; }
  2264. };
  2265. data_sink.done = [&](void) { done_with_trailer(nullptr); };
  2266. data_sink.done_with_trailer = [&](const Headers &trailer) {
  2267. done_with_trailer(&trailer);
  2268. };
  2269. while (data_available && !is_shutting_down()) {
  2270. if (!strm.wait_writable()) {
  2271. error = Error::Write;
  2272. return false;
  2273. } else if (!content_provider(offset, 0, data_sink)) {
  2274. error = Error::Canceled;
  2275. return false;
  2276. } else if (!ok) {
  2277. error = Error::Write;
  2278. return false;
  2279. }
  2280. }
  2281. error = Error::Success;
  2282. return true;
  2283. }
  2284. template <typename T, typename U>
  2285. bool write_content_chunked(Stream &strm,
  2286. const ContentProvider &content_provider,
  2287. const T &is_shutting_down, U &compressor) {
  2288. auto error = Error::Success;
  2289. return write_content_chunked(strm, content_provider, is_shutting_down,
  2290. compressor, error);
  2291. }
  2292. template <typename T>
  2293. bool redirect(T &cli, Request &req, Response &res,
  2294. const std::string &path, const std::string &location,
  2295. Error &error) {
  2296. Request new_req = req;
  2297. new_req.path = path;
  2298. new_req.redirect_count_ -= 1;
  2299. if (res.status == StatusCode::SeeOther_303 &&
  2300. (req.method != "GET" && req.method != "HEAD")) {
  2301. new_req.method = "GET";
  2302. new_req.body.clear();
  2303. new_req.headers.clear();
  2304. }
  2305. Response new_res;
  2306. auto ret = cli.send(new_req, new_res, error);
  2307. if (ret) {
  2308. req = std::move(new_req);
  2309. res = std::move(new_res);
  2310. if (res.location.empty()) { res.location = location; }
  2311. }
  2312. return ret;
  2313. }
  2314. std::string params_to_query_str(const Params &params) {
  2315. std::string query;
  2316. for (auto it = params.begin(); it != params.end(); ++it) {
  2317. if (it != params.begin()) { query += '&'; }
  2318. query += encode_query_component(it->first);
  2319. query += '=';
  2320. query += encode_query_component(it->second);
  2321. }
  2322. return query;
  2323. }
  2324. void parse_query_text(const char *data, std::size_t size,
  2325. Params &params) {
  2326. std::set<std::string> cache;
  2327. split(data, data + size, '&', [&](const char *b, const char *e) {
  2328. std::string kv(b, e);
  2329. if (cache.find(kv) != cache.end()) { return; }
  2330. cache.insert(std::move(kv));
  2331. std::string key;
  2332. std::string val;
  2333. divide(b, static_cast<std::size_t>(e - b), '=',
  2334. [&](const char *lhs_data, std::size_t lhs_size, const char *rhs_data,
  2335. std::size_t rhs_size) {
  2336. key.assign(lhs_data, lhs_size);
  2337. val.assign(rhs_data, rhs_size);
  2338. });
  2339. if (!key.empty()) {
  2340. params.emplace(decode_query_component(key), decode_query_component(val));
  2341. }
  2342. });
  2343. }
  2344. void parse_query_text(const std::string &s, Params &params) {
  2345. parse_query_text(s.data(), s.size(), params);
  2346. }
  2347. // Normalize a query string by decoding and re-encoding each key/value pair
  2348. // while preserving the original parameter order. This avoids double-encoding
  2349. // and ensures consistent encoding without reordering (unlike Params which
  2350. // uses std::multimap and sorts keys).
  2351. std::string normalize_query_string(const std::string &query) {
  2352. std::string result;
  2353. split(query.data(), query.data() + query.size(), '&',
  2354. [&](const char *b, const char *e) {
  2355. std::string key;
  2356. std::string val;
  2357. divide(b, static_cast<std::size_t>(e - b), '=',
  2358. [&](const char *lhs_data, std::size_t lhs_size,
  2359. const char *rhs_data, std::size_t rhs_size) {
  2360. key.assign(lhs_data, lhs_size);
  2361. val.assign(rhs_data, rhs_size);
  2362. });
  2363. if (!key.empty()) {
  2364. auto dec_key = decode_query_component(key);
  2365. auto dec_val = decode_query_component(val);
  2366. if (!result.empty()) { result += '&'; }
  2367. result += encode_query_component(dec_key);
  2368. if (!val.empty() || std::find(b, e, '=') != e) {
  2369. result += '=';
  2370. result += encode_query_component(dec_val);
  2371. }
  2372. }
  2373. });
  2374. return result;
  2375. }
  2376. bool parse_multipart_boundary(const std::string &content_type,
  2377. std::string &boundary) {
  2378. auto boundary_keyword = "boundary=";
  2379. auto pos = content_type.find(boundary_keyword);
  2380. if (pos == std::string::npos) { return false; }
  2381. auto end = content_type.find(';', pos);
  2382. auto beg = pos + strlen(boundary_keyword);
  2383. boundary = trim_double_quotes_copy(content_type.substr(beg, end - beg));
  2384. return !boundary.empty();
  2385. }
  2386. void parse_disposition_params(const std::string &s, Params &params) {
  2387. std::set<std::string> cache;
  2388. split(s.data(), s.data() + s.size(), ';', [&](const char *b, const char *e) {
  2389. std::string kv(b, e);
  2390. if (cache.find(kv) != cache.end()) { return; }
  2391. cache.insert(kv);
  2392. std::string key;
  2393. std::string val;
  2394. split(b, e, '=', [&](const char *b2, const char *e2) {
  2395. if (key.empty()) {
  2396. key.assign(b2, e2);
  2397. } else {
  2398. val.assign(b2, e2);
  2399. }
  2400. });
  2401. if (!key.empty()) {
  2402. params.emplace(trim_double_quotes_copy((key)),
  2403. trim_double_quotes_copy((val)));
  2404. }
  2405. });
  2406. }
  2407. #ifdef CPPHTTPLIB_NO_EXCEPTIONS
  2408. bool parse_range_header(const std::string &s, Ranges &ranges) {
  2409. #else
  2410. bool parse_range_header(const std::string &s, Ranges &ranges) try {
  2411. #endif
  2412. auto is_valid = [](const std::string &str) {
  2413. return std::all_of(str.cbegin(), str.cend(),
  2414. [](unsigned char c) { return std::isdigit(c); });
  2415. };
  2416. if (s.size() > 7 && s.compare(0, 6, "bytes=") == 0) {
  2417. const auto pos = static_cast<size_t>(6);
  2418. const auto len = static_cast<size_t>(s.size() - 6);
  2419. auto all_valid_ranges = true;
  2420. split(&s[pos], &s[pos + len], ',', [&](const char *b, const char *e) {
  2421. if (!all_valid_ranges) { return; }
  2422. const auto it = std::find(b, e, '-');
  2423. if (it == e) {
  2424. all_valid_ranges = false;
  2425. return;
  2426. }
  2427. const auto lhs = std::string(b, it);
  2428. const auto rhs = std::string(it + 1, e);
  2429. if (!is_valid(lhs) || !is_valid(rhs)) {
  2430. all_valid_ranges = false;
  2431. return;
  2432. }
  2433. const auto first =
  2434. static_cast<ssize_t>(lhs.empty() ? -1 : std::stoll(lhs));
  2435. const auto last =
  2436. static_cast<ssize_t>(rhs.empty() ? -1 : std::stoll(rhs));
  2437. if ((first == -1 && last == -1) ||
  2438. (first != -1 && last != -1 && first > last)) {
  2439. all_valid_ranges = false;
  2440. return;
  2441. }
  2442. ranges.emplace_back(first, last);
  2443. });
  2444. return all_valid_ranges && !ranges.empty();
  2445. }
  2446. return false;
  2447. #ifdef CPPHTTPLIB_NO_EXCEPTIONS
  2448. }
  2449. #else
  2450. } catch (...) { return false; }
  2451. #endif
  2452. bool parse_accept_header(const std::string &s,
  2453. std::vector<std::string> &content_types) {
  2454. content_types.clear();
  2455. // Empty string is considered valid (no preference)
  2456. if (s.empty()) { return true; }
  2457. // Check for invalid patterns: leading/trailing commas or consecutive commas
  2458. if (s.front() == ',' || s.back() == ',' ||
  2459. s.find(",,") != std::string::npos) {
  2460. return false;
  2461. }
  2462. struct AcceptEntry {
  2463. std::string media_type;
  2464. double quality;
  2465. int order; // Original order in header
  2466. };
  2467. std::vector<AcceptEntry> entries;
  2468. int order = 0;
  2469. bool has_invalid_entry = false;
  2470. // Split by comma and parse each entry
  2471. split(s.data(), s.data() + s.size(), ',', [&](const char *b, const char *e) {
  2472. std::string entry(b, e);
  2473. entry = trim_copy(entry);
  2474. if (entry.empty()) {
  2475. has_invalid_entry = true;
  2476. return;
  2477. }
  2478. AcceptEntry accept_entry;
  2479. accept_entry.quality = 1.0; // Default quality
  2480. accept_entry.order = order++;
  2481. // Find q= parameter
  2482. auto q_pos = entry.find(";q=");
  2483. if (q_pos == std::string::npos) { q_pos = entry.find("; q="); }
  2484. if (q_pos != std::string::npos) {
  2485. // Extract media type (before q parameter)
  2486. accept_entry.media_type = trim_copy(entry.substr(0, q_pos));
  2487. // Extract quality value
  2488. auto q_start = entry.find('=', q_pos) + 1;
  2489. auto q_end = entry.find(';', q_start);
  2490. if (q_end == std::string::npos) { q_end = entry.length(); }
  2491. std::string quality_str =
  2492. trim_copy(entry.substr(q_start, q_end - q_start));
  2493. if (quality_str.empty()) {
  2494. has_invalid_entry = true;
  2495. return;
  2496. }
  2497. #ifdef CPPHTTPLIB_NO_EXCEPTIONS
  2498. {
  2499. std::istringstream iss(quality_str);
  2500. iss >> accept_entry.quality;
  2501. // Check if conversion was successful and entire string was consumed
  2502. if (iss.fail() || !iss.eof()) {
  2503. has_invalid_entry = true;
  2504. return;
  2505. }
  2506. }
  2507. #else
  2508. try {
  2509. accept_entry.quality = std::stod(quality_str);
  2510. } catch (...) {
  2511. has_invalid_entry = true;
  2512. return;
  2513. }
  2514. #endif
  2515. // Check if quality is in valid range [0.0, 1.0]
  2516. if (accept_entry.quality < 0.0 || accept_entry.quality > 1.0) {
  2517. has_invalid_entry = true;
  2518. return;
  2519. }
  2520. } else {
  2521. // No quality parameter, use entire entry as media type
  2522. accept_entry.media_type = entry;
  2523. }
  2524. // Remove additional parameters from media type
  2525. auto param_pos = accept_entry.media_type.find(';');
  2526. if (param_pos != std::string::npos) {
  2527. accept_entry.media_type =
  2528. trim_copy(accept_entry.media_type.substr(0, param_pos));
  2529. }
  2530. // Basic validation of media type format
  2531. if (accept_entry.media_type.empty()) {
  2532. has_invalid_entry = true;
  2533. return;
  2534. }
  2535. // Check for basic media type format (should contain '/' or be '*')
  2536. if (accept_entry.media_type != "*" &&
  2537. accept_entry.media_type.find('/') == std::string::npos) {
  2538. has_invalid_entry = true;
  2539. return;
  2540. }
  2541. entries.push_back(std::move(accept_entry));
  2542. });
  2543. // Return false if any invalid entry was found
  2544. if (has_invalid_entry) { return false; }
  2545. // Sort by quality (descending), then by original order (ascending)
  2546. std::sort(entries.begin(), entries.end(),
  2547. [](const AcceptEntry &a, const AcceptEntry &b) {
  2548. if (a.quality != b.quality) {
  2549. return a.quality > b.quality; // Higher quality first
  2550. }
  2551. return a.order < b.order; // Earlier order first for same quality
  2552. });
  2553. // Extract sorted media types
  2554. content_types.reserve(entries.size());
  2555. for (auto &entry : entries) {
  2556. content_types.push_back(std::move(entry.media_type));
  2557. }
  2558. return true;
  2559. }
  2560. class FormDataParser {
  2561. public:
  2562. FormDataParser() = default;
  2563. void set_boundary(std::string &&boundary) {
  2564. boundary_ = std::move(boundary);
  2565. dash_boundary_crlf_ = dash_ + boundary_ + crlf_;
  2566. crlf_dash_boundary_ = crlf_ + dash_ + boundary_;
  2567. }
  2568. bool is_valid() const { return is_valid_; }
  2569. bool parse(const char *buf, size_t n, const FormDataHeader &header_callback,
  2570. const ContentReceiver &content_callback) {
  2571. buf_append(buf, n);
  2572. while (buf_size() > 0) {
  2573. switch (state_) {
  2574. case 0: { // Initial boundary
  2575. auto pos = buf_find(dash_boundary_crlf_);
  2576. if (pos == buf_size()) { return true; }
  2577. buf_erase(pos + dash_boundary_crlf_.size());
  2578. state_ = 1;
  2579. break;
  2580. }
  2581. case 1: { // New entry
  2582. clear_file_info();
  2583. state_ = 2;
  2584. break;
  2585. }
  2586. case 2: { // Headers
  2587. auto pos = buf_find(crlf_);
  2588. if (pos > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; }
  2589. while (pos < buf_size()) {
  2590. // Empty line
  2591. if (pos == 0) {
  2592. if (!header_callback(file_)) {
  2593. is_valid_ = false;
  2594. return false;
  2595. }
  2596. buf_erase(crlf_.size());
  2597. state_ = 3;
  2598. break;
  2599. }
  2600. const auto header = buf_head(pos);
  2601. if (!parse_header(header.data(), header.data() + header.size(),
  2602. [&](const std::string &, const std::string &) {})) {
  2603. is_valid_ = false;
  2604. return false;
  2605. }
  2606. // Parse and emplace space trimmed headers into a map
  2607. if (!parse_header(
  2608. header.data(), header.data() + header.size(),
  2609. [&](const std::string &key, const std::string &val) {
  2610. file_.headers.emplace(key, val);
  2611. })) {
  2612. is_valid_ = false;
  2613. return false;
  2614. }
  2615. constexpr const char header_content_type[] = "Content-Type:";
  2616. if (start_with_case_ignore(header, header_content_type)) {
  2617. file_.content_type =
  2618. trim_copy(header.substr(str_len(header_content_type)));
  2619. } else {
  2620. thread_local const std::regex re_content_disposition(
  2621. R"~(^Content-Disposition:\s*form-data;\s*(.*)$)~",
  2622. std::regex_constants::icase);
  2623. std::smatch m;
  2624. if (std::regex_match(header, m, re_content_disposition)) {
  2625. Params params;
  2626. parse_disposition_params(m[1], params);
  2627. auto it = params.find("name");
  2628. if (it != params.end()) {
  2629. file_.name = it->second;
  2630. } else {
  2631. is_valid_ = false;
  2632. return false;
  2633. }
  2634. it = params.find("filename");
  2635. if (it != params.end()) { file_.filename = it->second; }
  2636. it = params.find("filename*");
  2637. if (it != params.end()) {
  2638. // Only allow UTF-8 encoding...
  2639. thread_local const std::regex re_rfc5987_encoding(
  2640. R"~(^UTF-8''(.+?)$)~", std::regex_constants::icase);
  2641. std::smatch m2;
  2642. if (std::regex_match(it->second, m2, re_rfc5987_encoding)) {
  2643. file_.filename = decode_path_component(m2[1]); // override...
  2644. } else {
  2645. is_valid_ = false;
  2646. return false;
  2647. }
  2648. }
  2649. }
  2650. }
  2651. buf_erase(pos + crlf_.size());
  2652. pos = buf_find(crlf_);
  2653. }
  2654. if (state_ != 3) { return true; }
  2655. break;
  2656. }
  2657. case 3: { // Body
  2658. if (crlf_dash_boundary_.size() > buf_size()) { return true; }
  2659. auto pos = buf_find(crlf_dash_boundary_);
  2660. if (pos < buf_size()) {
  2661. if (!content_callback(buf_data(), pos)) {
  2662. is_valid_ = false;
  2663. return false;
  2664. }
  2665. buf_erase(pos + crlf_dash_boundary_.size());
  2666. state_ = 4;
  2667. } else {
  2668. auto len = buf_size() - crlf_dash_boundary_.size();
  2669. if (len > 0) {
  2670. if (!content_callback(buf_data(), len)) {
  2671. is_valid_ = false;
  2672. return false;
  2673. }
  2674. buf_erase(len);
  2675. }
  2676. return true;
  2677. }
  2678. break;
  2679. }
  2680. case 4: { // Boundary
  2681. if (crlf_.size() > buf_size()) { return true; }
  2682. if (buf_start_with(crlf_)) {
  2683. buf_erase(crlf_.size());
  2684. state_ = 1;
  2685. } else {
  2686. if (dash_.size() > buf_size()) { return true; }
  2687. if (buf_start_with(dash_)) {
  2688. buf_erase(dash_.size());
  2689. is_valid_ = true;
  2690. buf_erase(buf_size()); // Remove epilogue
  2691. } else {
  2692. return true;
  2693. }
  2694. }
  2695. break;
  2696. }
  2697. }
  2698. }
  2699. return true;
  2700. }
  2701. private:
  2702. void clear_file_info() {
  2703. file_.name.clear();
  2704. file_.filename.clear();
  2705. file_.content_type.clear();
  2706. file_.headers.clear();
  2707. }
  2708. bool start_with_case_ignore(const std::string &a, const char *b) const {
  2709. const auto b_len = strlen(b);
  2710. if (a.size() < b_len) { return false; }
  2711. for (size_t i = 0; i < b_len; i++) {
  2712. if (case_ignore::to_lower(a[i]) != case_ignore::to_lower(b[i])) {
  2713. return false;
  2714. }
  2715. }
  2716. return true;
  2717. }
  2718. const std::string dash_ = "--";
  2719. const std::string crlf_ = "\r\n";
  2720. std::string boundary_;
  2721. std::string dash_boundary_crlf_;
  2722. std::string crlf_dash_boundary_;
  2723. size_t state_ = 0;
  2724. bool is_valid_ = false;
  2725. FormData file_;
  2726. // Buffer
  2727. bool start_with(const std::string &a, size_t spos, size_t epos,
  2728. const std::string &b) const {
  2729. if (epos - spos < b.size()) { return false; }
  2730. for (size_t i = 0; i < b.size(); i++) {
  2731. if (a[i + spos] != b[i]) { return false; }
  2732. }
  2733. return true;
  2734. }
  2735. size_t buf_size() const { return buf_epos_ - buf_spos_; }
  2736. const char *buf_data() const { return &buf_[buf_spos_]; }
  2737. std::string buf_head(size_t l) const { return buf_.substr(buf_spos_, l); }
  2738. bool buf_start_with(const std::string &s) const {
  2739. return start_with(buf_, buf_spos_, buf_epos_, s);
  2740. }
  2741. size_t buf_find(const std::string &s) const {
  2742. auto c = s.front();
  2743. size_t off = buf_spos_;
  2744. while (off < buf_epos_) {
  2745. auto pos = off;
  2746. while (true) {
  2747. if (pos == buf_epos_) { return buf_size(); }
  2748. if (buf_[pos] == c) { break; }
  2749. pos++;
  2750. }
  2751. auto remaining_size = buf_epos_ - pos;
  2752. if (s.size() > remaining_size) { return buf_size(); }
  2753. if (start_with(buf_, pos, buf_epos_, s)) { return pos - buf_spos_; }
  2754. off = pos + 1;
  2755. }
  2756. return buf_size();
  2757. }
  2758. void buf_append(const char *data, size_t n) {
  2759. auto remaining_size = buf_size();
  2760. if (remaining_size > 0 && buf_spos_ > 0) {
  2761. for (size_t i = 0; i < remaining_size; i++) {
  2762. buf_[i] = buf_[buf_spos_ + i];
  2763. }
  2764. }
  2765. buf_spos_ = 0;
  2766. buf_epos_ = remaining_size;
  2767. if (remaining_size + n > buf_.size()) { buf_.resize(remaining_size + n); }
  2768. for (size_t i = 0; i < n; i++) {
  2769. buf_[buf_epos_ + i] = data[i];
  2770. }
  2771. buf_epos_ += n;
  2772. }
  2773. void buf_erase(size_t size) { buf_spos_ += size; }
  2774. std::string buf_;
  2775. size_t buf_spos_ = 0;
  2776. size_t buf_epos_ = 0;
  2777. };
  2778. std::string random_string(size_t length) {
  2779. constexpr const char data[] =
  2780. "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  2781. thread_local auto engine([]() {
  2782. // std::random_device might actually be deterministic on some
  2783. // platforms, but due to lack of support in the c++ standard library,
  2784. // doing better requires either some ugly hacks or breaking portability.
  2785. std::random_device seed_gen;
  2786. // Request 128 bits of entropy for initialization
  2787. std::seed_seq seed_sequence{seed_gen(), seed_gen(), seed_gen(), seed_gen()};
  2788. return std::mt19937(seed_sequence);
  2789. }());
  2790. std::string result;
  2791. for (size_t i = 0; i < length; i++) {
  2792. result += data[engine() % (sizeof(data) - 1)];
  2793. }
  2794. return result;
  2795. }
  2796. std::string make_multipart_data_boundary() {
  2797. return "--cpp-httplib-multipart-data-" + detail::random_string(16);
  2798. }
  2799. bool is_multipart_boundary_chars_valid(const std::string &boundary) {
  2800. auto valid = true;
  2801. for (size_t i = 0; i < boundary.size(); i++) {
  2802. auto c = boundary[i];
  2803. if (!std::isalnum(c) && c != '-' && c != '_') {
  2804. valid = false;
  2805. break;
  2806. }
  2807. }
  2808. return valid;
  2809. }
  2810. template <typename T>
  2811. std::string
  2812. serialize_multipart_formdata_item_begin(const T &item,
  2813. const std::string &boundary) {
  2814. std::string body = "--" + boundary + "\r\n";
  2815. body += "Content-Disposition: form-data; name=\"" + item.name + "\"";
  2816. if (!item.filename.empty()) {
  2817. body += "; filename=\"" + item.filename + "\"";
  2818. }
  2819. body += "\r\n";
  2820. if (!item.content_type.empty()) {
  2821. body += "Content-Type: " + item.content_type + "\r\n";
  2822. }
  2823. body += "\r\n";
  2824. return body;
  2825. }
  2826. std::string serialize_multipart_formdata_item_end() { return "\r\n"; }
  2827. std::string
  2828. serialize_multipart_formdata_finish(const std::string &boundary) {
  2829. return "--" + boundary + "--\r\n";
  2830. }
  2831. std::string
  2832. serialize_multipart_formdata_get_content_type(const std::string &boundary) {
  2833. return "multipart/form-data; boundary=" + boundary;
  2834. }
  2835. std::string
  2836. serialize_multipart_formdata(const UploadFormDataItems &items,
  2837. const std::string &boundary, bool finish = true) {
  2838. std::string body;
  2839. for (const auto &item : items) {
  2840. body += serialize_multipart_formdata_item_begin(item, boundary);
  2841. body += item.content + serialize_multipart_formdata_item_end();
  2842. }
  2843. if (finish) { body += serialize_multipart_formdata_finish(boundary); }
  2844. return body;
  2845. }
  2846. void coalesce_ranges(Ranges &ranges, size_t content_length) {
  2847. if (ranges.size() <= 1) return;
  2848. // Sort ranges by start position
  2849. std::sort(ranges.begin(), ranges.end(),
  2850. [](const Range &a, const Range &b) { return a.first < b.first; });
  2851. Ranges coalesced;
  2852. coalesced.reserve(ranges.size());
  2853. for (auto &r : ranges) {
  2854. auto first_pos = r.first;
  2855. auto last_pos = r.second;
  2856. // Handle special cases like in range_error
  2857. if (first_pos == -1 && last_pos == -1) {
  2858. first_pos = 0;
  2859. last_pos = static_cast<ssize_t>(content_length);
  2860. }
  2861. if (first_pos == -1) {
  2862. first_pos = static_cast<ssize_t>(content_length) - last_pos;
  2863. last_pos = static_cast<ssize_t>(content_length) - 1;
  2864. }
  2865. if (last_pos == -1 || last_pos >= static_cast<ssize_t>(content_length)) {
  2866. last_pos = static_cast<ssize_t>(content_length) - 1;
  2867. }
  2868. // Skip invalid ranges
  2869. if (!(0 <= first_pos && first_pos <= last_pos &&
  2870. last_pos < static_cast<ssize_t>(content_length))) {
  2871. continue;
  2872. }
  2873. // Coalesce with previous range if overlapping or adjacent (but not
  2874. // identical)
  2875. if (!coalesced.empty()) {
  2876. auto &prev = coalesced.back();
  2877. // Check if current range overlaps or is adjacent to previous range
  2878. // but don't coalesce identical ranges (allow duplicates)
  2879. if (first_pos <= prev.second + 1 &&
  2880. !(first_pos == prev.first && last_pos == prev.second)) {
  2881. // Extend the previous range
  2882. prev.second = (std::max)(prev.second, last_pos);
  2883. continue;
  2884. }
  2885. }
  2886. // Add new range
  2887. coalesced.emplace_back(first_pos, last_pos);
  2888. }
  2889. ranges = std::move(coalesced);
  2890. }
  2891. bool range_error(Request &req, Response &res) {
  2892. if (!req.ranges.empty() && 200 <= res.status && res.status < 300) {
  2893. ssize_t content_len = static_cast<ssize_t>(
  2894. res.content_length_ ? res.content_length_ : res.body.size());
  2895. std::vector<std::pair<ssize_t, ssize_t>> processed_ranges;
  2896. size_t overwrapping_count = 0;
  2897. // NOTE: The following Range check is based on '14.2. Range' in RFC 9110
  2898. // 'HTTP Semantics' to avoid potential denial-of-service attacks.
  2899. // https://www.rfc-editor.org/rfc/rfc9110#section-14.2
  2900. // Too many ranges
  2901. if (req.ranges.size() > CPPHTTPLIB_RANGE_MAX_COUNT) { return true; }
  2902. for (auto &r : req.ranges) {
  2903. auto &first_pos = r.first;
  2904. auto &last_pos = r.second;
  2905. if (first_pos == -1 && last_pos == -1) {
  2906. first_pos = 0;
  2907. last_pos = content_len;
  2908. }
  2909. if (first_pos == -1) {
  2910. first_pos = content_len - last_pos;
  2911. last_pos = content_len - 1;
  2912. }
  2913. // NOTE: RFC-9110 '14.1.2. Byte Ranges':
  2914. // A client can limit the number of bytes requested without knowing the
  2915. // size of the selected representation. If the last-pos value is absent,
  2916. // or if the value is greater than or equal to the current length of the
  2917. // representation data, the byte range is interpreted as the remainder of
  2918. // the representation (i.e., the server replaces the value of last-pos
  2919. // with a value that is one less than the current length of the selected
  2920. // representation).
  2921. // https://www.rfc-editor.org/rfc/rfc9110.html#section-14.1.2-6
  2922. if (last_pos == -1 || last_pos >= content_len) {
  2923. last_pos = content_len - 1;
  2924. }
  2925. // Range must be within content length
  2926. if (!(0 <= first_pos && first_pos <= last_pos &&
  2927. last_pos <= content_len - 1)) {
  2928. return true;
  2929. }
  2930. // Request must not have more than two overlapping ranges
  2931. for (const auto &processed_range : processed_ranges) {
  2932. if (!(last_pos < processed_range.first ||
  2933. first_pos > processed_range.second)) {
  2934. overwrapping_count++;
  2935. if (overwrapping_count > 2) { return true; }
  2936. break; // Only count once per range
  2937. }
  2938. }
  2939. processed_ranges.emplace_back(first_pos, last_pos);
  2940. }
  2941. // After validation, coalesce overlapping ranges as per RFC 9110
  2942. coalesce_ranges(req.ranges, static_cast<size_t>(content_len));
  2943. }
  2944. return false;
  2945. }
  2946. std::pair<size_t, size_t>
  2947. get_range_offset_and_length(Range r, size_t content_length) {
  2948. assert(r.first != -1 && r.second != -1);
  2949. assert(0 <= r.first && r.first < static_cast<ssize_t>(content_length));
  2950. assert(r.first <= r.second &&
  2951. r.second < static_cast<ssize_t>(content_length));
  2952. (void)(content_length);
  2953. return std::make_pair(r.first, static_cast<size_t>(r.second - r.first) + 1);
  2954. }
  2955. std::string make_content_range_header_field(
  2956. const std::pair<size_t, size_t> &offset_and_length, size_t content_length) {
  2957. auto st = offset_and_length.first;
  2958. auto ed = st + offset_and_length.second - 1;
  2959. std::string field = "bytes ";
  2960. field += std::to_string(st);
  2961. field += '-';
  2962. field += std::to_string(ed);
  2963. field += '/';
  2964. field += std::to_string(content_length);
  2965. return field;
  2966. }
  2967. template <typename SToken, typename CToken, typename Content>
  2968. bool process_multipart_ranges_data(const Request &req,
  2969. const std::string &boundary,
  2970. const std::string &content_type,
  2971. size_t content_length, SToken stoken,
  2972. CToken ctoken, Content content) {
  2973. for (size_t i = 0; i < req.ranges.size(); i++) {
  2974. ctoken("--");
  2975. stoken(boundary);
  2976. ctoken("\r\n");
  2977. if (!content_type.empty()) {
  2978. ctoken("Content-Type: ");
  2979. stoken(content_type);
  2980. ctoken("\r\n");
  2981. }
  2982. auto offset_and_length =
  2983. get_range_offset_and_length(req.ranges[i], content_length);
  2984. ctoken("Content-Range: ");
  2985. stoken(make_content_range_header_field(offset_and_length, content_length));
  2986. ctoken("\r\n");
  2987. ctoken("\r\n");
  2988. if (!content(offset_and_length.first, offset_and_length.second)) {
  2989. return false;
  2990. }
  2991. ctoken("\r\n");
  2992. }
  2993. ctoken("--");
  2994. stoken(boundary);
  2995. ctoken("--");
  2996. return true;
  2997. }
  2998. void make_multipart_ranges_data(const Request &req, Response &res,
  2999. const std::string &boundary,
  3000. const std::string &content_type,
  3001. size_t content_length,
  3002. std::string &data) {
  3003. process_multipart_ranges_data(
  3004. req, boundary, content_type, content_length,
  3005. [&](const std::string &token) { data += token; },
  3006. [&](const std::string &token) { data += token; },
  3007. [&](size_t offset, size_t length) {
  3008. assert(offset + length <= content_length);
  3009. data += res.body.substr(offset, length);
  3010. return true;
  3011. });
  3012. }
  3013. size_t get_multipart_ranges_data_length(const Request &req,
  3014. const std::string &boundary,
  3015. const std::string &content_type,
  3016. size_t content_length) {
  3017. size_t data_length = 0;
  3018. process_multipart_ranges_data(
  3019. req, boundary, content_type, content_length,
  3020. [&](const std::string &token) { data_length += token.size(); },
  3021. [&](const std::string &token) { data_length += token.size(); },
  3022. [&](size_t /*offset*/, size_t length) {
  3023. data_length += length;
  3024. return true;
  3025. });
  3026. return data_length;
  3027. }
  3028. template <typename T>
  3029. bool
  3030. write_multipart_ranges_data(Stream &strm, const Request &req, Response &res,
  3031. const std::string &boundary,
  3032. const std::string &content_type,
  3033. size_t content_length, const T &is_shutting_down) {
  3034. return process_multipart_ranges_data(
  3035. req, boundary, content_type, content_length,
  3036. [&](const std::string &token) { strm.write(token); },
  3037. [&](const std::string &token) { strm.write(token); },
  3038. [&](size_t offset, size_t length) {
  3039. return write_content(strm, res.content_provider_, offset, length,
  3040. is_shutting_down);
  3041. });
  3042. }
  3043. bool expect_content(const Request &req) {
  3044. if (req.method == "POST" || req.method == "PUT" || req.method == "PATCH" ||
  3045. req.method == "DELETE") {
  3046. return true;
  3047. }
  3048. if (req.has_header("Content-Length") &&
  3049. req.get_header_value_u64("Content-Length") > 0) {
  3050. return true;
  3051. }
  3052. if (is_chunked_transfer_encoding(req.headers)) { return true; }
  3053. return false;
  3054. }
  3055. bool has_crlf(const std::string &s) {
  3056. auto p = s.c_str();
  3057. while (*p) {
  3058. if (*p == '\r' || *p == '\n') { return true; }
  3059. p++;
  3060. }
  3061. return false;
  3062. }
  3063. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  3064. std::string message_digest(const std::string &s, const EVP_MD *algo) {
  3065. auto context = std::unique_ptr<EVP_MD_CTX, decltype(&EVP_MD_CTX_free)>(
  3066. EVP_MD_CTX_new(), EVP_MD_CTX_free);
  3067. unsigned int hash_length = 0;
  3068. unsigned char hash[EVP_MAX_MD_SIZE];
  3069. EVP_DigestInit_ex(context.get(), algo, nullptr);
  3070. EVP_DigestUpdate(context.get(), s.c_str(), s.size());
  3071. EVP_DigestFinal_ex(context.get(), hash, &hash_length);
  3072. std::stringstream ss;
  3073. for (auto i = 0u; i < hash_length; ++i) {
  3074. ss << std::hex << std::setw(2) << std::setfill('0')
  3075. << static_cast<unsigned int>(hash[i]);
  3076. }
  3077. return ss.str();
  3078. }
  3079. std::string MD5(const std::string &s) {
  3080. return message_digest(s, EVP_md5());
  3081. }
  3082. std::string SHA_256(const std::string &s) {
  3083. return message_digest(s, EVP_sha256());
  3084. }
  3085. std::string SHA_512(const std::string &s) {
  3086. return message_digest(s, EVP_sha512());
  3087. }
  3088. std::pair<std::string, std::string> make_digest_authentication_header(
  3089. const Request &req, const std::map<std::string, std::string> &auth,
  3090. size_t cnonce_count, const std::string &cnonce, const std::string &username,
  3091. const std::string &password, bool is_proxy = false) {
  3092. std::string nc;
  3093. {
  3094. std::stringstream ss;
  3095. ss << std::setfill('0') << std::setw(8) << std::hex << cnonce_count;
  3096. nc = ss.str();
  3097. }
  3098. std::string qop;
  3099. if (auth.find("qop") != auth.end()) {
  3100. qop = auth.at("qop");
  3101. if (qop.find("auth-int") != std::string::npos) {
  3102. qop = "auth-int";
  3103. } else if (qop.find("auth") != std::string::npos) {
  3104. qop = "auth";
  3105. } else {
  3106. qop.clear();
  3107. }
  3108. }
  3109. std::string algo = "MD5";
  3110. if (auth.find("algorithm") != auth.end()) { algo = auth.at("algorithm"); }
  3111. std::string response;
  3112. {
  3113. auto H = algo == "SHA-256" ? detail::SHA_256
  3114. : algo == "SHA-512" ? detail::SHA_512
  3115. : detail::MD5;
  3116. auto A1 = username + ":" + auth.at("realm") + ":" + password;
  3117. auto A2 = req.method + ":" + req.path;
  3118. if (qop == "auth-int") { A2 += ":" + H(req.body); }
  3119. if (qop.empty()) {
  3120. response = H(H(A1) + ":" + auth.at("nonce") + ":" + H(A2));
  3121. } else {
  3122. response = H(H(A1) + ":" + auth.at("nonce") + ":" + nc + ":" + cnonce +
  3123. ":" + qop + ":" + H(A2));
  3124. }
  3125. }
  3126. auto opaque = (auth.find("opaque") != auth.end()) ? auth.at("opaque") : "";
  3127. auto field = "Digest username=\"" + username + "\", realm=\"" +
  3128. auth.at("realm") + "\", nonce=\"" + auth.at("nonce") +
  3129. "\", uri=\"" + req.path + "\", algorithm=" + algo +
  3130. (qop.empty() ? ", response=\""
  3131. : ", qop=" + qop + ", nc=" + nc + ", cnonce=\"" +
  3132. cnonce + "\", response=\"") +
  3133. response + "\"" +
  3134. (opaque.empty() ? "" : ", opaque=\"" + opaque + "\"");
  3135. auto key = is_proxy ? "Proxy-Authorization" : "Authorization";
  3136. return std::make_pair(key, field);
  3137. }
  3138. bool is_ssl_peer_could_be_closed(SSL *ssl, socket_t sock) {
  3139. detail::set_nonblocking(sock, true);
  3140. auto se = detail::scope_exit([&]() { detail::set_nonblocking(sock, false); });
  3141. char buf[1];
  3142. return !SSL_peek(ssl, buf, 1) &&
  3143. SSL_get_error(ssl, 0) == SSL_ERROR_ZERO_RETURN;
  3144. }
  3145. #ifdef _WIN32
  3146. // NOTE: This code came up with the following stackoverflow post:
  3147. // https://stackoverflow.com/questions/9507184/can-openssl-on-windows-use-the-system-certificate-store
  3148. bool load_system_certs_on_windows(X509_STORE *store) {
  3149. auto hStore = CertOpenSystemStoreW((HCRYPTPROV_LEGACY)NULL, L"ROOT");
  3150. if (!hStore) { return false; }
  3151. auto result = false;
  3152. PCCERT_CONTEXT pContext = NULL;
  3153. while ((pContext = CertEnumCertificatesInStore(hStore, pContext)) !=
  3154. nullptr) {
  3155. auto encoded_cert =
  3156. static_cast<const unsigned char *>(pContext->pbCertEncoded);
  3157. auto x509 = d2i_X509(NULL, &encoded_cert, pContext->cbCertEncoded);
  3158. if (x509) {
  3159. X509_STORE_add_cert(store, x509);
  3160. X509_free(x509);
  3161. result = true;
  3162. }
  3163. }
  3164. CertFreeCertificateContext(pContext);
  3165. CertCloseStore(hStore, 0);
  3166. return result;
  3167. }
  3168. #elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && TARGET_OS_MAC
  3169. template <typename T>
  3170. using CFObjectPtr =
  3171. std::unique_ptr<typename std::remove_pointer<T>::type, void (*)(CFTypeRef)>;
  3172. void cf_object_ptr_deleter(CFTypeRef obj) {
  3173. if (obj) { CFRelease(obj); }
  3174. }
  3175. bool retrieve_certs_from_keychain(CFObjectPtr<CFArrayRef> &certs) {
  3176. CFStringRef keys[] = {kSecClass, kSecMatchLimit, kSecReturnRef};
  3177. CFTypeRef values[] = {kSecClassCertificate, kSecMatchLimitAll,
  3178. kCFBooleanTrue};
  3179. CFObjectPtr<CFDictionaryRef> query(
  3180. CFDictionaryCreate(nullptr, reinterpret_cast<const void **>(keys), values,
  3181. sizeof(keys) / sizeof(keys[0]),
  3182. &kCFTypeDictionaryKeyCallBacks,
  3183. &kCFTypeDictionaryValueCallBacks),
  3184. cf_object_ptr_deleter);
  3185. if (!query) { return false; }
  3186. CFTypeRef security_items = nullptr;
  3187. if (SecItemCopyMatching(query.get(), &security_items) != errSecSuccess ||
  3188. CFArrayGetTypeID() != CFGetTypeID(security_items)) {
  3189. return false;
  3190. }
  3191. certs.reset(reinterpret_cast<CFArrayRef>(security_items));
  3192. return true;
  3193. }
  3194. bool retrieve_root_certs_from_keychain(CFObjectPtr<CFArrayRef> &certs) {
  3195. CFArrayRef root_security_items = nullptr;
  3196. if (SecTrustCopyAnchorCertificates(&root_security_items) != errSecSuccess) {
  3197. return false;
  3198. }
  3199. certs.reset(root_security_items);
  3200. return true;
  3201. }
  3202. bool add_certs_to_x509_store(CFArrayRef certs, X509_STORE *store) {
  3203. auto result = false;
  3204. for (auto i = 0; i < CFArrayGetCount(certs); ++i) {
  3205. const auto cert = reinterpret_cast<const __SecCertificate *>(
  3206. CFArrayGetValueAtIndex(certs, i));
  3207. if (SecCertificateGetTypeID() != CFGetTypeID(cert)) { continue; }
  3208. CFDataRef cert_data = nullptr;
  3209. if (SecItemExport(cert, kSecFormatX509Cert, 0, nullptr, &cert_data) !=
  3210. errSecSuccess) {
  3211. continue;
  3212. }
  3213. CFObjectPtr<CFDataRef> cert_data_ptr(cert_data, cf_object_ptr_deleter);
  3214. auto encoded_cert = static_cast<const unsigned char *>(
  3215. CFDataGetBytePtr(cert_data_ptr.get()));
  3216. auto x509 =
  3217. d2i_X509(NULL, &encoded_cert, CFDataGetLength(cert_data_ptr.get()));
  3218. if (x509) {
  3219. X509_STORE_add_cert(store, x509);
  3220. X509_free(x509);
  3221. result = true;
  3222. }
  3223. }
  3224. return result;
  3225. }
  3226. bool load_system_certs_on_macos(X509_STORE *store) {
  3227. auto result = false;
  3228. CFObjectPtr<CFArrayRef> certs(nullptr, cf_object_ptr_deleter);
  3229. if (retrieve_certs_from_keychain(certs) && certs) {
  3230. result = add_certs_to_x509_store(certs.get(), store);
  3231. }
  3232. if (retrieve_root_certs_from_keychain(certs) && certs) {
  3233. result = add_certs_to_x509_store(certs.get(), store) || result;
  3234. }
  3235. return result;
  3236. }
  3237. #endif // _WIN32
  3238. #endif // CPPHTTPLIB_OPENSSL_SUPPORT
  3239. #ifdef _WIN32
  3240. class WSInit {
  3241. public:
  3242. WSInit() {
  3243. WSADATA wsaData;
  3244. if (WSAStartup(0x0002, &wsaData) == 0) is_valid_ = true;
  3245. }
  3246. ~WSInit() {
  3247. if (is_valid_) WSACleanup();
  3248. }
  3249. bool is_valid_ = false;
  3250. };
  3251. static WSInit wsinit_;
  3252. #endif
  3253. bool parse_www_authenticate(const Response &res,
  3254. std::map<std::string, std::string> &auth,
  3255. bool is_proxy) {
  3256. auto auth_key = is_proxy ? "Proxy-Authenticate" : "WWW-Authenticate";
  3257. if (res.has_header(auth_key)) {
  3258. thread_local auto re =
  3259. std::regex(R"~((?:(?:,\s*)?(.+?)=(?:"(.*?)"|([^,]*))))~");
  3260. auto s = res.get_header_value(auth_key);
  3261. auto pos = s.find(' ');
  3262. if (pos != std::string::npos) {
  3263. auto type = s.substr(0, pos);
  3264. if (type == "Basic") {
  3265. return false;
  3266. } else if (type == "Digest") {
  3267. s = s.substr(pos + 1);
  3268. auto beg = std::sregex_iterator(s.begin(), s.end(), re);
  3269. for (auto i = beg; i != std::sregex_iterator(); ++i) {
  3270. const auto &m = *i;
  3271. auto key = s.substr(static_cast<size_t>(m.position(1)),
  3272. static_cast<size_t>(m.length(1)));
  3273. auto val = m.length(2) > 0
  3274. ? s.substr(static_cast<size_t>(m.position(2)),
  3275. static_cast<size_t>(m.length(2)))
  3276. : s.substr(static_cast<size_t>(m.position(3)),
  3277. static_cast<size_t>(m.length(3)));
  3278. auth[std::move(key)] = std::move(val);
  3279. }
  3280. return true;
  3281. }
  3282. }
  3283. }
  3284. return false;
  3285. }
  3286. class ContentProviderAdapter {
  3287. public:
  3288. explicit ContentProviderAdapter(
  3289. ContentProviderWithoutLength &&content_provider)
  3290. : content_provider_(std::move(content_provider)) {}
  3291. bool operator()(size_t offset, size_t, DataSink &sink) {
  3292. return content_provider_(offset, sink);
  3293. }
  3294. private:
  3295. ContentProviderWithoutLength content_provider_;
  3296. };
  3297. // NOTE: https://www.rfc-editor.org/rfc/rfc9110#section-5
  3298. namespace fields {
  3299. bool is_token_char(char c) {
  3300. return std::isalnum(c) || c == '!' || c == '#' || c == '$' || c == '%' ||
  3301. c == '&' || c == '\'' || c == '*' || c == '+' || c == '-' ||
  3302. c == '.' || c == '^' || c == '_' || c == '`' || c == '|' || c == '~';
  3303. }
  3304. bool is_token(const std::string &s) {
  3305. if (s.empty()) { return false; }
  3306. for (auto c : s) {
  3307. if (!is_token_char(c)) { return false; }
  3308. }
  3309. return true;
  3310. }
  3311. bool is_field_name(const std::string &s) { return is_token(s); }
  3312. bool is_vchar(char c) { return c >= 33 && c <= 126; }
  3313. bool is_obs_text(char c) { return 128 <= static_cast<unsigned char>(c); }
  3314. bool is_field_vchar(char c) { return is_vchar(c) || is_obs_text(c); }
  3315. bool is_field_content(const std::string &s) {
  3316. if (s.empty()) { return true; }
  3317. if (s.size() == 1) {
  3318. return is_field_vchar(s[0]);
  3319. } else if (s.size() == 2) {
  3320. return is_field_vchar(s[0]) && is_field_vchar(s[1]);
  3321. } else {
  3322. size_t i = 0;
  3323. if (!is_field_vchar(s[i])) { return false; }
  3324. i++;
  3325. while (i < s.size() - 1) {
  3326. auto c = s[i++];
  3327. if (c == ' ' || c == '\t' || is_field_vchar(c)) {
  3328. } else {
  3329. return false;
  3330. }
  3331. }
  3332. return is_field_vchar(s[i]);
  3333. }
  3334. }
  3335. bool is_field_value(const std::string &s) { return is_field_content(s); }
  3336. } // namespace fields
  3337. } // namespace detail
  3338. const char *status_message(int status) {
  3339. switch (status) {
  3340. case StatusCode::Continue_100: return "Continue";
  3341. case StatusCode::SwitchingProtocol_101: return "Switching Protocol";
  3342. case StatusCode::Processing_102: return "Processing";
  3343. case StatusCode::EarlyHints_103: return "Early Hints";
  3344. case StatusCode::OK_200: return "OK";
  3345. case StatusCode::Created_201: return "Created";
  3346. case StatusCode::Accepted_202: return "Accepted";
  3347. case StatusCode::NonAuthoritativeInformation_203:
  3348. return "Non-Authoritative Information";
  3349. case StatusCode::NoContent_204: return "No Content";
  3350. case StatusCode::ResetContent_205: return "Reset Content";
  3351. case StatusCode::PartialContent_206: return "Partial Content";
  3352. case StatusCode::MultiStatus_207: return "Multi-Status";
  3353. case StatusCode::AlreadyReported_208: return "Already Reported";
  3354. case StatusCode::IMUsed_226: return "IM Used";
  3355. case StatusCode::MultipleChoices_300: return "Multiple Choices";
  3356. case StatusCode::MovedPermanently_301: return "Moved Permanently";
  3357. case StatusCode::Found_302: return "Found";
  3358. case StatusCode::SeeOther_303: return "See Other";
  3359. case StatusCode::NotModified_304: return "Not Modified";
  3360. case StatusCode::UseProxy_305: return "Use Proxy";
  3361. case StatusCode::unused_306: return "unused";
  3362. case StatusCode::TemporaryRedirect_307: return "Temporary Redirect";
  3363. case StatusCode::PermanentRedirect_308: return "Permanent Redirect";
  3364. case StatusCode::BadRequest_400: return "Bad Request";
  3365. case StatusCode::Unauthorized_401: return "Unauthorized";
  3366. case StatusCode::PaymentRequired_402: return "Payment Required";
  3367. case StatusCode::Forbidden_403: return "Forbidden";
  3368. case StatusCode::NotFound_404: return "Not Found";
  3369. case StatusCode::MethodNotAllowed_405: return "Method Not Allowed";
  3370. case StatusCode::NotAcceptable_406: return "Not Acceptable";
  3371. case StatusCode::ProxyAuthenticationRequired_407:
  3372. return "Proxy Authentication Required";
  3373. case StatusCode::RequestTimeout_408: return "Request Timeout";
  3374. case StatusCode::Conflict_409: return "Conflict";
  3375. case StatusCode::Gone_410: return "Gone";
  3376. case StatusCode::LengthRequired_411: return "Length Required";
  3377. case StatusCode::PreconditionFailed_412: return "Precondition Failed";
  3378. case StatusCode::PayloadTooLarge_413: return "Payload Too Large";
  3379. case StatusCode::UriTooLong_414: return "URI Too Long";
  3380. case StatusCode::UnsupportedMediaType_415: return "Unsupported Media Type";
  3381. case StatusCode::RangeNotSatisfiable_416: return "Range Not Satisfiable";
  3382. case StatusCode::ExpectationFailed_417: return "Expectation Failed";
  3383. case StatusCode::ImATeapot_418: return "I'm a teapot";
  3384. case StatusCode::MisdirectedRequest_421: return "Misdirected Request";
  3385. case StatusCode::UnprocessableContent_422: return "Unprocessable Content";
  3386. case StatusCode::Locked_423: return "Locked";
  3387. case StatusCode::FailedDependency_424: return "Failed Dependency";
  3388. case StatusCode::TooEarly_425: return "Too Early";
  3389. case StatusCode::UpgradeRequired_426: return "Upgrade Required";
  3390. case StatusCode::PreconditionRequired_428: return "Precondition Required";
  3391. case StatusCode::TooManyRequests_429: return "Too Many Requests";
  3392. case StatusCode::RequestHeaderFieldsTooLarge_431:
  3393. return "Request Header Fields Too Large";
  3394. case StatusCode::UnavailableForLegalReasons_451:
  3395. return "Unavailable For Legal Reasons";
  3396. case StatusCode::NotImplemented_501: return "Not Implemented";
  3397. case StatusCode::BadGateway_502: return "Bad Gateway";
  3398. case StatusCode::ServiceUnavailable_503: return "Service Unavailable";
  3399. case StatusCode::GatewayTimeout_504: return "Gateway Timeout";
  3400. case StatusCode::HttpVersionNotSupported_505:
  3401. return "HTTP Version Not Supported";
  3402. case StatusCode::VariantAlsoNegotiates_506: return "Variant Also Negotiates";
  3403. case StatusCode::InsufficientStorage_507: return "Insufficient Storage";
  3404. case StatusCode::LoopDetected_508: return "Loop Detected";
  3405. case StatusCode::NotExtended_510: return "Not Extended";
  3406. case StatusCode::NetworkAuthenticationRequired_511:
  3407. return "Network Authentication Required";
  3408. default:
  3409. case StatusCode::InternalServerError_500: return "Internal Server Error";
  3410. }
  3411. }
  3412. std::string to_string(const Error error) {
  3413. switch (error) {
  3414. case Error::Success: return "Success (no error)";
  3415. case Error::Unknown: return "Unknown";
  3416. case Error::Connection: return "Could not establish connection";
  3417. case Error::BindIPAddress: return "Failed to bind IP address";
  3418. case Error::Read: return "Failed to read connection";
  3419. case Error::Write: return "Failed to write connection";
  3420. case Error::ExceedRedirectCount: return "Maximum redirect count exceeded";
  3421. case Error::Canceled: return "Connection handling canceled";
  3422. case Error::SSLConnection: return "SSL connection failed";
  3423. case Error::SSLLoadingCerts: return "SSL certificate loading failed";
  3424. case Error::SSLServerVerification: return "SSL server verification failed";
  3425. case Error::SSLServerHostnameVerification:
  3426. return "SSL server hostname verification failed";
  3427. case Error::UnsupportedMultipartBoundaryChars:
  3428. return "Unsupported HTTP multipart boundary characters";
  3429. case Error::Compression: return "Compression failed";
  3430. case Error::ConnectionTimeout: return "Connection timed out";
  3431. case Error::ProxyConnection: return "Proxy connection failed";
  3432. case Error::ConnectionClosed: return "Connection closed by server";
  3433. case Error::Timeout: return "Read timeout";
  3434. case Error::ResourceExhaustion: return "Resource exhaustion";
  3435. case Error::TooManyFormDataFiles: return "Too many form data files";
  3436. case Error::ExceedMaxPayloadSize: return "Exceeded maximum payload size";
  3437. case Error::ExceedUriMaxLength: return "Exceeded maximum URI length";
  3438. case Error::ExceedMaxSocketDescriptorCount:
  3439. return "Exceeded maximum socket descriptor count";
  3440. case Error::InvalidRequestLine: return "Invalid request line";
  3441. case Error::InvalidHTTPMethod: return "Invalid HTTP method";
  3442. case Error::InvalidHTTPVersion: return "Invalid HTTP version";
  3443. case Error::InvalidHeaders: return "Invalid headers";
  3444. case Error::MultipartParsing: return "Multipart parsing failed";
  3445. case Error::OpenFile: return "Failed to open file";
  3446. case Error::Listen: return "Failed to listen on socket";
  3447. case Error::GetSockName: return "Failed to get socket name";
  3448. case Error::UnsupportedAddressFamily: return "Unsupported address family";
  3449. case Error::HTTPParsing: return "HTTP parsing failed";
  3450. case Error::InvalidRangeHeader: return "Invalid Range header";
  3451. default: break;
  3452. }
  3453. return "Invalid";
  3454. }
  3455. std::ostream &operator<<(std::ostream &os, const Error &obj) {
  3456. os << to_string(obj);
  3457. os << " (" << static_cast<std::underlying_type<Error>::type>(obj) << ')';
  3458. return os;
  3459. }
  3460. std::string hosted_at(const std::string &hostname) {
  3461. std::vector<std::string> addrs;
  3462. hosted_at(hostname, addrs);
  3463. if (addrs.empty()) { return std::string(); }
  3464. return addrs[0];
  3465. }
  3466. void hosted_at(const std::string &hostname,
  3467. std::vector<std::string> &addrs) {
  3468. struct addrinfo hints;
  3469. struct addrinfo *result;
  3470. memset(&hints, 0, sizeof(struct addrinfo));
  3471. hints.ai_family = AF_UNSPEC;
  3472. hints.ai_socktype = SOCK_STREAM;
  3473. hints.ai_protocol = 0;
  3474. if (detail::getaddrinfo_with_timeout(hostname.c_str(), nullptr, &hints,
  3475. &result, 0)) {
  3476. #if defined __linux__ && !defined __ANDROID__
  3477. res_init();
  3478. #endif
  3479. return;
  3480. }
  3481. auto se = detail::scope_exit([&] { freeaddrinfo(result); });
  3482. for (auto rp = result; rp; rp = rp->ai_next) {
  3483. const auto &addr =
  3484. *reinterpret_cast<struct sockaddr_storage *>(rp->ai_addr);
  3485. std::string ip;
  3486. auto dummy = -1;
  3487. if (detail::get_ip_and_port(addr, sizeof(struct sockaddr_storage), ip,
  3488. dummy)) {
  3489. addrs.emplace_back(std::move(ip));
  3490. }
  3491. }
  3492. }
  3493. std::string encode_uri_component(const std::string &value) {
  3494. std::ostringstream escaped;
  3495. escaped.fill('0');
  3496. escaped << std::hex;
  3497. for (auto c : value) {
  3498. if (std::isalnum(static_cast<uint8_t>(c)) || c == '-' || c == '_' ||
  3499. c == '.' || c == '!' || c == '~' || c == '*' || c == '\'' || c == '(' ||
  3500. c == ')') {
  3501. escaped << c;
  3502. } else {
  3503. escaped << std::uppercase;
  3504. escaped << '%' << std::setw(2)
  3505. << static_cast<int>(static_cast<unsigned char>(c));
  3506. escaped << std::nouppercase;
  3507. }
  3508. }
  3509. return escaped.str();
  3510. }
  3511. std::string encode_uri(const std::string &value) {
  3512. std::ostringstream escaped;
  3513. escaped.fill('0');
  3514. escaped << std::hex;
  3515. for (auto c : value) {
  3516. if (std::isalnum(static_cast<uint8_t>(c)) || c == '-' || c == '_' ||
  3517. c == '.' || c == '!' || c == '~' || c == '*' || c == '\'' || c == '(' ||
  3518. c == ')' || c == ';' || c == '/' || c == '?' || c == ':' || c == '@' ||
  3519. c == '&' || c == '=' || c == '+' || c == '$' || c == ',' || c == '#') {
  3520. escaped << c;
  3521. } else {
  3522. escaped << std::uppercase;
  3523. escaped << '%' << std::setw(2)
  3524. << static_cast<int>(static_cast<unsigned char>(c));
  3525. escaped << std::nouppercase;
  3526. }
  3527. }
  3528. return escaped.str();
  3529. }
  3530. std::string decode_uri_component(const std::string &value) {
  3531. std::string result;
  3532. for (size_t i = 0; i < value.size(); i++) {
  3533. if (value[i] == '%' && i + 2 < value.size()) {
  3534. auto val = 0;
  3535. if (detail::from_hex_to_i(value, i + 1, 2, val)) {
  3536. result += static_cast<char>(val);
  3537. i += 2;
  3538. } else {
  3539. result += value[i];
  3540. }
  3541. } else {
  3542. result += value[i];
  3543. }
  3544. }
  3545. return result;
  3546. }
  3547. std::string decode_uri(const std::string &value) {
  3548. std::string result;
  3549. for (size_t i = 0; i < value.size(); i++) {
  3550. if (value[i] == '%' && i + 2 < value.size()) {
  3551. auto val = 0;
  3552. if (detail::from_hex_to_i(value, i + 1, 2, val)) {
  3553. result += static_cast<char>(val);
  3554. i += 2;
  3555. } else {
  3556. result += value[i];
  3557. }
  3558. } else {
  3559. result += value[i];
  3560. }
  3561. }
  3562. return result;
  3563. }
  3564. std::string encode_path_component(const std::string &component) {
  3565. std::string result;
  3566. result.reserve(component.size() * 3);
  3567. for (size_t i = 0; i < component.size(); i++) {
  3568. auto c = static_cast<unsigned char>(component[i]);
  3569. // Unreserved characters per RFC 3986: ALPHA / DIGIT / "-" / "." / "_" / "~"
  3570. if (std::isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~') {
  3571. result += static_cast<char>(c);
  3572. }
  3573. // Path-safe sub-delimiters: "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" /
  3574. // "," / ";" / "="
  3575. else if (c == '!' || c == '$' || c == '&' || c == '\'' || c == '(' ||
  3576. c == ')' || c == '*' || c == '+' || c == ',' || c == ';' ||
  3577. c == '=') {
  3578. result += static_cast<char>(c);
  3579. }
  3580. // Colon is allowed in path segments except first segment
  3581. else if (c == ':') {
  3582. result += static_cast<char>(c);
  3583. }
  3584. // @ is allowed in path
  3585. else if (c == '@') {
  3586. result += static_cast<char>(c);
  3587. } else {
  3588. result += '%';
  3589. char hex[3];
  3590. snprintf(hex, sizeof(hex), "%02X", c);
  3591. result.append(hex, 2);
  3592. }
  3593. }
  3594. return result;
  3595. }
  3596. std::string decode_path_component(const std::string &component) {
  3597. std::string result;
  3598. result.reserve(component.size());
  3599. for (size_t i = 0; i < component.size(); i++) {
  3600. if (component[i] == '%' && i + 1 < component.size()) {
  3601. if (component[i + 1] == 'u') {
  3602. // Unicode %uXXXX encoding
  3603. auto val = 0;
  3604. if (detail::from_hex_to_i(component, i + 2, 4, val)) {
  3605. // 4 digits Unicode codes
  3606. char buff[4];
  3607. size_t len = detail::to_utf8(val, buff);
  3608. if (len > 0) { result.append(buff, len); }
  3609. i += 5; // 'u0000'
  3610. } else {
  3611. result += component[i];
  3612. }
  3613. } else {
  3614. // Standard %XX encoding
  3615. auto val = 0;
  3616. if (detail::from_hex_to_i(component, i + 1, 2, val)) {
  3617. // 2 digits hex codes
  3618. result += static_cast<char>(val);
  3619. i += 2; // 'XX'
  3620. } else {
  3621. result += component[i];
  3622. }
  3623. }
  3624. } else {
  3625. result += component[i];
  3626. }
  3627. }
  3628. return result;
  3629. }
  3630. std::string encode_query_component(const std::string &component,
  3631. bool space_as_plus) {
  3632. std::string result;
  3633. result.reserve(component.size() * 3);
  3634. for (size_t i = 0; i < component.size(); i++) {
  3635. auto c = static_cast<unsigned char>(component[i]);
  3636. // Unreserved characters per RFC 3986
  3637. if (std::isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~') {
  3638. result += static_cast<char>(c);
  3639. }
  3640. // Space handling
  3641. else if (c == ' ') {
  3642. if (space_as_plus) {
  3643. result += '+';
  3644. } else {
  3645. result += "%20";
  3646. }
  3647. }
  3648. // Plus sign handling
  3649. else if (c == '+') {
  3650. if (space_as_plus) {
  3651. result += "%2B";
  3652. } else {
  3653. result += static_cast<char>(c);
  3654. }
  3655. }
  3656. // Query-safe sub-delimiters (excluding & and = which are query delimiters)
  3657. else if (c == '!' || c == '$' || c == '\'' || c == '(' || c == ')' ||
  3658. c == '*' || c == ',' || c == ';') {
  3659. result += static_cast<char>(c);
  3660. }
  3661. // Colon and @ are allowed in query
  3662. else if (c == ':' || c == '@') {
  3663. result += static_cast<char>(c);
  3664. }
  3665. // Forward slash is allowed in query values
  3666. else if (c == '/') {
  3667. result += static_cast<char>(c);
  3668. }
  3669. // Question mark is allowed in query values (after first ?)
  3670. else if (c == '?') {
  3671. result += static_cast<char>(c);
  3672. } else {
  3673. result += '%';
  3674. char hex[3];
  3675. snprintf(hex, sizeof(hex), "%02X", c);
  3676. result.append(hex, 2);
  3677. }
  3678. }
  3679. return result;
  3680. }
  3681. std::string decode_query_component(const std::string &component,
  3682. bool plus_as_space) {
  3683. std::string result;
  3684. result.reserve(component.size());
  3685. for (size_t i = 0; i < component.size(); i++) {
  3686. if (component[i] == '%' && i + 2 < component.size()) {
  3687. std::string hex = component.substr(i + 1, 2);
  3688. char *end;
  3689. unsigned long value = std::strtoul(hex.c_str(), &end, 16);
  3690. if (end == hex.c_str() + 2) {
  3691. result += static_cast<char>(value);
  3692. i += 2;
  3693. } else {
  3694. result += component[i];
  3695. }
  3696. } else if (component[i] == '+' && plus_as_space) {
  3697. result += ' '; // + becomes space in form-urlencoded
  3698. } else {
  3699. result += component[i];
  3700. }
  3701. }
  3702. return result;
  3703. }
  3704. std::string append_query_params(const std::string &path,
  3705. const Params &params) {
  3706. std::string path_with_query = path;
  3707. thread_local const std::regex re("[^?]+\\?.*");
  3708. auto delm = std::regex_match(path, re) ? '&' : '?';
  3709. path_with_query += delm + detail::params_to_query_str(params);
  3710. return path_with_query;
  3711. }
  3712. // Header utilities
  3713. std::pair<std::string, std::string>
  3714. make_range_header(const Ranges &ranges) {
  3715. std::string field = "bytes=";
  3716. auto i = 0;
  3717. for (const auto &r : ranges) {
  3718. if (i != 0) { field += ", "; }
  3719. if (r.first != -1) { field += std::to_string(r.first); }
  3720. field += '-';
  3721. if (r.second != -1) { field += std::to_string(r.second); }
  3722. i++;
  3723. }
  3724. return std::make_pair("Range", std::move(field));
  3725. }
  3726. std::pair<std::string, std::string>
  3727. make_basic_authentication_header(const std::string &username,
  3728. const std::string &password, bool is_proxy) {
  3729. auto field = "Basic " + detail::base64_encode(username + ":" + password);
  3730. auto key = is_proxy ? "Proxy-Authorization" : "Authorization";
  3731. return std::make_pair(key, std::move(field));
  3732. }
  3733. std::pair<std::string, std::string>
  3734. make_bearer_token_authentication_header(const std::string &token,
  3735. bool is_proxy = false) {
  3736. auto field = "Bearer " + token;
  3737. auto key = is_proxy ? "Proxy-Authorization" : "Authorization";
  3738. return std::make_pair(key, std::move(field));
  3739. }
  3740. // Request implementation
  3741. bool Request::has_header(const std::string &key) const {
  3742. return detail::has_header(headers, key);
  3743. }
  3744. std::string Request::get_header_value(const std::string &key,
  3745. const char *def, size_t id) const {
  3746. return detail::get_header_value(headers, key, def, id);
  3747. }
  3748. size_t Request::get_header_value_count(const std::string &key) const {
  3749. auto r = headers.equal_range(key);
  3750. return static_cast<size_t>(std::distance(r.first, r.second));
  3751. }
  3752. void Request::set_header(const std::string &key,
  3753. const std::string &val) {
  3754. if (detail::fields::is_field_name(key) &&
  3755. detail::fields::is_field_value(val)) {
  3756. headers.emplace(key, val);
  3757. }
  3758. }
  3759. bool Request::has_trailer(const std::string &key) const {
  3760. return trailers.find(key) != trailers.end();
  3761. }
  3762. std::string Request::get_trailer_value(const std::string &key,
  3763. size_t id) const {
  3764. auto rng = trailers.equal_range(key);
  3765. auto it = rng.first;
  3766. std::advance(it, static_cast<ssize_t>(id));
  3767. if (it != rng.second) { return it->second; }
  3768. return std::string();
  3769. }
  3770. size_t Request::get_trailer_value_count(const std::string &key) const {
  3771. auto r = trailers.equal_range(key);
  3772. return static_cast<size_t>(std::distance(r.first, r.second));
  3773. }
  3774. bool Request::has_param(const std::string &key) const {
  3775. return params.find(key) != params.end();
  3776. }
  3777. std::string Request::get_param_value(const std::string &key,
  3778. size_t id) const {
  3779. auto rng = params.equal_range(key);
  3780. auto it = rng.first;
  3781. std::advance(it, static_cast<ssize_t>(id));
  3782. if (it != rng.second) { return it->second; }
  3783. return std::string();
  3784. }
  3785. size_t Request::get_param_value_count(const std::string &key) const {
  3786. auto r = params.equal_range(key);
  3787. return static_cast<size_t>(std::distance(r.first, r.second));
  3788. }
  3789. bool Request::is_multipart_form_data() const {
  3790. const auto &content_type = get_header_value("Content-Type");
  3791. return !content_type.rfind("multipart/form-data", 0);
  3792. }
  3793. // Multipart FormData implementation
  3794. std::string MultipartFormData::get_field(const std::string &key,
  3795. size_t id) const {
  3796. auto rng = fields.equal_range(key);
  3797. auto it = rng.first;
  3798. std::advance(it, static_cast<ssize_t>(id));
  3799. if (it != rng.second) { return it->second.content; }
  3800. return std::string();
  3801. }
  3802. std::vector<std::string>
  3803. MultipartFormData::get_fields(const std::string &key) const {
  3804. std::vector<std::string> values;
  3805. auto rng = fields.equal_range(key);
  3806. for (auto it = rng.first; it != rng.second; it++) {
  3807. values.push_back(it->second.content);
  3808. }
  3809. return values;
  3810. }
  3811. bool MultipartFormData::has_field(const std::string &key) const {
  3812. return fields.find(key) != fields.end();
  3813. }
  3814. size_t MultipartFormData::get_field_count(const std::string &key) const {
  3815. auto r = fields.equal_range(key);
  3816. return static_cast<size_t>(std::distance(r.first, r.second));
  3817. }
  3818. FormData MultipartFormData::get_file(const std::string &key,
  3819. size_t id) const {
  3820. auto rng = files.equal_range(key);
  3821. auto it = rng.first;
  3822. std::advance(it, static_cast<ssize_t>(id));
  3823. if (it != rng.second) { return it->second; }
  3824. return FormData();
  3825. }
  3826. std::vector<FormData>
  3827. MultipartFormData::get_files(const std::string &key) const {
  3828. std::vector<FormData> values;
  3829. auto rng = files.equal_range(key);
  3830. for (auto it = rng.first; it != rng.second; it++) {
  3831. values.push_back(it->second);
  3832. }
  3833. return values;
  3834. }
  3835. bool MultipartFormData::has_file(const std::string &key) const {
  3836. return files.find(key) != files.end();
  3837. }
  3838. size_t MultipartFormData::get_file_count(const std::string &key) const {
  3839. auto r = files.equal_range(key);
  3840. return static_cast<size_t>(std::distance(r.first, r.second));
  3841. }
  3842. // Response implementation
  3843. bool Response::has_header(const std::string &key) const {
  3844. return headers.find(key) != headers.end();
  3845. }
  3846. std::string Response::get_header_value(const std::string &key,
  3847. const char *def,
  3848. size_t id) const {
  3849. return detail::get_header_value(headers, key, def, id);
  3850. }
  3851. size_t Response::get_header_value_count(const std::string &key) const {
  3852. auto r = headers.equal_range(key);
  3853. return static_cast<size_t>(std::distance(r.first, r.second));
  3854. }
  3855. void Response::set_header(const std::string &key,
  3856. const std::string &val) {
  3857. if (detail::fields::is_field_name(key) &&
  3858. detail::fields::is_field_value(val)) {
  3859. headers.emplace(key, val);
  3860. }
  3861. }
  3862. bool Response::has_trailer(const std::string &key) const {
  3863. return trailers.find(key) != trailers.end();
  3864. }
  3865. std::string Response::get_trailer_value(const std::string &key,
  3866. size_t id) const {
  3867. auto rng = trailers.equal_range(key);
  3868. auto it = rng.first;
  3869. std::advance(it, static_cast<ssize_t>(id));
  3870. if (it != rng.second) { return it->second; }
  3871. return std::string();
  3872. }
  3873. size_t Response::get_trailer_value_count(const std::string &key) const {
  3874. auto r = trailers.equal_range(key);
  3875. return static_cast<size_t>(std::distance(r.first, r.second));
  3876. }
  3877. void Response::set_redirect(const std::string &url, int stat) {
  3878. if (detail::fields::is_field_value(url)) {
  3879. set_header("Location", url);
  3880. if (300 <= stat && stat < 400) {
  3881. this->status = stat;
  3882. } else {
  3883. this->status = StatusCode::Found_302;
  3884. }
  3885. }
  3886. }
  3887. void Response::set_content(const char *s, size_t n,
  3888. const std::string &content_type) {
  3889. body.assign(s, n);
  3890. auto rng = headers.equal_range("Content-Type");
  3891. headers.erase(rng.first, rng.second);
  3892. set_header("Content-Type", content_type);
  3893. }
  3894. void Response::set_content(const std::string &s,
  3895. const std::string &content_type) {
  3896. set_content(s.data(), s.size(), content_type);
  3897. }
  3898. void Response::set_content(std::string &&s,
  3899. const std::string &content_type) {
  3900. body = std::move(s);
  3901. auto rng = headers.equal_range("Content-Type");
  3902. headers.erase(rng.first, rng.second);
  3903. set_header("Content-Type", content_type);
  3904. }
  3905. void Response::set_content_provider(
  3906. size_t in_length, const std::string &content_type, ContentProvider provider,
  3907. ContentProviderResourceReleaser resource_releaser) {
  3908. set_header("Content-Type", content_type);
  3909. content_length_ = in_length;
  3910. if (in_length > 0) { content_provider_ = std::move(provider); }
  3911. content_provider_resource_releaser_ = std::move(resource_releaser);
  3912. is_chunked_content_provider_ = false;
  3913. }
  3914. void Response::set_content_provider(
  3915. const std::string &content_type, ContentProviderWithoutLength provider,
  3916. ContentProviderResourceReleaser resource_releaser) {
  3917. set_header("Content-Type", content_type);
  3918. content_length_ = 0;
  3919. content_provider_ = detail::ContentProviderAdapter(std::move(provider));
  3920. content_provider_resource_releaser_ = std::move(resource_releaser);
  3921. is_chunked_content_provider_ = false;
  3922. }
  3923. void Response::set_chunked_content_provider(
  3924. const std::string &content_type, ContentProviderWithoutLength provider,
  3925. ContentProviderResourceReleaser resource_releaser) {
  3926. set_header("Content-Type", content_type);
  3927. content_length_ = 0;
  3928. content_provider_ = detail::ContentProviderAdapter(std::move(provider));
  3929. content_provider_resource_releaser_ = std::move(resource_releaser);
  3930. is_chunked_content_provider_ = true;
  3931. }
  3932. void Response::set_file_content(const std::string &path,
  3933. const std::string &content_type) {
  3934. file_content_path_ = path;
  3935. file_content_content_type_ = content_type;
  3936. }
  3937. void Response::set_file_content(const std::string &path) {
  3938. file_content_path_ = path;
  3939. }
  3940. // Result implementation
  3941. bool Result::has_request_header(const std::string &key) const {
  3942. return request_headers_.find(key) != request_headers_.end();
  3943. }
  3944. std::string Result::get_request_header_value(const std::string &key,
  3945. const char *def,
  3946. size_t id) const {
  3947. return detail::get_header_value(request_headers_, key, def, id);
  3948. }
  3949. size_t
  3950. Result::get_request_header_value_count(const std::string &key) const {
  3951. auto r = request_headers_.equal_range(key);
  3952. return static_cast<size_t>(std::distance(r.first, r.second));
  3953. }
  3954. // Stream implementation
  3955. ssize_t Stream::write(const char *ptr) {
  3956. return write(ptr, strlen(ptr));
  3957. }
  3958. ssize_t Stream::write(const std::string &s) {
  3959. return write(s.data(), s.size());
  3960. }
  3961. // BodyReader implementation
  3962. ssize_t detail::BodyReader::read(char *buf, size_t len) {
  3963. if (!stream) {
  3964. last_error = Error::Connection;
  3965. return -1;
  3966. }
  3967. if (eof) { return 0; }
  3968. if (!chunked) {
  3969. // Content-Length based reading
  3970. if (bytes_read >= content_length) {
  3971. eof = true;
  3972. return 0;
  3973. }
  3974. auto remaining = content_length - bytes_read;
  3975. auto to_read = (std::min)(len, remaining);
  3976. auto n = stream->read(buf, to_read);
  3977. if (n < 0) {
  3978. last_error = stream->get_error();
  3979. if (last_error == Error::Success) { last_error = Error::Read; }
  3980. eof = true;
  3981. return n;
  3982. }
  3983. if (n == 0) {
  3984. // Unexpected EOF before content_length
  3985. last_error = stream->get_error();
  3986. if (last_error == Error::Success) { last_error = Error::Read; }
  3987. eof = true;
  3988. return 0;
  3989. }
  3990. bytes_read += static_cast<size_t>(n);
  3991. if (bytes_read >= content_length) { eof = true; }
  3992. return n;
  3993. }
  3994. // Chunked transfer encoding: delegate to shared decoder instance.
  3995. if (!chunked_decoder) { chunked_decoder.reset(new ChunkedDecoder(*stream)); }
  3996. size_t chunk_offset = 0;
  3997. size_t chunk_total = 0;
  3998. auto n = chunked_decoder->read_payload(buf, len, chunk_offset, chunk_total);
  3999. if (n < 0) {
  4000. last_error = stream->get_error();
  4001. if (last_error == Error::Success) { last_error = Error::Read; }
  4002. eof = true;
  4003. return n;
  4004. }
  4005. if (n == 0) {
  4006. // Final chunk observed. Leave trailer parsing to the caller (StreamHandle).
  4007. eof = true;
  4008. return 0;
  4009. }
  4010. bytes_read += static_cast<size_t>(n);
  4011. return n;
  4012. }
  4013. namespace detail {
  4014. void calc_actual_timeout(time_t max_timeout_msec, time_t duration_msec,
  4015. time_t timeout_sec, time_t timeout_usec,
  4016. time_t &actual_timeout_sec,
  4017. time_t &actual_timeout_usec) {
  4018. auto timeout_msec = (timeout_sec * 1000) + (timeout_usec / 1000);
  4019. auto actual_timeout_msec =
  4020. (std::min)(max_timeout_msec - duration_msec, timeout_msec);
  4021. if (actual_timeout_msec < 0) { actual_timeout_msec = 0; }
  4022. actual_timeout_sec = actual_timeout_msec / 1000;
  4023. actual_timeout_usec = (actual_timeout_msec % 1000) * 1000;
  4024. }
  4025. // Socket stream implementation
  4026. SocketStream::SocketStream(
  4027. socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec,
  4028. time_t write_timeout_sec, time_t write_timeout_usec,
  4029. time_t max_timeout_msec,
  4030. std::chrono::time_point<std::chrono::steady_clock> start_time)
  4031. : sock_(sock), read_timeout_sec_(read_timeout_sec),
  4032. read_timeout_usec_(read_timeout_usec),
  4033. write_timeout_sec_(write_timeout_sec),
  4034. write_timeout_usec_(write_timeout_usec),
  4035. max_timeout_msec_(max_timeout_msec), start_time_(start_time),
  4036. read_buff_(read_buff_size_, 0) {}
  4037. SocketStream::~SocketStream() = default;
  4038. bool SocketStream::is_readable() const {
  4039. return read_buff_off_ < read_buff_content_size_;
  4040. }
  4041. bool SocketStream::wait_readable() const {
  4042. if (max_timeout_msec_ <= 0) {
  4043. return select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0;
  4044. }
  4045. time_t read_timeout_sec;
  4046. time_t read_timeout_usec;
  4047. calc_actual_timeout(max_timeout_msec_, duration(), read_timeout_sec_,
  4048. read_timeout_usec_, read_timeout_sec, read_timeout_usec);
  4049. return select_read(sock_, read_timeout_sec, read_timeout_usec) > 0;
  4050. }
  4051. bool SocketStream::wait_writable() const {
  4052. return select_write(sock_, write_timeout_sec_, write_timeout_usec_) > 0 &&
  4053. is_socket_alive(sock_);
  4054. }
  4055. ssize_t SocketStream::read(char *ptr, size_t size) {
  4056. #ifdef _WIN32
  4057. size =
  4058. (std::min)(size, static_cast<size_t>((std::numeric_limits<int>::max)()));
  4059. #else
  4060. size = (std::min)(size,
  4061. static_cast<size_t>((std::numeric_limits<ssize_t>::max)()));
  4062. #endif
  4063. if (read_buff_off_ < read_buff_content_size_) {
  4064. auto remaining_size = read_buff_content_size_ - read_buff_off_;
  4065. if (size <= remaining_size) {
  4066. memcpy(ptr, read_buff_.data() + read_buff_off_, size);
  4067. read_buff_off_ += size;
  4068. return static_cast<ssize_t>(size);
  4069. } else {
  4070. memcpy(ptr, read_buff_.data() + read_buff_off_, remaining_size);
  4071. read_buff_off_ += remaining_size;
  4072. return static_cast<ssize_t>(remaining_size);
  4073. }
  4074. }
  4075. if (!wait_readable()) {
  4076. error_ = Error::Timeout;
  4077. return -1;
  4078. }
  4079. read_buff_off_ = 0;
  4080. read_buff_content_size_ = 0;
  4081. if (size < read_buff_size_) {
  4082. auto n = read_socket(sock_, read_buff_.data(), read_buff_size_,
  4083. CPPHTTPLIB_RECV_FLAGS);
  4084. if (n <= 0) {
  4085. if (n == 0) {
  4086. error_ = Error::ConnectionClosed;
  4087. } else {
  4088. error_ = Error::Read;
  4089. }
  4090. return n;
  4091. } else if (n <= static_cast<ssize_t>(size)) {
  4092. memcpy(ptr, read_buff_.data(), static_cast<size_t>(n));
  4093. return n;
  4094. } else {
  4095. memcpy(ptr, read_buff_.data(), size);
  4096. read_buff_off_ = size;
  4097. read_buff_content_size_ = static_cast<size_t>(n);
  4098. return static_cast<ssize_t>(size);
  4099. }
  4100. } else {
  4101. auto n = read_socket(sock_, ptr, size, CPPHTTPLIB_RECV_FLAGS);
  4102. if (n <= 0) {
  4103. if (n == 0) {
  4104. error_ = Error::ConnectionClosed;
  4105. } else {
  4106. error_ = Error::Read;
  4107. }
  4108. }
  4109. return n;
  4110. }
  4111. }
  4112. ssize_t SocketStream::write(const char *ptr, size_t size) {
  4113. if (!wait_writable()) { return -1; }
  4114. #if defined(_WIN32) && !defined(_WIN64)
  4115. size =
  4116. (std::min)(size, static_cast<size_t>((std::numeric_limits<int>::max)()));
  4117. #endif
  4118. return send_socket(sock_, ptr, size, CPPHTTPLIB_SEND_FLAGS);
  4119. }
  4120. void SocketStream::get_remote_ip_and_port(std::string &ip,
  4121. int &port) const {
  4122. return detail::get_remote_ip_and_port(sock_, ip, port);
  4123. }
  4124. void SocketStream::get_local_ip_and_port(std::string &ip,
  4125. int &port) const {
  4126. return detail::get_local_ip_and_port(sock_, ip, port);
  4127. }
  4128. socket_t SocketStream::socket() const { return sock_; }
  4129. time_t SocketStream::duration() const {
  4130. return std::chrono::duration_cast<std::chrono::milliseconds>(
  4131. std::chrono::steady_clock::now() - start_time_)
  4132. .count();
  4133. }
  4134. // Buffer stream implementation
  4135. bool BufferStream::is_readable() const { return true; }
  4136. bool BufferStream::wait_readable() const { return true; }
  4137. bool BufferStream::wait_writable() const { return true; }
  4138. ssize_t BufferStream::read(char *ptr, size_t size) {
  4139. #if defined(_MSC_VER) && _MSC_VER < 1910
  4140. auto len_read = buffer._Copy_s(ptr, size, size, position);
  4141. #else
  4142. auto len_read = buffer.copy(ptr, size, position);
  4143. #endif
  4144. position += static_cast<size_t>(len_read);
  4145. return static_cast<ssize_t>(len_read);
  4146. }
  4147. ssize_t BufferStream::write(const char *ptr, size_t size) {
  4148. buffer.append(ptr, size);
  4149. return static_cast<ssize_t>(size);
  4150. }
  4151. void BufferStream::get_remote_ip_and_port(std::string & /*ip*/,
  4152. int & /*port*/) const {}
  4153. void BufferStream::get_local_ip_and_port(std::string & /*ip*/,
  4154. int & /*port*/) const {}
  4155. socket_t BufferStream::socket() const { return 0; }
  4156. time_t BufferStream::duration() const { return 0; }
  4157. const std::string &BufferStream::get_buffer() const { return buffer; }
  4158. PathParamsMatcher::PathParamsMatcher(const std::string &pattern)
  4159. : MatcherBase(pattern) {
  4160. constexpr const char marker[] = "/:";
  4161. // One past the last ending position of a path param substring
  4162. std::size_t last_param_end = 0;
  4163. #ifndef CPPHTTPLIB_NO_EXCEPTIONS
  4164. // Needed to ensure that parameter names are unique during matcher
  4165. // construction
  4166. // If exceptions are disabled, only last duplicate path
  4167. // parameter will be set
  4168. std::unordered_set<std::string> param_name_set;
  4169. #endif
  4170. while (true) {
  4171. const auto marker_pos = pattern.find(
  4172. marker, last_param_end == 0 ? last_param_end : last_param_end - 1);
  4173. if (marker_pos == std::string::npos) { break; }
  4174. static_fragments_.push_back(
  4175. pattern.substr(last_param_end, marker_pos - last_param_end + 1));
  4176. const auto param_name_start = marker_pos + str_len(marker);
  4177. auto sep_pos = pattern.find(separator, param_name_start);
  4178. if (sep_pos == std::string::npos) { sep_pos = pattern.length(); }
  4179. auto param_name =
  4180. pattern.substr(param_name_start, sep_pos - param_name_start);
  4181. #ifndef CPPHTTPLIB_NO_EXCEPTIONS
  4182. if (param_name_set.find(param_name) != param_name_set.cend()) {
  4183. std::string msg = "Encountered path parameter '" + param_name +
  4184. "' multiple times in route pattern '" + pattern + "'.";
  4185. throw std::invalid_argument(msg);
  4186. }
  4187. #endif
  4188. param_names_.push_back(std::move(param_name));
  4189. last_param_end = sep_pos + 1;
  4190. }
  4191. if (last_param_end < pattern.length()) {
  4192. static_fragments_.push_back(pattern.substr(last_param_end));
  4193. }
  4194. }
  4195. bool PathParamsMatcher::match(Request &request) const {
  4196. request.matches = std::smatch();
  4197. request.path_params.clear();
  4198. request.path_params.reserve(param_names_.size());
  4199. // One past the position at which the path matched the pattern last time
  4200. std::size_t starting_pos = 0;
  4201. for (size_t i = 0; i < static_fragments_.size(); ++i) {
  4202. const auto &fragment = static_fragments_[i];
  4203. if (starting_pos + fragment.length() > request.path.length()) {
  4204. return false;
  4205. }
  4206. // Avoid unnecessary allocation by using strncmp instead of substr +
  4207. // comparison
  4208. if (std::strncmp(request.path.c_str() + starting_pos, fragment.c_str(),
  4209. fragment.length()) != 0) {
  4210. return false;
  4211. }
  4212. starting_pos += fragment.length();
  4213. // Should only happen when we have a static fragment after a param
  4214. // Example: '/users/:id/subscriptions'
  4215. // The 'subscriptions' fragment here does not have a corresponding param
  4216. if (i >= param_names_.size()) { continue; }
  4217. auto sep_pos = request.path.find(separator, starting_pos);
  4218. if (sep_pos == std::string::npos) { sep_pos = request.path.length(); }
  4219. const auto &param_name = param_names_[i];
  4220. request.path_params.emplace(
  4221. param_name, request.path.substr(starting_pos, sep_pos - starting_pos));
  4222. // Mark everything up to '/' as matched
  4223. starting_pos = sep_pos + 1;
  4224. }
  4225. // Returns false if the path is longer than the pattern
  4226. return starting_pos >= request.path.length();
  4227. }
  4228. bool RegexMatcher::match(Request &request) const {
  4229. request.path_params.clear();
  4230. return std::regex_match(request.path, request.matches, regex_);
  4231. }
  4232. // Enclose IPv6 address in brackets if needed
  4233. std::string prepare_host_string(const std::string &host) {
  4234. // Enclose IPv6 address in brackets (but not if already enclosed)
  4235. if (host.find(':') == std::string::npos ||
  4236. (!host.empty() && host[0] == '[')) {
  4237. // IPv4, hostname, or already bracketed IPv6
  4238. return host;
  4239. } else {
  4240. // IPv6 address without brackets
  4241. return "[" + host + "]";
  4242. }
  4243. }
  4244. std::string make_host_and_port_string(const std::string &host, int port,
  4245. bool is_ssl) {
  4246. auto result = prepare_host_string(host);
  4247. // Append port if not default
  4248. if ((!is_ssl && port == 80) || (is_ssl && port == 443)) {
  4249. ; // do nothing
  4250. } else {
  4251. result += ":" + std::to_string(port);
  4252. }
  4253. return result;
  4254. }
  4255. // Create "host:port" string always including port number (for CONNECT method)
  4256. std::string
  4257. make_host_and_port_string_always_port(const std::string &host, int port) {
  4258. return prepare_host_string(host) + ":" + std::to_string(port);
  4259. }
  4260. template <typename T>
  4261. bool check_and_write_headers(Stream &strm, Headers &headers,
  4262. T header_writer, Error &error) {
  4263. for (const auto &h : headers) {
  4264. if (!detail::fields::is_field_name(h.first) ||
  4265. !detail::fields::is_field_value(h.second)) {
  4266. error = Error::InvalidHeaders;
  4267. return false;
  4268. }
  4269. }
  4270. if (header_writer(strm, headers) <= 0) {
  4271. error = Error::Write;
  4272. return false;
  4273. }
  4274. return true;
  4275. }
  4276. } // namespace detail
  4277. // HTTP server implementation
  4278. Server::Server()
  4279. : new_task_queue(
  4280. [] { return new ThreadPool(CPPHTTPLIB_THREAD_POOL_COUNT); }) {
  4281. #ifndef _WIN32
  4282. signal(SIGPIPE, SIG_IGN);
  4283. #endif
  4284. }
  4285. Server::~Server() = default;
  4286. std::unique_ptr<detail::MatcherBase>
  4287. Server::make_matcher(const std::string &pattern) {
  4288. if (pattern.find("/:") != std::string::npos) {
  4289. return detail::make_unique<detail::PathParamsMatcher>(pattern);
  4290. } else {
  4291. return detail::make_unique<detail::RegexMatcher>(pattern);
  4292. }
  4293. }
  4294. Server &Server::Get(const std::string &pattern, Handler handler) {
  4295. get_handlers_.emplace_back(make_matcher(pattern), std::move(handler));
  4296. return *this;
  4297. }
  4298. Server &Server::Post(const std::string &pattern, Handler handler) {
  4299. post_handlers_.emplace_back(make_matcher(pattern), std::move(handler));
  4300. return *this;
  4301. }
  4302. Server &Server::Post(const std::string &pattern,
  4303. HandlerWithContentReader handler) {
  4304. post_handlers_for_content_reader_.emplace_back(make_matcher(pattern),
  4305. std::move(handler));
  4306. return *this;
  4307. }
  4308. Server &Server::Put(const std::string &pattern, Handler handler) {
  4309. put_handlers_.emplace_back(make_matcher(pattern), std::move(handler));
  4310. return *this;
  4311. }
  4312. Server &Server::Put(const std::string &pattern,
  4313. HandlerWithContentReader handler) {
  4314. put_handlers_for_content_reader_.emplace_back(make_matcher(pattern),
  4315. std::move(handler));
  4316. return *this;
  4317. }
  4318. Server &Server::Patch(const std::string &pattern, Handler handler) {
  4319. patch_handlers_.emplace_back(make_matcher(pattern), std::move(handler));
  4320. return *this;
  4321. }
  4322. Server &Server::Patch(const std::string &pattern,
  4323. HandlerWithContentReader handler) {
  4324. patch_handlers_for_content_reader_.emplace_back(make_matcher(pattern),
  4325. std::move(handler));
  4326. return *this;
  4327. }
  4328. Server &Server::Delete(const std::string &pattern, Handler handler) {
  4329. delete_handlers_.emplace_back(make_matcher(pattern), std::move(handler));
  4330. return *this;
  4331. }
  4332. Server &Server::Delete(const std::string &pattern,
  4333. HandlerWithContentReader handler) {
  4334. delete_handlers_for_content_reader_.emplace_back(make_matcher(pattern),
  4335. std::move(handler));
  4336. return *this;
  4337. }
  4338. Server &Server::Options(const std::string &pattern, Handler handler) {
  4339. options_handlers_.emplace_back(make_matcher(pattern), std::move(handler));
  4340. return *this;
  4341. }
  4342. bool Server::set_base_dir(const std::string &dir,
  4343. const std::string &mount_point) {
  4344. return set_mount_point(mount_point, dir);
  4345. }
  4346. bool Server::set_mount_point(const std::string &mount_point,
  4347. const std::string &dir, Headers headers) {
  4348. detail::FileStat stat(dir);
  4349. if (stat.is_dir()) {
  4350. std::string mnt = !mount_point.empty() ? mount_point : "/";
  4351. if (!mnt.empty() && mnt[0] == '/') {
  4352. base_dirs_.push_back({std::move(mnt), dir, std::move(headers)});
  4353. return true;
  4354. }
  4355. }
  4356. return false;
  4357. }
  4358. bool Server::remove_mount_point(const std::string &mount_point) {
  4359. for (auto it = base_dirs_.begin(); it != base_dirs_.end(); ++it) {
  4360. if (it->mount_point == mount_point) {
  4361. base_dirs_.erase(it);
  4362. return true;
  4363. }
  4364. }
  4365. return false;
  4366. }
  4367. Server &
  4368. Server::set_file_extension_and_mimetype_mapping(const std::string &ext,
  4369. const std::string &mime) {
  4370. file_extension_and_mimetype_map_[ext] = mime;
  4371. return *this;
  4372. }
  4373. Server &Server::set_default_file_mimetype(const std::string &mime) {
  4374. default_file_mimetype_ = mime;
  4375. return *this;
  4376. }
  4377. Server &Server::set_file_request_handler(Handler handler) {
  4378. file_request_handler_ = std::move(handler);
  4379. return *this;
  4380. }
  4381. Server &Server::set_error_handler_core(HandlerWithResponse handler,
  4382. std::true_type) {
  4383. error_handler_ = std::move(handler);
  4384. return *this;
  4385. }
  4386. Server &Server::set_error_handler_core(Handler handler,
  4387. std::false_type) {
  4388. error_handler_ = [handler](const Request &req, Response &res) {
  4389. handler(req, res);
  4390. return HandlerResponse::Handled;
  4391. };
  4392. return *this;
  4393. }
  4394. Server &Server::set_exception_handler(ExceptionHandler handler) {
  4395. exception_handler_ = std::move(handler);
  4396. return *this;
  4397. }
  4398. Server &Server::set_pre_routing_handler(HandlerWithResponse handler) {
  4399. pre_routing_handler_ = std::move(handler);
  4400. return *this;
  4401. }
  4402. Server &Server::set_post_routing_handler(Handler handler) {
  4403. post_routing_handler_ = std::move(handler);
  4404. return *this;
  4405. }
  4406. Server &Server::set_pre_request_handler(HandlerWithResponse handler) {
  4407. pre_request_handler_ = std::move(handler);
  4408. return *this;
  4409. }
  4410. Server &Server::set_logger(Logger logger) {
  4411. logger_ = std::move(logger);
  4412. return *this;
  4413. }
  4414. Server &Server::set_error_logger(ErrorLogger error_logger) {
  4415. error_logger_ = std::move(error_logger);
  4416. return *this;
  4417. }
  4418. Server &Server::set_pre_compression_logger(Logger logger) {
  4419. pre_compression_logger_ = std::move(logger);
  4420. return *this;
  4421. }
  4422. Server &
  4423. Server::set_expect_100_continue_handler(Expect100ContinueHandler handler) {
  4424. expect_100_continue_handler_ = std::move(handler);
  4425. return *this;
  4426. }
  4427. Server &Server::set_address_family(int family) {
  4428. address_family_ = family;
  4429. return *this;
  4430. }
  4431. Server &Server::set_tcp_nodelay(bool on) {
  4432. tcp_nodelay_ = on;
  4433. return *this;
  4434. }
  4435. Server &Server::set_ipv6_v6only(bool on) {
  4436. ipv6_v6only_ = on;
  4437. return *this;
  4438. }
  4439. Server &Server::set_socket_options(SocketOptions socket_options) {
  4440. socket_options_ = std::move(socket_options);
  4441. return *this;
  4442. }
  4443. Server &Server::set_default_headers(Headers headers) {
  4444. default_headers_ = std::move(headers);
  4445. return *this;
  4446. }
  4447. Server &Server::set_header_writer(
  4448. std::function<ssize_t(Stream &, Headers &)> const &writer) {
  4449. header_writer_ = writer;
  4450. return *this;
  4451. }
  4452. Server &
  4453. Server::set_trusted_proxies(const std::vector<std::string> &proxies) {
  4454. trusted_proxies_ = proxies;
  4455. return *this;
  4456. }
  4457. Server &Server::set_keep_alive_max_count(size_t count) {
  4458. keep_alive_max_count_ = count;
  4459. return *this;
  4460. }
  4461. Server &Server::set_keep_alive_timeout(time_t sec) {
  4462. keep_alive_timeout_sec_ = sec;
  4463. return *this;
  4464. }
  4465. Server &Server::set_read_timeout(time_t sec, time_t usec) {
  4466. read_timeout_sec_ = sec;
  4467. read_timeout_usec_ = usec;
  4468. return *this;
  4469. }
  4470. Server &Server::set_write_timeout(time_t sec, time_t usec) {
  4471. write_timeout_sec_ = sec;
  4472. write_timeout_usec_ = usec;
  4473. return *this;
  4474. }
  4475. Server &Server::set_idle_interval(time_t sec, time_t usec) {
  4476. idle_interval_sec_ = sec;
  4477. idle_interval_usec_ = usec;
  4478. return *this;
  4479. }
  4480. Server &Server::set_payload_max_length(size_t length) {
  4481. payload_max_length_ = length;
  4482. return *this;
  4483. }
  4484. bool Server::bind_to_port(const std::string &host, int port,
  4485. int socket_flags) {
  4486. auto ret = bind_internal(host, port, socket_flags);
  4487. if (ret == -1) { is_decommissioned = true; }
  4488. return ret >= 0;
  4489. }
  4490. int Server::bind_to_any_port(const std::string &host, int socket_flags) {
  4491. auto ret = bind_internal(host, 0, socket_flags);
  4492. if (ret == -1) { is_decommissioned = true; }
  4493. return ret;
  4494. }
  4495. bool Server::listen_after_bind() { return listen_internal(); }
  4496. bool Server::listen(const std::string &host, int port,
  4497. int socket_flags) {
  4498. return bind_to_port(host, port, socket_flags) && listen_internal();
  4499. }
  4500. bool Server::is_running() const { return is_running_; }
  4501. void Server::wait_until_ready() const {
  4502. while (!is_running_ && !is_decommissioned) {
  4503. std::this_thread::sleep_for(std::chrono::milliseconds{1});
  4504. }
  4505. }
  4506. void Server::stop() {
  4507. if (is_running_) {
  4508. assert(svr_sock_ != INVALID_SOCKET);
  4509. std::atomic<socket_t> sock(svr_sock_.exchange(INVALID_SOCKET));
  4510. detail::shutdown_socket(sock);
  4511. detail::close_socket(sock);
  4512. }
  4513. is_decommissioned = false;
  4514. }
  4515. void Server::decommission() { is_decommissioned = true; }
  4516. bool Server::parse_request_line(const char *s, Request &req) const {
  4517. auto len = strlen(s);
  4518. if (len < 2 || s[len - 2] != '\r' || s[len - 1] != '\n') { return false; }
  4519. len -= 2;
  4520. {
  4521. size_t count = 0;
  4522. detail::split(s, s + len, ' ', [&](const char *b, const char *e) {
  4523. switch (count) {
  4524. case 0: req.method = std::string(b, e); break;
  4525. case 1: req.target = std::string(b, e); break;
  4526. case 2: req.version = std::string(b, e); break;
  4527. default: break;
  4528. }
  4529. count++;
  4530. });
  4531. if (count != 3) { return false; }
  4532. }
  4533. thread_local const std::set<std::string> methods{
  4534. "GET", "HEAD", "POST", "PUT", "DELETE",
  4535. "CONNECT", "OPTIONS", "TRACE", "PATCH", "PRI"};
  4536. if (methods.find(req.method) == methods.end()) {
  4537. output_error_log(Error::InvalidHTTPMethod, &req);
  4538. return false;
  4539. }
  4540. if (req.version != "HTTP/1.1" && req.version != "HTTP/1.0") {
  4541. output_error_log(Error::InvalidHTTPVersion, &req);
  4542. return false;
  4543. }
  4544. {
  4545. // Skip URL fragment
  4546. for (size_t i = 0; i < req.target.size(); i++) {
  4547. if (req.target[i] == '#') {
  4548. req.target.erase(i);
  4549. break;
  4550. }
  4551. }
  4552. detail::divide(req.target, '?',
  4553. [&](const char *lhs_data, std::size_t lhs_size,
  4554. const char *rhs_data, std::size_t rhs_size) {
  4555. req.path =
  4556. decode_path_component(std::string(lhs_data, lhs_size));
  4557. detail::parse_query_text(rhs_data, rhs_size, req.params);
  4558. });
  4559. }
  4560. return true;
  4561. }
  4562. bool Server::write_response(Stream &strm, bool close_connection,
  4563. Request &req, Response &res) {
  4564. // NOTE: `req.ranges` should be empty, otherwise it will be applied
  4565. // incorrectly to the error content.
  4566. req.ranges.clear();
  4567. return write_response_core(strm, close_connection, req, res, false);
  4568. }
  4569. bool Server::write_response_with_content(Stream &strm,
  4570. bool close_connection,
  4571. const Request &req,
  4572. Response &res) {
  4573. return write_response_core(strm, close_connection, req, res, true);
  4574. }
  4575. bool Server::write_response_core(Stream &strm, bool close_connection,
  4576. const Request &req, Response &res,
  4577. bool need_apply_ranges) {
  4578. assert(res.status != -1);
  4579. if (400 <= res.status && error_handler_ &&
  4580. error_handler_(req, res) == HandlerResponse::Handled) {
  4581. need_apply_ranges = true;
  4582. }
  4583. std::string content_type;
  4584. std::string boundary;
  4585. if (need_apply_ranges) { apply_ranges(req, res, content_type, boundary); }
  4586. // Prepare additional headers
  4587. if (close_connection || req.get_header_value("Connection") == "close" ||
  4588. 400 <= res.status) { // Don't leave connections open after errors
  4589. res.set_header("Connection", "close");
  4590. } else {
  4591. std::string s = "timeout=";
  4592. s += std::to_string(keep_alive_timeout_sec_);
  4593. s += ", max=";
  4594. s += std::to_string(keep_alive_max_count_);
  4595. res.set_header("Keep-Alive", s);
  4596. }
  4597. if ((!res.body.empty() || res.content_length_ > 0 || res.content_provider_) &&
  4598. !res.has_header("Content-Type")) {
  4599. res.set_header("Content-Type", "text/plain");
  4600. }
  4601. if (res.body.empty() && !res.content_length_ && !res.content_provider_ &&
  4602. !res.has_header("Content-Length")) {
  4603. res.set_header("Content-Length", "0");
  4604. }
  4605. if (req.method == "HEAD" && !res.has_header("Accept-Ranges")) {
  4606. res.set_header("Accept-Ranges", "bytes");
  4607. }
  4608. if (post_routing_handler_) { post_routing_handler_(req, res); }
  4609. // Response line and headers
  4610. {
  4611. detail::BufferStream bstrm;
  4612. if (!detail::write_response_line(bstrm, res.status)) { return false; }
  4613. if (header_writer_(bstrm, res.headers) <= 0) { return false; }
  4614. // Flush buffer
  4615. auto &data = bstrm.get_buffer();
  4616. detail::write_data(strm, data.data(), data.size());
  4617. }
  4618. // Body
  4619. auto ret = true;
  4620. if (req.method != "HEAD") {
  4621. if (!res.body.empty()) {
  4622. if (!detail::write_data(strm, res.body.data(), res.body.size())) {
  4623. ret = false;
  4624. }
  4625. } else if (res.content_provider_) {
  4626. if (write_content_with_provider(strm, req, res, boundary, content_type)) {
  4627. res.content_provider_success_ = true;
  4628. } else {
  4629. ret = false;
  4630. }
  4631. }
  4632. }
  4633. // Log
  4634. output_log(req, res);
  4635. return ret;
  4636. }
  4637. bool
  4638. Server::write_content_with_provider(Stream &strm, const Request &req,
  4639. Response &res, const std::string &boundary,
  4640. const std::string &content_type) {
  4641. auto is_shutting_down = [this]() {
  4642. return this->svr_sock_ == INVALID_SOCKET;
  4643. };
  4644. if (res.content_length_ > 0) {
  4645. if (req.ranges.empty()) {
  4646. return detail::write_content(strm, res.content_provider_, 0,
  4647. res.content_length_, is_shutting_down);
  4648. } else if (req.ranges.size() == 1) {
  4649. auto offset_and_length = detail::get_range_offset_and_length(
  4650. req.ranges[0], res.content_length_);
  4651. return detail::write_content(strm, res.content_provider_,
  4652. offset_and_length.first,
  4653. offset_and_length.second, is_shutting_down);
  4654. } else {
  4655. return detail::write_multipart_ranges_data(
  4656. strm, req, res, boundary, content_type, res.content_length_,
  4657. is_shutting_down);
  4658. }
  4659. } else {
  4660. if (res.is_chunked_content_provider_) {
  4661. auto type = detail::encoding_type(req, res);
  4662. std::unique_ptr<detail::compressor> compressor;
  4663. if (type == detail::EncodingType::Gzip) {
  4664. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  4665. compressor = detail::make_unique<detail::gzip_compressor>();
  4666. #endif
  4667. } else if (type == detail::EncodingType::Brotli) {
  4668. #ifdef CPPHTTPLIB_BROTLI_SUPPORT
  4669. compressor = detail::make_unique<detail::brotli_compressor>();
  4670. #endif
  4671. } else if (type == detail::EncodingType::Zstd) {
  4672. #ifdef CPPHTTPLIB_ZSTD_SUPPORT
  4673. compressor = detail::make_unique<detail::zstd_compressor>();
  4674. #endif
  4675. } else {
  4676. compressor = detail::make_unique<detail::nocompressor>();
  4677. }
  4678. assert(compressor != nullptr);
  4679. return detail::write_content_chunked(strm, res.content_provider_,
  4680. is_shutting_down, *compressor);
  4681. } else {
  4682. return detail::write_content_without_length(strm, res.content_provider_,
  4683. is_shutting_down);
  4684. }
  4685. }
  4686. }
  4687. bool Server::read_content(Stream &strm, Request &req, Response &res) {
  4688. FormFields::iterator cur_field;
  4689. FormFiles::iterator cur_file;
  4690. auto is_text_field = false;
  4691. size_t count = 0;
  4692. if (read_content_core(
  4693. strm, req, res,
  4694. // Regular
  4695. [&](const char *buf, size_t n) {
  4696. // Prevent arithmetic overflow when checking sizes.
  4697. // Avoid computing (req.body.size() + n) directly because
  4698. // adding two unsigned `size_t` values can wrap around and
  4699. // produce a small result instead of indicating overflow.
  4700. // Instead, check using subtraction: ensure `n` does not
  4701. // exceed the remaining capacity `max_size() - size()`.
  4702. if (req.body.size() >= req.body.max_size() ||
  4703. n > req.body.max_size() - req.body.size()) {
  4704. return false;
  4705. }
  4706. req.body.append(buf, n);
  4707. return true;
  4708. },
  4709. // Multipart FormData
  4710. [&](const FormData &file) {
  4711. if (count++ == CPPHTTPLIB_MULTIPART_FORM_DATA_FILE_MAX_COUNT) {
  4712. output_error_log(Error::TooManyFormDataFiles, &req);
  4713. return false;
  4714. }
  4715. if (file.filename.empty()) {
  4716. cur_field = req.form.fields.emplace(
  4717. file.name, FormField{file.name, file.content, file.headers});
  4718. is_text_field = true;
  4719. } else {
  4720. cur_file = req.form.files.emplace(file.name, file);
  4721. is_text_field = false;
  4722. }
  4723. return true;
  4724. },
  4725. [&](const char *buf, size_t n) {
  4726. if (is_text_field) {
  4727. auto &content = cur_field->second.content;
  4728. if (content.size() + n > content.max_size()) { return false; }
  4729. content.append(buf, n);
  4730. } else {
  4731. auto &content = cur_file->second.content;
  4732. if (content.size() + n > content.max_size()) { return false; }
  4733. content.append(buf, n);
  4734. }
  4735. return true;
  4736. })) {
  4737. const auto &content_type = req.get_header_value("Content-Type");
  4738. if (!content_type.find("application/x-www-form-urlencoded")) {
  4739. if (req.body.size() > CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH) {
  4740. res.status = StatusCode::PayloadTooLarge_413; // NOTE: should be 414?
  4741. output_error_log(Error::ExceedMaxPayloadSize, &req);
  4742. return false;
  4743. }
  4744. detail::parse_query_text(req.body, req.params);
  4745. }
  4746. return true;
  4747. }
  4748. return false;
  4749. }
  4750. bool Server::read_content_with_content_receiver(
  4751. Stream &strm, Request &req, Response &res, ContentReceiver receiver,
  4752. FormDataHeader multipart_header, ContentReceiver multipart_receiver) {
  4753. return read_content_core(strm, req, res, std::move(receiver),
  4754. std::move(multipart_header),
  4755. std::move(multipart_receiver));
  4756. }
  4757. bool Server::read_content_core(
  4758. Stream &strm, Request &req, Response &res, ContentReceiver receiver,
  4759. FormDataHeader multipart_header, ContentReceiver multipart_receiver) const {
  4760. detail::FormDataParser multipart_form_data_parser;
  4761. ContentReceiverWithProgress out;
  4762. if (req.is_multipart_form_data()) {
  4763. const auto &content_type = req.get_header_value("Content-Type");
  4764. std::string boundary;
  4765. if (!detail::parse_multipart_boundary(content_type, boundary)) {
  4766. res.status = StatusCode::BadRequest_400;
  4767. output_error_log(Error::MultipartParsing, &req);
  4768. return false;
  4769. }
  4770. multipart_form_data_parser.set_boundary(std::move(boundary));
  4771. out = [&](const char *buf, size_t n, size_t /*off*/, size_t /*len*/) {
  4772. return multipart_form_data_parser.parse(buf, n, multipart_header,
  4773. multipart_receiver);
  4774. };
  4775. } else {
  4776. out = [receiver](const char *buf, size_t n, size_t /*off*/,
  4777. size_t /*len*/) { return receiver(buf, n); };
  4778. }
  4779. // RFC 7230 Section 3.3.3: If this is a request message and none of the above
  4780. // are true (no Transfer-Encoding and no Content-Length), then the message
  4781. // body length is zero (no message body is present).
  4782. //
  4783. // For non-SSL builds, peek into the socket to detect clients that send a
  4784. // body without a Content-Length header (raw HTTP over TCP). If there is
  4785. // pending data that exceeds the configured payload limit, treat this as an
  4786. // oversized request and fail early (causing connection close). For SSL
  4787. // builds we cannot reliably peek the decrypted application bytes, so keep
  4788. // the original behaviour.
  4789. #if !defined(CPPHTTPLIB_OPENSSL_SUPPORT) && !defined(_WIN32)
  4790. if (!req.has_header("Content-Length") &&
  4791. !detail::is_chunked_transfer_encoding(req.headers)) {
  4792. socket_t s = strm.socket();
  4793. if (s != INVALID_SOCKET) {
  4794. // Peek up to payload_max_length_ + 1 bytes. If more than
  4795. // payload_max_length_ bytes are pending, reject the request.
  4796. size_t to_peek =
  4797. (payload_max_length_ > 0)
  4798. ? (std::min)(payload_max_length_ + 1, static_cast<size_t>(4096))
  4799. : 1;
  4800. std::vector<char> peekbuf(to_peek);
  4801. ssize_t n = ::recv(s, peekbuf.data(), to_peek, MSG_PEEK);
  4802. if (n > 0 && static_cast<size_t>(n) > payload_max_length_) {
  4803. // Indicate failure so connection will be closed.
  4804. return false;
  4805. }
  4806. }
  4807. return true;
  4808. }
  4809. #else
  4810. if (!req.has_header("Content-Length") &&
  4811. !detail::is_chunked_transfer_encoding(req.headers)) {
  4812. return true;
  4813. }
  4814. #endif
  4815. if (!detail::read_content(strm, req, payload_max_length_, res.status, nullptr,
  4816. out, true)) {
  4817. return false;
  4818. }
  4819. if (req.is_multipart_form_data()) {
  4820. if (!multipart_form_data_parser.is_valid()) {
  4821. res.status = StatusCode::BadRequest_400;
  4822. output_error_log(Error::MultipartParsing, &req);
  4823. return false;
  4824. }
  4825. }
  4826. return true;
  4827. }
  4828. bool Server::handle_file_request(Request &req, Response &res) {
  4829. for (const auto &entry : base_dirs_) {
  4830. // Prefix match
  4831. if (!req.path.compare(0, entry.mount_point.size(), entry.mount_point)) {
  4832. std::string sub_path = "/" + req.path.substr(entry.mount_point.size());
  4833. if (detail::is_valid_path(sub_path)) {
  4834. auto path = entry.base_dir + sub_path;
  4835. if (path.back() == '/') { path += "index.html"; }
  4836. detail::FileStat stat(path);
  4837. if (stat.is_dir()) {
  4838. res.set_redirect(sub_path + "/", StatusCode::MovedPermanently_301);
  4839. return true;
  4840. }
  4841. if (stat.is_file()) {
  4842. for (const auto &kv : entry.headers) {
  4843. res.set_header(kv.first, kv.second);
  4844. }
  4845. auto etag = detail::compute_etag(stat);
  4846. if (!etag.empty()) { res.set_header("ETag", etag); }
  4847. auto mtime = stat.mtime();
  4848. auto last_modified = detail::file_mtime_to_http_date(mtime);
  4849. if (!last_modified.empty()) {
  4850. res.set_header("Last-Modified", last_modified);
  4851. }
  4852. if (check_if_not_modified(req, res, etag, mtime)) { return true; }
  4853. check_if_range(req, etag, mtime);
  4854. auto mm = std::make_shared<detail::mmap>(path.c_str());
  4855. if (!mm->is_open()) {
  4856. output_error_log(Error::OpenFile, &req);
  4857. return false;
  4858. }
  4859. res.set_content_provider(
  4860. mm->size(),
  4861. detail::find_content_type(path, file_extension_and_mimetype_map_,
  4862. default_file_mimetype_),
  4863. [mm](size_t offset, size_t length, DataSink &sink) -> bool {
  4864. sink.write(mm->data() + offset, length);
  4865. return true;
  4866. });
  4867. if (req.method != "HEAD" && file_request_handler_) {
  4868. file_request_handler_(req, res);
  4869. }
  4870. return true;
  4871. } else {
  4872. output_error_log(Error::OpenFile, &req);
  4873. }
  4874. }
  4875. }
  4876. }
  4877. return false;
  4878. }
  4879. bool Server::check_if_not_modified(const Request &req, Response &res,
  4880. const std::string &etag,
  4881. time_t mtime) const {
  4882. // Handle conditional GET:
  4883. // 1. If-None-Match takes precedence (RFC 9110 Section 13.1.2)
  4884. // 2. If-Modified-Since is checked only when If-None-Match is absent
  4885. if (req.has_header("If-None-Match")) {
  4886. if (!etag.empty()) {
  4887. auto val = req.get_header_value("If-None-Match");
  4888. // NOTE: We use exact string matching here. This works correctly
  4889. // because our server always generates weak ETags (W/"..."), and
  4890. // clients typically send back the same ETag they received.
  4891. // RFC 9110 Section 8.8.3.2 allows weak comparison for
  4892. // If-None-Match, where W/"x" and "x" would match, but this
  4893. // simplified implementation requires exact matches.
  4894. auto ret = detail::split_find(val.data(), val.data() + val.size(), ',',
  4895. [&](const char *b, const char *e) {
  4896. return std::equal(b, e, "*") ||
  4897. std::equal(b, e, etag.begin());
  4898. });
  4899. if (ret) {
  4900. res.status = StatusCode::NotModified_304;
  4901. return true;
  4902. }
  4903. }
  4904. } else if (req.has_header("If-Modified-Since")) {
  4905. auto val = req.get_header_value("If-Modified-Since");
  4906. auto t = detail::parse_http_date(val);
  4907. if (t != static_cast<time_t>(-1) && mtime <= t) {
  4908. res.status = StatusCode::NotModified_304;
  4909. return true;
  4910. }
  4911. }
  4912. return false;
  4913. }
  4914. bool Server::check_if_range(Request &req, const std::string &etag,
  4915. time_t mtime) const {
  4916. // Handle If-Range for partial content requests (RFC 9110
  4917. // Section 13.1.5). If-Range is only evaluated when Range header is
  4918. // present. If the validator matches, serve partial content; otherwise
  4919. // serve full content.
  4920. if (!req.ranges.empty() && req.has_header("If-Range")) {
  4921. auto val = req.get_header_value("If-Range");
  4922. auto is_valid_range = [&]() {
  4923. if (detail::is_strong_etag(val)) {
  4924. // RFC 9110 Section 13.1.5: If-Range requires strong ETag
  4925. // comparison.
  4926. return (!etag.empty() && val == etag);
  4927. } else if (detail::is_weak_etag(val)) {
  4928. // Weak ETags are not valid for If-Range (RFC 9110 Section 13.1.5)
  4929. return false;
  4930. } else {
  4931. // HTTP-date comparison
  4932. auto t = detail::parse_http_date(val);
  4933. return (t != static_cast<time_t>(-1) && mtime <= t);
  4934. }
  4935. };
  4936. if (!is_valid_range()) {
  4937. // Validator doesn't match: ignore Range and serve full content
  4938. req.ranges.clear();
  4939. return false;
  4940. }
  4941. }
  4942. return true;
  4943. }
  4944. socket_t
  4945. Server::create_server_socket(const std::string &host, int port,
  4946. int socket_flags,
  4947. SocketOptions socket_options) const {
  4948. return detail::create_socket(
  4949. host, std::string(), port, address_family_, socket_flags, tcp_nodelay_,
  4950. ipv6_v6only_, std::move(socket_options),
  4951. [&](socket_t sock, struct addrinfo &ai, bool & /*quit*/) -> bool {
  4952. if (::bind(sock, ai.ai_addr, static_cast<socklen_t>(ai.ai_addrlen))) {
  4953. output_error_log(Error::BindIPAddress, nullptr);
  4954. return false;
  4955. }
  4956. if (::listen(sock, CPPHTTPLIB_LISTEN_BACKLOG)) {
  4957. output_error_log(Error::Listen, nullptr);
  4958. return false;
  4959. }
  4960. return true;
  4961. });
  4962. }
  4963. int Server::bind_internal(const std::string &host, int port,
  4964. int socket_flags) {
  4965. if (is_decommissioned) { return -1; }
  4966. if (!is_valid()) { return -1; }
  4967. svr_sock_ = create_server_socket(host, port, socket_flags, socket_options_);
  4968. if (svr_sock_ == INVALID_SOCKET) { return -1; }
  4969. if (port == 0) {
  4970. struct sockaddr_storage addr;
  4971. socklen_t addr_len = sizeof(addr);
  4972. if (getsockname(svr_sock_, reinterpret_cast<struct sockaddr *>(&addr),
  4973. &addr_len) == -1) {
  4974. output_error_log(Error::GetSockName, nullptr);
  4975. return -1;
  4976. }
  4977. if (addr.ss_family == AF_INET) {
  4978. return ntohs(reinterpret_cast<struct sockaddr_in *>(&addr)->sin_port);
  4979. } else if (addr.ss_family == AF_INET6) {
  4980. return ntohs(reinterpret_cast<struct sockaddr_in6 *>(&addr)->sin6_port);
  4981. } else {
  4982. output_error_log(Error::UnsupportedAddressFamily, nullptr);
  4983. return -1;
  4984. }
  4985. } else {
  4986. return port;
  4987. }
  4988. }
  4989. bool Server::listen_internal() {
  4990. if (is_decommissioned) { return false; }
  4991. auto ret = true;
  4992. is_running_ = true;
  4993. auto se = detail::scope_exit([&]() { is_running_ = false; });
  4994. {
  4995. std::unique_ptr<TaskQueue> task_queue(new_task_queue());
  4996. while (svr_sock_ != INVALID_SOCKET) {
  4997. #ifndef _WIN32
  4998. if (idle_interval_sec_ > 0 || idle_interval_usec_ > 0) {
  4999. #endif
  5000. auto val = detail::select_read(svr_sock_, idle_interval_sec_,
  5001. idle_interval_usec_);
  5002. if (val == 0) { // Timeout
  5003. task_queue->on_idle();
  5004. continue;
  5005. }
  5006. #ifndef _WIN32
  5007. }
  5008. #endif
  5009. #if defined _WIN32
  5010. // sockets connected via WASAccept inherit flags NO_HANDLE_INHERIT,
  5011. // OVERLAPPED
  5012. socket_t sock = WSAAccept(svr_sock_, nullptr, nullptr, nullptr, 0);
  5013. #elif defined SOCK_CLOEXEC
  5014. socket_t sock = accept4(svr_sock_, nullptr, nullptr, SOCK_CLOEXEC);
  5015. #else
  5016. socket_t sock = accept(svr_sock_, nullptr, nullptr);
  5017. #endif
  5018. if (sock == INVALID_SOCKET) {
  5019. if (errno == EMFILE) {
  5020. // The per-process limit of open file descriptors has been reached.
  5021. // Try to accept new connections after a short sleep.
  5022. std::this_thread::sleep_for(std::chrono::microseconds{1});
  5023. continue;
  5024. } else if (errno == EINTR || errno == EAGAIN) {
  5025. continue;
  5026. }
  5027. if (svr_sock_ != INVALID_SOCKET) {
  5028. detail::close_socket(svr_sock_);
  5029. ret = false;
  5030. output_error_log(Error::Connection, nullptr);
  5031. } else {
  5032. ; // The server socket was closed by user.
  5033. }
  5034. break;
  5035. }
  5036. detail::set_socket_opt_time(sock, SOL_SOCKET, SO_RCVTIMEO,
  5037. read_timeout_sec_, read_timeout_usec_);
  5038. detail::set_socket_opt_time(sock, SOL_SOCKET, SO_SNDTIMEO,
  5039. write_timeout_sec_, write_timeout_usec_);
  5040. if (!task_queue->enqueue(
  5041. [this, sock]() { process_and_close_socket(sock); })) {
  5042. output_error_log(Error::ResourceExhaustion, nullptr);
  5043. detail::shutdown_socket(sock);
  5044. detail::close_socket(sock);
  5045. }
  5046. }
  5047. task_queue->shutdown();
  5048. }
  5049. is_decommissioned = !ret;
  5050. return ret;
  5051. }
  5052. bool Server::routing(Request &req, Response &res, Stream &strm) {
  5053. if (pre_routing_handler_ &&
  5054. pre_routing_handler_(req, res) == HandlerResponse::Handled) {
  5055. return true;
  5056. }
  5057. // File handler
  5058. if ((req.method == "GET" || req.method == "HEAD") &&
  5059. handle_file_request(req, res)) {
  5060. return true;
  5061. }
  5062. if (detail::expect_content(req)) {
  5063. // Content reader handler
  5064. {
  5065. ContentReader reader(
  5066. [&](ContentReceiver receiver) {
  5067. auto result = read_content_with_content_receiver(
  5068. strm, req, res, std::move(receiver), nullptr, nullptr);
  5069. if (!result) { output_error_log(Error::Read, &req); }
  5070. return result;
  5071. },
  5072. [&](FormDataHeader header, ContentReceiver receiver) {
  5073. auto result = read_content_with_content_receiver(
  5074. strm, req, res, nullptr, std::move(header),
  5075. std::move(receiver));
  5076. if (!result) { output_error_log(Error::Read, &req); }
  5077. return result;
  5078. });
  5079. if (req.method == "POST") {
  5080. if (dispatch_request_for_content_reader(
  5081. req, res, std::move(reader),
  5082. post_handlers_for_content_reader_)) {
  5083. return true;
  5084. }
  5085. } else if (req.method == "PUT") {
  5086. if (dispatch_request_for_content_reader(
  5087. req, res, std::move(reader),
  5088. put_handlers_for_content_reader_)) {
  5089. return true;
  5090. }
  5091. } else if (req.method == "PATCH") {
  5092. if (dispatch_request_for_content_reader(
  5093. req, res, std::move(reader),
  5094. patch_handlers_for_content_reader_)) {
  5095. return true;
  5096. }
  5097. } else if (req.method == "DELETE") {
  5098. if (dispatch_request_for_content_reader(
  5099. req, res, std::move(reader),
  5100. delete_handlers_for_content_reader_)) {
  5101. return true;
  5102. }
  5103. }
  5104. }
  5105. // Read content into `req.body`
  5106. if (!read_content(strm, req, res)) {
  5107. output_error_log(Error::Read, &req);
  5108. return false;
  5109. }
  5110. }
  5111. // Regular handler
  5112. if (req.method == "GET" || req.method == "HEAD") {
  5113. return dispatch_request(req, res, get_handlers_);
  5114. } else if (req.method == "POST") {
  5115. return dispatch_request(req, res, post_handlers_);
  5116. } else if (req.method == "PUT") {
  5117. return dispatch_request(req, res, put_handlers_);
  5118. } else if (req.method == "DELETE") {
  5119. return dispatch_request(req, res, delete_handlers_);
  5120. } else if (req.method == "OPTIONS") {
  5121. return dispatch_request(req, res, options_handlers_);
  5122. } else if (req.method == "PATCH") {
  5123. return dispatch_request(req, res, patch_handlers_);
  5124. }
  5125. res.status = StatusCode::BadRequest_400;
  5126. return false;
  5127. }
  5128. bool Server::dispatch_request(Request &req, Response &res,
  5129. const Handlers &handlers) const {
  5130. for (const auto &x : handlers) {
  5131. const auto &matcher = x.first;
  5132. const auto &handler = x.second;
  5133. if (matcher->match(req)) {
  5134. req.matched_route = matcher->pattern();
  5135. if (!pre_request_handler_ ||
  5136. pre_request_handler_(req, res) != HandlerResponse::Handled) {
  5137. handler(req, res);
  5138. }
  5139. return true;
  5140. }
  5141. }
  5142. return false;
  5143. }
  5144. void Server::apply_ranges(const Request &req, Response &res,
  5145. std::string &content_type,
  5146. std::string &boundary) const {
  5147. if (req.ranges.size() > 1 && res.status == StatusCode::PartialContent_206) {
  5148. auto it = res.headers.find("Content-Type");
  5149. if (it != res.headers.end()) {
  5150. content_type = it->second;
  5151. res.headers.erase(it);
  5152. }
  5153. boundary = detail::make_multipart_data_boundary();
  5154. res.set_header("Content-Type",
  5155. "multipart/byteranges; boundary=" + boundary);
  5156. }
  5157. auto type = detail::encoding_type(req, res);
  5158. if (res.body.empty()) {
  5159. if (res.content_length_ > 0) {
  5160. size_t length = 0;
  5161. if (req.ranges.empty() || res.status != StatusCode::PartialContent_206) {
  5162. length = res.content_length_;
  5163. } else if (req.ranges.size() == 1) {
  5164. auto offset_and_length = detail::get_range_offset_and_length(
  5165. req.ranges[0], res.content_length_);
  5166. length = offset_and_length.second;
  5167. auto content_range = detail::make_content_range_header_field(
  5168. offset_and_length, res.content_length_);
  5169. res.set_header("Content-Range", content_range);
  5170. } else {
  5171. length = detail::get_multipart_ranges_data_length(
  5172. req, boundary, content_type, res.content_length_);
  5173. }
  5174. res.set_header("Content-Length", std::to_string(length));
  5175. } else {
  5176. if (res.content_provider_) {
  5177. if (res.is_chunked_content_provider_) {
  5178. res.set_header("Transfer-Encoding", "chunked");
  5179. if (type == detail::EncodingType::Gzip) {
  5180. res.set_header("Content-Encoding", "gzip");
  5181. res.set_header("Vary", "Accept-Encoding");
  5182. } else if (type == detail::EncodingType::Brotli) {
  5183. res.set_header("Content-Encoding", "br");
  5184. res.set_header("Vary", "Accept-Encoding");
  5185. } else if (type == detail::EncodingType::Zstd) {
  5186. res.set_header("Content-Encoding", "zstd");
  5187. res.set_header("Vary", "Accept-Encoding");
  5188. }
  5189. }
  5190. }
  5191. }
  5192. } else {
  5193. if (req.ranges.empty() || res.status != StatusCode::PartialContent_206) {
  5194. ;
  5195. } else if (req.ranges.size() == 1) {
  5196. auto offset_and_length =
  5197. detail::get_range_offset_and_length(req.ranges[0], res.body.size());
  5198. auto offset = offset_and_length.first;
  5199. auto length = offset_and_length.second;
  5200. auto content_range = detail::make_content_range_header_field(
  5201. offset_and_length, res.body.size());
  5202. res.set_header("Content-Range", content_range);
  5203. assert(offset + length <= res.body.size());
  5204. res.body = res.body.substr(offset, length);
  5205. } else {
  5206. std::string data;
  5207. detail::make_multipart_ranges_data(req, res, boundary, content_type,
  5208. res.body.size(), data);
  5209. res.body.swap(data);
  5210. }
  5211. if (type != detail::EncodingType::None) {
  5212. output_pre_compression_log(req, res);
  5213. std::unique_ptr<detail::compressor> compressor;
  5214. std::string content_encoding;
  5215. if (type == detail::EncodingType::Gzip) {
  5216. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  5217. compressor = detail::make_unique<detail::gzip_compressor>();
  5218. content_encoding = "gzip";
  5219. #endif
  5220. } else if (type == detail::EncodingType::Brotli) {
  5221. #ifdef CPPHTTPLIB_BROTLI_SUPPORT
  5222. compressor = detail::make_unique<detail::brotli_compressor>();
  5223. content_encoding = "br";
  5224. #endif
  5225. } else if (type == detail::EncodingType::Zstd) {
  5226. #ifdef CPPHTTPLIB_ZSTD_SUPPORT
  5227. compressor = detail::make_unique<detail::zstd_compressor>();
  5228. content_encoding = "zstd";
  5229. #endif
  5230. }
  5231. if (compressor) {
  5232. std::string compressed;
  5233. if (compressor->compress(res.body.data(), res.body.size(), true,
  5234. [&](const char *data, size_t data_len) {
  5235. compressed.append(data, data_len);
  5236. return true;
  5237. })) {
  5238. res.body.swap(compressed);
  5239. res.set_header("Content-Encoding", content_encoding);
  5240. res.set_header("Vary", "Accept-Encoding");
  5241. }
  5242. }
  5243. }
  5244. auto length = std::to_string(res.body.size());
  5245. res.set_header("Content-Length", length);
  5246. }
  5247. }
  5248. bool Server::dispatch_request_for_content_reader(
  5249. Request &req, Response &res, ContentReader content_reader,
  5250. const HandlersForContentReader &handlers) const {
  5251. for (const auto &x : handlers) {
  5252. const auto &matcher = x.first;
  5253. const auto &handler = x.second;
  5254. if (matcher->match(req)) {
  5255. req.matched_route = matcher->pattern();
  5256. if (!pre_request_handler_ ||
  5257. pre_request_handler_(req, res) != HandlerResponse::Handled) {
  5258. handler(req, res, content_reader);
  5259. }
  5260. return true;
  5261. }
  5262. }
  5263. return false;
  5264. }
  5265. std::string
  5266. get_client_ip(const std::string &x_forwarded_for,
  5267. const std::vector<std::string> &trusted_proxies) {
  5268. // X-Forwarded-For is a comma-separated list per RFC 7239
  5269. std::vector<std::string> ip_list;
  5270. detail::split(x_forwarded_for.data(),
  5271. x_forwarded_for.data() + x_forwarded_for.size(), ',',
  5272. [&](const char *b, const char *e) {
  5273. auto r = detail::trim(b, e, 0, static_cast<size_t>(e - b));
  5274. ip_list.emplace_back(std::string(b + r.first, b + r.second));
  5275. });
  5276. for (size_t i = 0; i < ip_list.size(); ++i) {
  5277. auto ip = ip_list[i];
  5278. auto is_trusted_proxy =
  5279. std::any_of(trusted_proxies.begin(), trusted_proxies.end(),
  5280. [&](const std::string &proxy) { return ip == proxy; });
  5281. if (is_trusted_proxy) {
  5282. if (i == 0) {
  5283. // If the trusted proxy is the first IP, there's no preceding client IP
  5284. return ip;
  5285. } else {
  5286. // Return the IP immediately before the trusted proxy
  5287. return ip_list[i - 1];
  5288. }
  5289. }
  5290. }
  5291. // If no trusted proxy is found, return the first IP in the list
  5292. return ip_list.front();
  5293. }
  5294. bool
  5295. Server::process_request(Stream &strm, const std::string &remote_addr,
  5296. int remote_port, const std::string &local_addr,
  5297. int local_port, bool close_connection,
  5298. bool &connection_closed,
  5299. const std::function<void(Request &)> &setup_request) {
  5300. std::array<char, 2048> buf{};
  5301. detail::stream_line_reader line_reader(strm, buf.data(), buf.size());
  5302. // Connection has been closed on client
  5303. if (!line_reader.getline()) { return false; }
  5304. Request req;
  5305. req.start_time_ = std::chrono::steady_clock::now();
  5306. req.remote_addr = remote_addr;
  5307. req.remote_port = remote_port;
  5308. req.local_addr = local_addr;
  5309. req.local_port = local_port;
  5310. Response res;
  5311. res.version = "HTTP/1.1";
  5312. res.headers = default_headers_;
  5313. #ifdef __APPLE__
  5314. // Socket file descriptor exceeded FD_SETSIZE...
  5315. if (strm.socket() >= FD_SETSIZE) {
  5316. Headers dummy;
  5317. detail::read_headers(strm, dummy);
  5318. res.status = StatusCode::InternalServerError_500;
  5319. output_error_log(Error::ExceedMaxSocketDescriptorCount, &req);
  5320. return write_response(strm, close_connection, req, res);
  5321. }
  5322. #endif
  5323. // Request line and headers
  5324. if (!parse_request_line(line_reader.ptr(), req)) {
  5325. res.status = StatusCode::BadRequest_400;
  5326. output_error_log(Error::InvalidRequestLine, &req);
  5327. return write_response(strm, close_connection, req, res);
  5328. }
  5329. // Request headers
  5330. if (!detail::read_headers(strm, req.headers)) {
  5331. res.status = StatusCode::BadRequest_400;
  5332. output_error_log(Error::InvalidHeaders, &req);
  5333. return write_response(strm, close_connection, req, res);
  5334. }
  5335. // Check if the request URI doesn't exceed the limit
  5336. if (req.target.size() > CPPHTTPLIB_REQUEST_URI_MAX_LENGTH) {
  5337. res.status = StatusCode::UriTooLong_414;
  5338. output_error_log(Error::ExceedUriMaxLength, &req);
  5339. return write_response(strm, close_connection, req, res);
  5340. }
  5341. if (req.get_header_value("Connection") == "close") {
  5342. connection_closed = true;
  5343. }
  5344. if (req.version == "HTTP/1.0" &&
  5345. req.get_header_value("Connection") != "Keep-Alive") {
  5346. connection_closed = true;
  5347. }
  5348. if (!trusted_proxies_.empty() && req.has_header("X-Forwarded-For")) {
  5349. auto x_forwarded_for = req.get_header_value("X-Forwarded-For");
  5350. req.remote_addr = get_client_ip(x_forwarded_for, trusted_proxies_);
  5351. } else {
  5352. req.remote_addr = remote_addr;
  5353. }
  5354. req.remote_port = remote_port;
  5355. req.local_addr = local_addr;
  5356. req.local_port = local_port;
  5357. if (req.has_header("Accept")) {
  5358. const auto &accept_header = req.get_header_value("Accept");
  5359. if (!detail::parse_accept_header(accept_header, req.accept_content_types)) {
  5360. res.status = StatusCode::BadRequest_400;
  5361. output_error_log(Error::HTTPParsing, &req);
  5362. return write_response(strm, close_connection, req, res);
  5363. }
  5364. }
  5365. if (req.has_header("Range")) {
  5366. const auto &range_header_value = req.get_header_value("Range");
  5367. if (!detail::parse_range_header(range_header_value, req.ranges)) {
  5368. res.status = StatusCode::RangeNotSatisfiable_416;
  5369. output_error_log(Error::InvalidRangeHeader, &req);
  5370. return write_response(strm, close_connection, req, res);
  5371. }
  5372. }
  5373. if (setup_request) { setup_request(req); }
  5374. if (req.get_header_value("Expect") == "100-continue") {
  5375. int status = StatusCode::Continue_100;
  5376. if (expect_100_continue_handler_) {
  5377. status = expect_100_continue_handler_(req, res);
  5378. }
  5379. switch (status) {
  5380. case StatusCode::Continue_100:
  5381. case StatusCode::ExpectationFailed_417:
  5382. detail::write_response_line(strm, status);
  5383. strm.write("\r\n");
  5384. break;
  5385. default:
  5386. connection_closed = true;
  5387. return write_response(strm, true, req, res);
  5388. }
  5389. }
  5390. // Setup `is_connection_closed` method
  5391. auto sock = strm.socket();
  5392. req.is_connection_closed = [sock]() {
  5393. return !detail::is_socket_alive(sock);
  5394. };
  5395. // Routing
  5396. auto routed = false;
  5397. #ifdef CPPHTTPLIB_NO_EXCEPTIONS
  5398. routed = routing(req, res, strm);
  5399. #else
  5400. try {
  5401. routed = routing(req, res, strm);
  5402. } catch (std::exception &e) {
  5403. if (exception_handler_) {
  5404. auto ep = std::current_exception();
  5405. exception_handler_(req, res, ep);
  5406. routed = true;
  5407. } else {
  5408. res.status = StatusCode::InternalServerError_500;
  5409. std::string val;
  5410. auto s = e.what();
  5411. for (size_t i = 0; s[i]; i++) {
  5412. switch (s[i]) {
  5413. case '\r': val += "\\r"; break;
  5414. case '\n': val += "\\n"; break;
  5415. default: val += s[i]; break;
  5416. }
  5417. }
  5418. res.set_header("EXCEPTION_WHAT", val);
  5419. }
  5420. } catch (...) {
  5421. if (exception_handler_) {
  5422. auto ep = std::current_exception();
  5423. exception_handler_(req, res, ep);
  5424. routed = true;
  5425. } else {
  5426. res.status = StatusCode::InternalServerError_500;
  5427. res.set_header("EXCEPTION_WHAT", "UNKNOWN");
  5428. }
  5429. }
  5430. #endif
  5431. if (routed) {
  5432. if (res.status == -1) {
  5433. res.status = req.ranges.empty() ? StatusCode::OK_200
  5434. : StatusCode::PartialContent_206;
  5435. }
  5436. // Serve file content by using a content provider
  5437. if (!res.file_content_path_.empty()) {
  5438. const auto &path = res.file_content_path_;
  5439. auto mm = std::make_shared<detail::mmap>(path.c_str());
  5440. if (!mm->is_open()) {
  5441. res.body.clear();
  5442. res.content_length_ = 0;
  5443. res.content_provider_ = nullptr;
  5444. res.status = StatusCode::NotFound_404;
  5445. output_error_log(Error::OpenFile, &req);
  5446. return write_response(strm, close_connection, req, res);
  5447. }
  5448. auto content_type = res.file_content_content_type_;
  5449. if (content_type.empty()) {
  5450. content_type = detail::find_content_type(
  5451. path, file_extension_and_mimetype_map_, default_file_mimetype_);
  5452. }
  5453. res.set_content_provider(
  5454. mm->size(), content_type,
  5455. [mm](size_t offset, size_t length, DataSink &sink) -> bool {
  5456. sink.write(mm->data() + offset, length);
  5457. return true;
  5458. });
  5459. }
  5460. if (detail::range_error(req, res)) {
  5461. res.body.clear();
  5462. res.content_length_ = 0;
  5463. res.content_provider_ = nullptr;
  5464. res.status = StatusCode::RangeNotSatisfiable_416;
  5465. return write_response(strm, close_connection, req, res);
  5466. }
  5467. return write_response_with_content(strm, close_connection, req, res);
  5468. } else {
  5469. if (res.status == -1) { res.status = StatusCode::NotFound_404; }
  5470. return write_response(strm, close_connection, req, res);
  5471. }
  5472. }
  5473. bool Server::is_valid() const { return true; }
  5474. bool Server::process_and_close_socket(socket_t sock) {
  5475. std::string remote_addr;
  5476. int remote_port = 0;
  5477. detail::get_remote_ip_and_port(sock, remote_addr, remote_port);
  5478. std::string local_addr;
  5479. int local_port = 0;
  5480. detail::get_local_ip_and_port(sock, local_addr, local_port);
  5481. auto ret = detail::process_server_socket(
  5482. svr_sock_, sock, keep_alive_max_count_, keep_alive_timeout_sec_,
  5483. read_timeout_sec_, read_timeout_usec_, write_timeout_sec_,
  5484. write_timeout_usec_,
  5485. [&](Stream &strm, bool close_connection, bool &connection_closed) {
  5486. return process_request(strm, remote_addr, remote_port, local_addr,
  5487. local_port, close_connection, connection_closed,
  5488. nullptr);
  5489. });
  5490. detail::shutdown_socket(sock);
  5491. detail::close_socket(sock);
  5492. return ret;
  5493. }
  5494. void Server::output_log(const Request &req, const Response &res) const {
  5495. if (logger_) {
  5496. std::lock_guard<std::mutex> guard(logger_mutex_);
  5497. logger_(req, res);
  5498. }
  5499. }
  5500. void Server::output_pre_compression_log(const Request &req,
  5501. const Response &res) const {
  5502. if (pre_compression_logger_) {
  5503. std::lock_guard<std::mutex> guard(logger_mutex_);
  5504. pre_compression_logger_(req, res);
  5505. }
  5506. }
  5507. void Server::output_error_log(const Error &err,
  5508. const Request *req) const {
  5509. if (error_logger_) {
  5510. std::lock_guard<std::mutex> guard(logger_mutex_);
  5511. error_logger_(err, req);
  5512. }
  5513. }
  5514. // HTTP client implementation
  5515. ClientImpl::ClientImpl(const std::string &host)
  5516. : ClientImpl(host, 80, std::string(), std::string()) {}
  5517. ClientImpl::ClientImpl(const std::string &host, int port)
  5518. : ClientImpl(host, port, std::string(), std::string()) {}
  5519. ClientImpl::ClientImpl(const std::string &host, int port,
  5520. const std::string &client_cert_path,
  5521. const std::string &client_key_path)
  5522. : host_(detail::escape_abstract_namespace_unix_domain(host)), port_(port),
  5523. client_cert_path_(client_cert_path), client_key_path_(client_key_path) {}
  5524. ClientImpl::~ClientImpl() {
  5525. // Wait until all the requests in flight are handled.
  5526. size_t retry_count = 10;
  5527. while (retry_count-- > 0) {
  5528. {
  5529. std::lock_guard<std::mutex> guard(socket_mutex_);
  5530. if (socket_requests_in_flight_ == 0) { break; }
  5531. }
  5532. std::this_thread::sleep_for(std::chrono::milliseconds{1});
  5533. }
  5534. std::lock_guard<std::mutex> guard(socket_mutex_);
  5535. shutdown_socket(socket_);
  5536. close_socket(socket_);
  5537. }
  5538. bool ClientImpl::is_valid() const { return true; }
  5539. void ClientImpl::copy_settings(const ClientImpl &rhs) {
  5540. client_cert_path_ = rhs.client_cert_path_;
  5541. client_key_path_ = rhs.client_key_path_;
  5542. connection_timeout_sec_ = rhs.connection_timeout_sec_;
  5543. read_timeout_sec_ = rhs.read_timeout_sec_;
  5544. read_timeout_usec_ = rhs.read_timeout_usec_;
  5545. write_timeout_sec_ = rhs.write_timeout_sec_;
  5546. write_timeout_usec_ = rhs.write_timeout_usec_;
  5547. max_timeout_msec_ = rhs.max_timeout_msec_;
  5548. basic_auth_username_ = rhs.basic_auth_username_;
  5549. basic_auth_password_ = rhs.basic_auth_password_;
  5550. bearer_token_auth_token_ = rhs.bearer_token_auth_token_;
  5551. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  5552. digest_auth_username_ = rhs.digest_auth_username_;
  5553. digest_auth_password_ = rhs.digest_auth_password_;
  5554. #endif
  5555. keep_alive_ = rhs.keep_alive_;
  5556. follow_location_ = rhs.follow_location_;
  5557. path_encode_ = rhs.path_encode_;
  5558. address_family_ = rhs.address_family_;
  5559. tcp_nodelay_ = rhs.tcp_nodelay_;
  5560. ipv6_v6only_ = rhs.ipv6_v6only_;
  5561. socket_options_ = rhs.socket_options_;
  5562. compress_ = rhs.compress_;
  5563. decompress_ = rhs.decompress_;
  5564. interface_ = rhs.interface_;
  5565. proxy_host_ = rhs.proxy_host_;
  5566. proxy_port_ = rhs.proxy_port_;
  5567. proxy_basic_auth_username_ = rhs.proxy_basic_auth_username_;
  5568. proxy_basic_auth_password_ = rhs.proxy_basic_auth_password_;
  5569. proxy_bearer_token_auth_token_ = rhs.proxy_bearer_token_auth_token_;
  5570. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  5571. proxy_digest_auth_username_ = rhs.proxy_digest_auth_username_;
  5572. proxy_digest_auth_password_ = rhs.proxy_digest_auth_password_;
  5573. #endif
  5574. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  5575. ca_cert_file_path_ = rhs.ca_cert_file_path_;
  5576. ca_cert_dir_path_ = rhs.ca_cert_dir_path_;
  5577. ca_cert_store_ = rhs.ca_cert_store_;
  5578. #endif
  5579. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  5580. server_certificate_verification_ = rhs.server_certificate_verification_;
  5581. server_hostname_verification_ = rhs.server_hostname_verification_;
  5582. server_certificate_verifier_ = rhs.server_certificate_verifier_;
  5583. #endif
  5584. logger_ = rhs.logger_;
  5585. error_logger_ = rhs.error_logger_;
  5586. }
  5587. socket_t ClientImpl::create_client_socket(Error &error) const {
  5588. if (!proxy_host_.empty() && proxy_port_ != -1) {
  5589. return detail::create_client_socket(
  5590. proxy_host_, std::string(), proxy_port_, address_family_, tcp_nodelay_,
  5591. ipv6_v6only_, socket_options_, connection_timeout_sec_,
  5592. connection_timeout_usec_, read_timeout_sec_, read_timeout_usec_,
  5593. write_timeout_sec_, write_timeout_usec_, interface_, error);
  5594. }
  5595. // Check is custom IP specified for host_
  5596. std::string ip;
  5597. auto it = addr_map_.find(host_);
  5598. if (it != addr_map_.end()) { ip = it->second; }
  5599. return detail::create_client_socket(
  5600. host_, ip, port_, address_family_, tcp_nodelay_, ipv6_v6only_,
  5601. socket_options_, connection_timeout_sec_, connection_timeout_usec_,
  5602. read_timeout_sec_, read_timeout_usec_, write_timeout_sec_,
  5603. write_timeout_usec_, interface_, error);
  5604. }
  5605. bool ClientImpl::create_and_connect_socket(Socket &socket,
  5606. Error &error) {
  5607. auto sock = create_client_socket(error);
  5608. if (sock == INVALID_SOCKET) { return false; }
  5609. socket.sock = sock;
  5610. return true;
  5611. }
  5612. bool ClientImpl::ensure_socket_connection(Socket &socket, Error &error) {
  5613. return create_and_connect_socket(socket, error);
  5614. }
  5615. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  5616. bool SSLClient::ensure_socket_connection(Socket &socket, Error &error) {
  5617. if (!ClientImpl::ensure_socket_connection(socket, error)) { return false; }
  5618. if (!proxy_host_.empty() && proxy_port_ != -1) { return true; }
  5619. if (!initialize_ssl(socket, error)) {
  5620. shutdown_socket(socket);
  5621. close_socket(socket);
  5622. return false;
  5623. }
  5624. return true;
  5625. }
  5626. #endif
  5627. void ClientImpl::shutdown_ssl(Socket & /*socket*/,
  5628. bool /*shutdown_gracefully*/) {
  5629. // If there are any requests in flight from threads other than us, then it's
  5630. // a thread-unsafe race because individual ssl* objects are not thread-safe.
  5631. assert(socket_requests_in_flight_ == 0 ||
  5632. socket_requests_are_from_thread_ == std::this_thread::get_id());
  5633. }
  5634. void ClientImpl::shutdown_socket(Socket &socket) const {
  5635. if (socket.sock == INVALID_SOCKET) { return; }
  5636. detail::shutdown_socket(socket.sock);
  5637. }
  5638. void ClientImpl::close_socket(Socket &socket) {
  5639. // If there are requests in flight in another thread, usually closing
  5640. // the socket will be fine and they will simply receive an error when
  5641. // using the closed socket, but it is still a bug since rarely the OS
  5642. // may reassign the socket id to be used for a new socket, and then
  5643. // suddenly they will be operating on a live socket that is different
  5644. // than the one they intended!
  5645. assert(socket_requests_in_flight_ == 0 ||
  5646. socket_requests_are_from_thread_ == std::this_thread::get_id());
  5647. // It is also a bug if this happens while SSL is still active
  5648. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  5649. assert(socket.ssl == nullptr);
  5650. #endif
  5651. if (socket.sock == INVALID_SOCKET) { return; }
  5652. detail::close_socket(socket.sock);
  5653. socket.sock = INVALID_SOCKET;
  5654. }
  5655. bool ClientImpl::read_response_line(Stream &strm, const Request &req,
  5656. Response &res) const {
  5657. std::array<char, 2048> buf{};
  5658. detail::stream_line_reader line_reader(strm, buf.data(), buf.size());
  5659. if (!line_reader.getline()) { return false; }
  5660. #ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR
  5661. thread_local const std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r?\n");
  5662. #else
  5663. thread_local const std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r\n");
  5664. #endif
  5665. std::cmatch m;
  5666. if (!std::regex_match(line_reader.ptr(), m, re)) {
  5667. return req.method == "CONNECT";
  5668. }
  5669. res.version = std::string(m[1]);
  5670. res.status = std::stoi(std::string(m[2]));
  5671. res.reason = std::string(m[3]);
  5672. // Ignore '100 Continue'
  5673. while (res.status == StatusCode::Continue_100) {
  5674. if (!line_reader.getline()) { return false; } // CRLF
  5675. if (!line_reader.getline()) { return false; } // next response line
  5676. if (!std::regex_match(line_reader.ptr(), m, re)) { return false; }
  5677. res.version = std::string(m[1]);
  5678. res.status = std::stoi(std::string(m[2]));
  5679. res.reason = std::string(m[3]);
  5680. }
  5681. return true;
  5682. }
  5683. bool ClientImpl::send(Request &req, Response &res, Error &error) {
  5684. std::lock_guard<std::recursive_mutex> request_mutex_guard(request_mutex_);
  5685. auto ret = send_(req, res, error);
  5686. if (error == Error::SSLPeerCouldBeClosed_) {
  5687. assert(!ret);
  5688. ret = send_(req, res, error);
  5689. }
  5690. return ret;
  5691. }
  5692. bool ClientImpl::send_(Request &req, Response &res, Error &error) {
  5693. {
  5694. std::lock_guard<std::mutex> guard(socket_mutex_);
  5695. // Set this to false immediately - if it ever gets set to true by the end
  5696. // of the request, we know another thread instructed us to close the
  5697. // socket.
  5698. socket_should_be_closed_when_request_is_done_ = false;
  5699. auto is_alive = false;
  5700. if (socket_.is_open()) {
  5701. is_alive = detail::is_socket_alive(socket_.sock);
  5702. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  5703. if (is_alive && is_ssl()) {
  5704. if (detail::is_ssl_peer_could_be_closed(socket_.ssl, socket_.sock)) {
  5705. is_alive = false;
  5706. }
  5707. }
  5708. #endif
  5709. if (!is_alive) {
  5710. // Attempt to avoid sigpipe by shutting down non-gracefully if it
  5711. // seems like the other side has already closed the connection Also,
  5712. // there cannot be any requests in flight from other threads since we
  5713. // locked request_mutex_, so safe to close everything immediately
  5714. const bool shutdown_gracefully = false;
  5715. shutdown_ssl(socket_, shutdown_gracefully);
  5716. shutdown_socket(socket_);
  5717. close_socket(socket_);
  5718. }
  5719. }
  5720. if (!is_alive) {
  5721. if (!ensure_socket_connection(socket_, error)) {
  5722. output_error_log(error, &req);
  5723. return false;
  5724. }
  5725. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  5726. // TODO: refactoring
  5727. if (is_ssl()) {
  5728. auto &scli = static_cast<SSLClient &>(*this);
  5729. if (!proxy_host_.empty() && proxy_port_ != -1) {
  5730. auto success = false;
  5731. if (!scli.connect_with_proxy(socket_, req.start_time_, res, success,
  5732. error)) {
  5733. if (!success) { output_error_log(error, &req); }
  5734. return success;
  5735. }
  5736. }
  5737. if (!proxy_host_.empty() && proxy_port_ != -1) {
  5738. if (!scli.initialize_ssl(socket_, error)) {
  5739. output_error_log(error, &req);
  5740. return false;
  5741. }
  5742. }
  5743. }
  5744. #endif
  5745. }
  5746. // Mark the current socket as being in use so that it cannot be closed by
  5747. // anyone else while this request is ongoing, even though we will be
  5748. // releasing the mutex.
  5749. if (socket_requests_in_flight_ > 1) {
  5750. assert(socket_requests_are_from_thread_ == std::this_thread::get_id());
  5751. }
  5752. socket_requests_in_flight_ += 1;
  5753. socket_requests_are_from_thread_ = std::this_thread::get_id();
  5754. }
  5755. for (const auto &header : default_headers_) {
  5756. if (req.headers.find(header.first) == req.headers.end()) {
  5757. req.headers.insert(header);
  5758. }
  5759. }
  5760. auto ret = false;
  5761. auto close_connection = !keep_alive_;
  5762. auto se = detail::scope_exit([&]() {
  5763. // Briefly lock mutex in order to mark that a request is no longer ongoing
  5764. std::lock_guard<std::mutex> guard(socket_mutex_);
  5765. socket_requests_in_flight_ -= 1;
  5766. if (socket_requests_in_flight_ <= 0) {
  5767. assert(socket_requests_in_flight_ == 0);
  5768. socket_requests_are_from_thread_ = std::thread::id();
  5769. }
  5770. if (socket_should_be_closed_when_request_is_done_ || close_connection ||
  5771. !ret) {
  5772. shutdown_ssl(socket_, true);
  5773. shutdown_socket(socket_);
  5774. close_socket(socket_);
  5775. }
  5776. });
  5777. ret = process_socket(socket_, req.start_time_, [&](Stream &strm) {
  5778. return handle_request(strm, req, res, close_connection, error);
  5779. });
  5780. if (!ret) {
  5781. if (error == Error::Success) {
  5782. error = Error::Unknown;
  5783. output_error_log(error, &req);
  5784. }
  5785. }
  5786. return ret;
  5787. }
  5788. Result ClientImpl::send(const Request &req) {
  5789. auto req2 = req;
  5790. return send_(std::move(req2));
  5791. }
  5792. Result ClientImpl::send_(Request &&req) {
  5793. auto res = detail::make_unique<Response>();
  5794. auto error = Error::Success;
  5795. auto ret = send(req, *res, error);
  5796. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  5797. return Result{ret ? std::move(res) : nullptr, error, std::move(req.headers),
  5798. last_ssl_error_, last_openssl_error_};
  5799. #else
  5800. return Result{ret ? std::move(res) : nullptr, error, std::move(req.headers)};
  5801. #endif
  5802. }
  5803. void ClientImpl::prepare_default_headers(Request &r, bool for_stream,
  5804. const std::string &ct) {
  5805. (void)for_stream;
  5806. for (const auto &header : default_headers_) {
  5807. if (!r.has_header(header.first)) { r.headers.insert(header); }
  5808. }
  5809. if (!r.has_header("Host")) {
  5810. if (address_family_ == AF_UNIX) {
  5811. r.headers.emplace("Host", "localhost");
  5812. } else {
  5813. r.headers.emplace(
  5814. "Host", detail::make_host_and_port_string(host_, port_, is_ssl()));
  5815. }
  5816. }
  5817. if (!r.has_header("Accept")) { r.headers.emplace("Accept", "*/*"); }
  5818. if (!r.content_receiver) {
  5819. if (!r.has_header("Accept-Encoding")) {
  5820. std::string accept_encoding;
  5821. #ifdef CPPHTTPLIB_BROTLI_SUPPORT
  5822. accept_encoding = "br";
  5823. #endif
  5824. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  5825. if (!accept_encoding.empty()) { accept_encoding += ", "; }
  5826. accept_encoding += "gzip, deflate";
  5827. #endif
  5828. #ifdef CPPHTTPLIB_ZSTD_SUPPORT
  5829. if (!accept_encoding.empty()) { accept_encoding += ", "; }
  5830. accept_encoding += "zstd";
  5831. #endif
  5832. r.set_header("Accept-Encoding", accept_encoding);
  5833. }
  5834. #ifndef CPPHTTPLIB_NO_DEFAULT_USER_AGENT
  5835. if (!r.has_header("User-Agent")) {
  5836. auto agent = std::string("cpp-httplib/") + CPPHTTPLIB_VERSION;
  5837. r.set_header("User-Agent", agent);
  5838. }
  5839. #endif
  5840. }
  5841. if (!r.body.empty()) {
  5842. if (!ct.empty() && !r.has_header("Content-Type")) {
  5843. r.headers.emplace("Content-Type", ct);
  5844. }
  5845. if (!r.has_header("Content-Length")) {
  5846. r.headers.emplace("Content-Length", std::to_string(r.body.size()));
  5847. }
  5848. }
  5849. }
  5850. ClientImpl::StreamHandle
  5851. ClientImpl::open_stream(const std::string &method, const std::string &path,
  5852. const Params &params, const Headers &headers,
  5853. const std::string &body,
  5854. const std::string &content_type) {
  5855. StreamHandle handle;
  5856. handle.response = detail::make_unique<Response>();
  5857. handle.error = Error::Success;
  5858. auto query_path = params.empty() ? path : append_query_params(path, params);
  5859. handle.connection_ = detail::make_unique<ClientConnection>();
  5860. {
  5861. std::lock_guard<std::mutex> guard(socket_mutex_);
  5862. auto is_alive = false;
  5863. if (socket_.is_open()) {
  5864. is_alive = detail::is_socket_alive(socket_.sock);
  5865. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  5866. if (is_alive && is_ssl()) {
  5867. if (detail::is_ssl_peer_could_be_closed(socket_.ssl, socket_.sock)) {
  5868. is_alive = false;
  5869. }
  5870. }
  5871. #endif
  5872. if (!is_alive) {
  5873. shutdown_ssl(socket_, false);
  5874. shutdown_socket(socket_);
  5875. close_socket(socket_);
  5876. }
  5877. }
  5878. if (!is_alive) {
  5879. if (!ensure_socket_connection(socket_, handle.error)) {
  5880. handle.response.reset();
  5881. return handle;
  5882. }
  5883. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  5884. if (is_ssl()) {
  5885. auto &scli = static_cast<SSLClient &>(*this);
  5886. if (!proxy_host_.empty() && proxy_port_ != -1) {
  5887. if (!scli.initialize_ssl(socket_, handle.error)) {
  5888. handle.response.reset();
  5889. return handle;
  5890. }
  5891. }
  5892. }
  5893. #endif
  5894. }
  5895. transfer_socket_ownership_to_handle(handle);
  5896. }
  5897. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  5898. if (is_ssl() && handle.connection_->ssl) {
  5899. handle.socket_stream_ = detail::make_unique<detail::SSLSocketStream>(
  5900. handle.connection_->sock, handle.connection_->ssl, read_timeout_sec_,
  5901. read_timeout_usec_, write_timeout_sec_, write_timeout_usec_);
  5902. } else {
  5903. handle.socket_stream_ = detail::make_unique<detail::SocketStream>(
  5904. handle.connection_->sock, read_timeout_sec_, read_timeout_usec_,
  5905. write_timeout_sec_, write_timeout_usec_);
  5906. }
  5907. #else
  5908. handle.socket_stream_ = detail::make_unique<detail::SocketStream>(
  5909. handle.connection_->sock, read_timeout_sec_, read_timeout_usec_,
  5910. write_timeout_sec_, write_timeout_usec_);
  5911. #endif
  5912. handle.stream_ = handle.socket_stream_.get();
  5913. Request req;
  5914. req.method = method;
  5915. req.path = query_path;
  5916. req.headers = headers;
  5917. req.body = body;
  5918. prepare_default_headers(req, true, content_type);
  5919. auto &strm = *handle.stream_;
  5920. if (detail::write_request_line(strm, req.method, req.path) < 0) {
  5921. handle.error = Error::Write;
  5922. handle.response.reset();
  5923. return handle;
  5924. }
  5925. if (!detail::check_and_write_headers(strm, req.headers, header_writer_,
  5926. handle.error)) {
  5927. handle.response.reset();
  5928. return handle;
  5929. }
  5930. if (!body.empty()) {
  5931. if (strm.write(body.data(), body.size()) < 0) {
  5932. handle.error = Error::Write;
  5933. handle.response.reset();
  5934. return handle;
  5935. }
  5936. }
  5937. if (!read_response_line(strm, req, *handle.response) ||
  5938. !detail::read_headers(strm, handle.response->headers)) {
  5939. handle.error = Error::Read;
  5940. handle.response.reset();
  5941. return handle;
  5942. }
  5943. handle.body_reader_.stream = handle.stream_;
  5944. auto content_length_str = handle.response->get_header_value("Content-Length");
  5945. if (!content_length_str.empty()) {
  5946. handle.body_reader_.content_length =
  5947. static_cast<size_t>(std::stoull(content_length_str));
  5948. }
  5949. auto transfer_encoding =
  5950. handle.response->get_header_value("Transfer-Encoding");
  5951. handle.body_reader_.chunked = (transfer_encoding == "chunked");
  5952. auto content_encoding = handle.response->get_header_value("Content-Encoding");
  5953. if (!content_encoding.empty()) {
  5954. handle.decompressor_ = detail::create_decompressor(content_encoding);
  5955. }
  5956. return handle;
  5957. }
  5958. ssize_t ClientImpl::StreamHandle::read(char *buf, size_t len) {
  5959. if (!is_valid() || !response) { return -1; }
  5960. if (decompressor_) { return read_with_decompression(buf, len); }
  5961. auto n = detail::read_body_content(stream_, body_reader_, buf, len);
  5962. if (n <= 0 && body_reader_.chunked && !trailers_parsed_ && stream_) {
  5963. trailers_parsed_ = true;
  5964. if (body_reader_.chunked_decoder) {
  5965. if (!body_reader_.chunked_decoder->parse_trailers_into(
  5966. response->trailers, response->headers)) {
  5967. return n;
  5968. }
  5969. } else {
  5970. detail::ChunkedDecoder dec(*stream_);
  5971. if (!dec.parse_trailers_into(response->trailers, response->headers)) {
  5972. return n;
  5973. }
  5974. }
  5975. }
  5976. return n;
  5977. }
  5978. ssize_t ClientImpl::StreamHandle::read_with_decompression(char *buf,
  5979. size_t len) {
  5980. if (decompress_offset_ < decompress_buffer_.size()) {
  5981. auto available = decompress_buffer_.size() - decompress_offset_;
  5982. auto to_copy = (std::min)(len, available);
  5983. std::memcpy(buf, decompress_buffer_.data() + decompress_offset_, to_copy);
  5984. decompress_offset_ += to_copy;
  5985. return static_cast<ssize_t>(to_copy);
  5986. }
  5987. decompress_buffer_.clear();
  5988. decompress_offset_ = 0;
  5989. constexpr size_t kDecompressionBufferSize = 8192;
  5990. char compressed_buf[kDecompressionBufferSize];
  5991. while (true) {
  5992. auto n = detail::read_body_content(stream_, body_reader_, compressed_buf,
  5993. sizeof(compressed_buf));
  5994. if (n <= 0) { return n; }
  5995. bool decompress_ok =
  5996. decompressor_->decompress(compressed_buf, static_cast<size_t>(n),
  5997. [this](const char *data, size_t data_len) {
  5998. decompress_buffer_.append(data, data_len);
  5999. return true;
  6000. });
  6001. if (!decompress_ok) {
  6002. body_reader_.last_error = Error::Read;
  6003. return -1;
  6004. }
  6005. if (!decompress_buffer_.empty()) { break; }
  6006. }
  6007. auto to_copy = (std::min)(len, decompress_buffer_.size());
  6008. std::memcpy(buf, decompress_buffer_.data(), to_copy);
  6009. decompress_offset_ = to_copy;
  6010. return static_cast<ssize_t>(to_copy);
  6011. }
  6012. void ClientImpl::StreamHandle::parse_trailers_if_needed() {
  6013. if (!response || !stream_ || !body_reader_.chunked || trailers_parsed_) {
  6014. return;
  6015. }
  6016. trailers_parsed_ = true;
  6017. const auto bufsiz = 128;
  6018. char line_buf[bufsiz];
  6019. detail::stream_line_reader line_reader(*stream_, line_buf, bufsiz);
  6020. if (!line_reader.getline()) { return; }
  6021. if (!detail::parse_trailers(line_reader, response->trailers,
  6022. response->headers)) {
  6023. return;
  6024. }
  6025. }
  6026. // Inline method implementations for `ChunkedDecoder`.
  6027. namespace detail {
  6028. ChunkedDecoder::ChunkedDecoder(Stream &s) : strm(s) {}
  6029. ssize_t ChunkedDecoder::read_payload(char *buf, size_t len,
  6030. size_t &out_chunk_offset,
  6031. size_t &out_chunk_total) {
  6032. if (finished) { return 0; }
  6033. if (chunk_remaining == 0) {
  6034. stream_line_reader lr(strm, line_buf, sizeof(line_buf));
  6035. if (!lr.getline()) { return -1; }
  6036. char *endptr = nullptr;
  6037. unsigned long chunk_len = std::strtoul(lr.ptr(), &endptr, 16);
  6038. if (endptr == lr.ptr()) { return -1; }
  6039. if (chunk_len == ULONG_MAX) { return -1; }
  6040. if (chunk_len == 0) {
  6041. chunk_remaining = 0;
  6042. finished = true;
  6043. out_chunk_offset = 0;
  6044. out_chunk_total = 0;
  6045. return 0;
  6046. }
  6047. chunk_remaining = static_cast<size_t>(chunk_len);
  6048. last_chunk_total = chunk_remaining;
  6049. last_chunk_offset = 0;
  6050. }
  6051. auto to_read = (std::min)(chunk_remaining, len);
  6052. auto n = strm.read(buf, to_read);
  6053. if (n <= 0) { return -1; }
  6054. auto offset_before = last_chunk_offset;
  6055. last_chunk_offset += static_cast<size_t>(n);
  6056. chunk_remaining -= static_cast<size_t>(n);
  6057. out_chunk_offset = offset_before;
  6058. out_chunk_total = last_chunk_total;
  6059. if (chunk_remaining == 0) {
  6060. stream_line_reader lr(strm, line_buf, sizeof(line_buf));
  6061. if (!lr.getline()) { return -1; }
  6062. if (std::strcmp(lr.ptr(), "\r\n") != 0) { return -1; }
  6063. }
  6064. return n;
  6065. }
  6066. bool ChunkedDecoder::parse_trailers_into(Headers &dest,
  6067. const Headers &src_headers) {
  6068. stream_line_reader lr(strm, line_buf, sizeof(line_buf));
  6069. if (!lr.getline()) { return false; }
  6070. return parse_trailers(lr, dest, src_headers);
  6071. }
  6072. } // namespace detail
  6073. void
  6074. ClientImpl::transfer_socket_ownership_to_handle(StreamHandle &handle) {
  6075. handle.connection_->sock = socket_.sock;
  6076. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  6077. handle.connection_->ssl = socket_.ssl;
  6078. socket_.ssl = nullptr;
  6079. #endif
  6080. socket_.sock = INVALID_SOCKET;
  6081. }
  6082. bool ClientImpl::handle_request(Stream &strm, Request &req,
  6083. Response &res, bool close_connection,
  6084. Error &error) {
  6085. if (req.path.empty()) {
  6086. error = Error::Connection;
  6087. output_error_log(error, &req);
  6088. return false;
  6089. }
  6090. auto req_save = req;
  6091. bool ret;
  6092. if (!is_ssl() && !proxy_host_.empty() && proxy_port_ != -1) {
  6093. auto req2 = req;
  6094. req2.path = "http://" +
  6095. detail::make_host_and_port_string(host_, port_, false) +
  6096. req.path;
  6097. ret = process_request(strm, req2, res, close_connection, error);
  6098. req = std::move(req2);
  6099. req.path = req_save.path;
  6100. } else {
  6101. ret = process_request(strm, req, res, close_connection, error);
  6102. }
  6103. if (!ret) { return false; }
  6104. if (res.get_header_value("Connection") == "close" ||
  6105. (res.version == "HTTP/1.0" && res.reason != "Connection established")) {
  6106. // TODO this requires a not-entirely-obvious chain of calls to be correct
  6107. // for this to be safe.
  6108. // This is safe to call because handle_request is only called by send_
  6109. // which locks the request mutex during the process. It would be a bug
  6110. // to call it from a different thread since it's a thread-safety issue
  6111. // to do these things to the socket if another thread is using the socket.
  6112. std::lock_guard<std::mutex> guard(socket_mutex_);
  6113. shutdown_ssl(socket_, true);
  6114. shutdown_socket(socket_);
  6115. close_socket(socket_);
  6116. }
  6117. if (300 < res.status && res.status < 400 && follow_location_) {
  6118. req = std::move(req_save);
  6119. ret = redirect(req, res, error);
  6120. }
  6121. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  6122. if ((res.status == StatusCode::Unauthorized_401 ||
  6123. res.status == StatusCode::ProxyAuthenticationRequired_407) &&
  6124. req.authorization_count_ < 5) {
  6125. auto is_proxy = res.status == StatusCode::ProxyAuthenticationRequired_407;
  6126. const auto &username =
  6127. is_proxy ? proxy_digest_auth_username_ : digest_auth_username_;
  6128. const auto &password =
  6129. is_proxy ? proxy_digest_auth_password_ : digest_auth_password_;
  6130. if (!username.empty() && !password.empty()) {
  6131. std::map<std::string, std::string> auth;
  6132. if (detail::parse_www_authenticate(res, auth, is_proxy)) {
  6133. Request new_req = req;
  6134. new_req.authorization_count_ += 1;
  6135. new_req.headers.erase(is_proxy ? "Proxy-Authorization"
  6136. : "Authorization");
  6137. new_req.headers.insert(detail::make_digest_authentication_header(
  6138. req, auth, new_req.authorization_count_, detail::random_string(10),
  6139. username, password, is_proxy));
  6140. Response new_res;
  6141. ret = send(new_req, new_res, error);
  6142. if (ret) { res = std::move(new_res); }
  6143. }
  6144. }
  6145. }
  6146. #endif
  6147. return ret;
  6148. }
  6149. bool ClientImpl::redirect(Request &req, Response &res, Error &error) {
  6150. if (req.redirect_count_ == 0) {
  6151. error = Error::ExceedRedirectCount;
  6152. output_error_log(error, &req);
  6153. return false;
  6154. }
  6155. auto location = res.get_header_value("location");
  6156. if (location.empty()) { return false; }
  6157. thread_local const std::regex re(
  6158. R"((?:(https?):)?(?://(?:\[([a-fA-F\d:]+)\]|([^:/?#]+))(?::(\d+))?)?([^?#]*)(\?[^#]*)?(?:#.*)?)");
  6159. std::smatch m;
  6160. if (!std::regex_match(location, m, re)) { return false; }
  6161. auto scheme = is_ssl() ? "https" : "http";
  6162. auto next_scheme = m[1].str();
  6163. auto next_host = m[2].str();
  6164. if (next_host.empty()) { next_host = m[3].str(); }
  6165. auto port_str = m[4].str();
  6166. auto next_path = m[5].str();
  6167. auto next_query = m[6].str();
  6168. auto next_port = port_;
  6169. if (!port_str.empty()) {
  6170. next_port = std::stoi(port_str);
  6171. } else if (!next_scheme.empty()) {
  6172. next_port = next_scheme == "https" ? 443 : 80;
  6173. }
  6174. if (next_scheme.empty()) { next_scheme = scheme; }
  6175. if (next_host.empty()) { next_host = host_; }
  6176. if (next_path.empty()) { next_path = "/"; }
  6177. auto path = decode_query_component(next_path, true) + next_query;
  6178. // Same host redirect - use current client
  6179. if (next_scheme == scheme && next_host == host_ && next_port == port_) {
  6180. return detail::redirect(*this, req, res, path, location, error);
  6181. }
  6182. // Cross-host/scheme redirect - create new client with robust setup
  6183. return create_redirect_client(next_scheme, next_host, next_port, req, res,
  6184. path, location, error);
  6185. }
  6186. // New method for robust redirect client creation
  6187. bool ClientImpl::create_redirect_client(
  6188. const std::string &scheme, const std::string &host, int port, Request &req,
  6189. Response &res, const std::string &path, const std::string &location,
  6190. Error &error) {
  6191. // Determine if we need SSL
  6192. auto need_ssl = (scheme == "https");
  6193. // Clean up request headers that are host/client specific
  6194. // Remove headers that should not be carried over to new host
  6195. auto headers_to_remove =
  6196. std::vector<std::string>{"Host", "Proxy-Authorization", "Authorization"};
  6197. for (const auto &header_name : headers_to_remove) {
  6198. auto it = req.headers.find(header_name);
  6199. while (it != req.headers.end()) {
  6200. it = req.headers.erase(it);
  6201. it = req.headers.find(header_name);
  6202. }
  6203. }
  6204. // Create appropriate client type and handle redirect
  6205. if (need_ssl) {
  6206. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  6207. // Create SSL client for HTTPS redirect
  6208. SSLClient redirect_client(host, port);
  6209. // Setup basic client configuration first
  6210. setup_redirect_client(redirect_client);
  6211. // SSL-specific configuration for proxy environments
  6212. if (!proxy_host_.empty() && proxy_port_ != -1) {
  6213. // Critical: Disable SSL verification for proxy environments
  6214. redirect_client.enable_server_certificate_verification(false);
  6215. redirect_client.enable_server_hostname_verification(false);
  6216. } else {
  6217. // For direct SSL connections, copy SSL verification settings
  6218. redirect_client.enable_server_certificate_verification(
  6219. server_certificate_verification_);
  6220. redirect_client.enable_server_hostname_verification(
  6221. server_hostname_verification_);
  6222. }
  6223. // Handle CA certificate store and paths if available
  6224. if (ca_cert_store_ && X509_STORE_up_ref(ca_cert_store_)) {
  6225. redirect_client.set_ca_cert_store(ca_cert_store_);
  6226. }
  6227. if (!ca_cert_file_path_.empty()) {
  6228. redirect_client.set_ca_cert_path(ca_cert_file_path_, ca_cert_dir_path_);
  6229. }
  6230. // Client certificates are set through constructor for SSLClient
  6231. // NOTE: SSLClient constructor already takes client_cert_path and
  6232. // client_key_path so we need to create it properly if client certs are
  6233. // needed
  6234. // Execute the redirect
  6235. return detail::redirect(redirect_client, req, res, path, location, error);
  6236. #else
  6237. // SSL not supported - set appropriate error
  6238. error = Error::SSLConnection;
  6239. output_error_log(error, &req);
  6240. return false;
  6241. #endif
  6242. } else {
  6243. // HTTP redirect
  6244. ClientImpl redirect_client(host, port);
  6245. // Setup client with robust configuration
  6246. setup_redirect_client(redirect_client);
  6247. // Execute the redirect
  6248. return detail::redirect(redirect_client, req, res, path, location, error);
  6249. }
  6250. }
  6251. // New method for robust client setup (based on basic_manual_redirect.cpp
  6252. // logic)
  6253. template <typename ClientType>
  6254. void ClientImpl::setup_redirect_client(ClientType &client) {
  6255. // Copy basic settings first
  6256. client.set_connection_timeout(connection_timeout_sec_);
  6257. client.set_read_timeout(read_timeout_sec_, read_timeout_usec_);
  6258. client.set_write_timeout(write_timeout_sec_, write_timeout_usec_);
  6259. client.set_keep_alive(keep_alive_);
  6260. client.set_follow_location(
  6261. true); // Enable redirects to handle multi-step redirects
  6262. client.set_path_encode(path_encode_);
  6263. client.set_compress(compress_);
  6264. client.set_decompress(decompress_);
  6265. // Copy authentication settings BEFORE proxy setup
  6266. if (!basic_auth_username_.empty()) {
  6267. client.set_basic_auth(basic_auth_username_, basic_auth_password_);
  6268. }
  6269. if (!bearer_token_auth_token_.empty()) {
  6270. client.set_bearer_token_auth(bearer_token_auth_token_);
  6271. }
  6272. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  6273. if (!digest_auth_username_.empty()) {
  6274. client.set_digest_auth(digest_auth_username_, digest_auth_password_);
  6275. }
  6276. #endif
  6277. // Setup proxy configuration (CRITICAL ORDER - proxy must be set
  6278. // before proxy auth)
  6279. if (!proxy_host_.empty() && proxy_port_ != -1) {
  6280. // First set proxy host and port
  6281. client.set_proxy(proxy_host_, proxy_port_);
  6282. // Then set proxy authentication (order matters!)
  6283. if (!proxy_basic_auth_username_.empty()) {
  6284. client.set_proxy_basic_auth(proxy_basic_auth_username_,
  6285. proxy_basic_auth_password_);
  6286. }
  6287. if (!proxy_bearer_token_auth_token_.empty()) {
  6288. client.set_proxy_bearer_token_auth(proxy_bearer_token_auth_token_);
  6289. }
  6290. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  6291. if (!proxy_digest_auth_username_.empty()) {
  6292. client.set_proxy_digest_auth(proxy_digest_auth_username_,
  6293. proxy_digest_auth_password_);
  6294. }
  6295. #endif
  6296. }
  6297. // Copy network and socket settings
  6298. client.set_address_family(address_family_);
  6299. client.set_tcp_nodelay(tcp_nodelay_);
  6300. client.set_ipv6_v6only(ipv6_v6only_);
  6301. if (socket_options_) { client.set_socket_options(socket_options_); }
  6302. if (!interface_.empty()) { client.set_interface(interface_); }
  6303. // Copy logging and headers
  6304. if (logger_) { client.set_logger(logger_); }
  6305. if (error_logger_) { client.set_error_logger(error_logger_); }
  6306. // NOTE: DO NOT copy default_headers_ as they may contain stale Host headers
  6307. // Each new client should generate its own headers based on its target host
  6308. }
  6309. bool ClientImpl::write_content_with_provider(Stream &strm,
  6310. const Request &req,
  6311. Error &error) const {
  6312. auto is_shutting_down = []() { return false; };
  6313. if (req.is_chunked_content_provider_) {
  6314. // TODO: Brotli support
  6315. std::unique_ptr<detail::compressor> compressor;
  6316. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  6317. if (compress_) {
  6318. compressor = detail::make_unique<detail::gzip_compressor>();
  6319. } else
  6320. #endif
  6321. {
  6322. compressor = detail::make_unique<detail::nocompressor>();
  6323. }
  6324. return detail::write_content_chunked(strm, req.content_provider_,
  6325. is_shutting_down, *compressor, error);
  6326. } else {
  6327. return detail::write_content_with_progress(
  6328. strm, req.content_provider_, 0, req.content_length_, is_shutting_down,
  6329. req.upload_progress, error);
  6330. }
  6331. }
  6332. bool ClientImpl::write_request(Stream &strm, Request &req,
  6333. bool close_connection, Error &error) {
  6334. // Prepare additional headers
  6335. if (close_connection) {
  6336. if (!req.has_header("Connection")) {
  6337. req.set_header("Connection", "close");
  6338. }
  6339. }
  6340. std::string ct_for_defaults;
  6341. if (!req.has_header("Content-Type") && !req.body.empty()) {
  6342. ct_for_defaults = "text/plain";
  6343. }
  6344. prepare_default_headers(req, false, ct_for_defaults);
  6345. if (req.body.empty()) {
  6346. if (req.content_provider_) {
  6347. if (!req.is_chunked_content_provider_) {
  6348. if (!req.has_header("Content-Length")) {
  6349. auto length = std::to_string(req.content_length_);
  6350. req.set_header("Content-Length", length);
  6351. }
  6352. }
  6353. } else {
  6354. if (req.method == "POST" || req.method == "PUT" ||
  6355. req.method == "PATCH") {
  6356. req.set_header("Content-Length", "0");
  6357. }
  6358. }
  6359. }
  6360. if (!basic_auth_password_.empty() || !basic_auth_username_.empty()) {
  6361. if (!req.has_header("Authorization")) {
  6362. req.headers.insert(make_basic_authentication_header(
  6363. basic_auth_username_, basic_auth_password_, false));
  6364. }
  6365. }
  6366. if (!proxy_basic_auth_username_.empty() &&
  6367. !proxy_basic_auth_password_.empty()) {
  6368. if (!req.has_header("Proxy-Authorization")) {
  6369. req.headers.insert(make_basic_authentication_header(
  6370. proxy_basic_auth_username_, proxy_basic_auth_password_, true));
  6371. }
  6372. }
  6373. if (!bearer_token_auth_token_.empty()) {
  6374. if (!req.has_header("Authorization")) {
  6375. req.headers.insert(make_bearer_token_authentication_header(
  6376. bearer_token_auth_token_, false));
  6377. }
  6378. }
  6379. if (!proxy_bearer_token_auth_token_.empty()) {
  6380. if (!req.has_header("Proxy-Authorization")) {
  6381. req.headers.insert(make_bearer_token_authentication_header(
  6382. proxy_bearer_token_auth_token_, true));
  6383. }
  6384. }
  6385. // Request line and headers
  6386. {
  6387. detail::BufferStream bstrm;
  6388. // Extract path and query from req.path
  6389. std::string path_part, query_part;
  6390. auto query_pos = req.path.find('?');
  6391. if (query_pos != std::string::npos) {
  6392. path_part = req.path.substr(0, query_pos);
  6393. query_part = req.path.substr(query_pos + 1);
  6394. } else {
  6395. path_part = req.path;
  6396. query_part = "";
  6397. }
  6398. // Encode path part. If the original `req.path` already contained a
  6399. // query component, preserve its raw query string (including parameter
  6400. // order) instead of reparsing and reassembling it which may reorder
  6401. // parameters due to container ordering (e.g. `Params` uses
  6402. // `std::multimap`). When there is no query in `req.path`, fall back to
  6403. // building a query from `req.params` so existing callers that pass
  6404. // `Params` continue to work.
  6405. auto path_with_query =
  6406. path_encode_ ? detail::encode_path(path_part) : path_part;
  6407. if (!query_part.empty()) {
  6408. // Normalize the query string (decode then re-encode) while preserving
  6409. // the original parameter order.
  6410. auto normalized = detail::normalize_query_string(query_part);
  6411. if (!normalized.empty()) { path_with_query += '?' + normalized; }
  6412. // Still populate req.params for handlers/users who read them.
  6413. detail::parse_query_text(query_part, req.params);
  6414. } else {
  6415. // No query in path; parse any query_part (empty) and append params
  6416. // from `req.params` when present (preserves prior behavior for
  6417. // callers who provide Params separately).
  6418. detail::parse_query_text(query_part, req.params);
  6419. if (!req.params.empty()) {
  6420. path_with_query = append_query_params(path_with_query, req.params);
  6421. }
  6422. }
  6423. // Write request line and headers
  6424. detail::write_request_line(bstrm, req.method, path_with_query);
  6425. if (!detail::check_and_write_headers(bstrm, req.headers, header_writer_,
  6426. error)) {
  6427. output_error_log(error, &req);
  6428. return false;
  6429. }
  6430. // Flush buffer
  6431. auto &data = bstrm.get_buffer();
  6432. if (!detail::write_data(strm, data.data(), data.size())) {
  6433. error = Error::Write;
  6434. output_error_log(error, &req);
  6435. return false;
  6436. }
  6437. }
  6438. // Body
  6439. if (req.body.empty()) {
  6440. return write_content_with_provider(strm, req, error);
  6441. }
  6442. if (req.upload_progress) {
  6443. auto body_size = req.body.size();
  6444. size_t written = 0;
  6445. auto data = req.body.data();
  6446. while (written < body_size) {
  6447. size_t to_write = (std::min)(CPPHTTPLIB_SEND_BUFSIZ, body_size - written);
  6448. if (!detail::write_data(strm, data + written, to_write)) {
  6449. error = Error::Write;
  6450. output_error_log(error, &req);
  6451. return false;
  6452. }
  6453. written += to_write;
  6454. if (!req.upload_progress(written, body_size)) {
  6455. error = Error::Canceled;
  6456. output_error_log(error, &req);
  6457. return false;
  6458. }
  6459. }
  6460. } else {
  6461. if (!detail::write_data(strm, req.body.data(), req.body.size())) {
  6462. error = Error::Write;
  6463. output_error_log(error, &req);
  6464. return false;
  6465. }
  6466. }
  6467. return true;
  6468. }
  6469. std::unique_ptr<Response>
  6470. ClientImpl::send_with_content_provider_and_receiver(
  6471. Request &req, const char *body, size_t content_length,
  6472. ContentProvider content_provider,
  6473. ContentProviderWithoutLength content_provider_without_length,
  6474. const std::string &content_type, ContentReceiver content_receiver,
  6475. Error &error) {
  6476. if (!content_type.empty()) { req.set_header("Content-Type", content_type); }
  6477. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  6478. if (compress_) { req.set_header("Content-Encoding", "gzip"); }
  6479. #endif
  6480. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  6481. if (compress_ && !content_provider_without_length) {
  6482. // TODO: Brotli support
  6483. detail::gzip_compressor compressor;
  6484. if (content_provider) {
  6485. auto ok = true;
  6486. size_t offset = 0;
  6487. DataSink data_sink;
  6488. data_sink.write = [&](const char *data, size_t data_len) -> bool {
  6489. if (ok) {
  6490. auto last = offset + data_len == content_length;
  6491. auto ret = compressor.compress(
  6492. data, data_len, last,
  6493. [&](const char *compressed_data, size_t compressed_data_len) {
  6494. req.body.append(compressed_data, compressed_data_len);
  6495. return true;
  6496. });
  6497. if (ret) {
  6498. offset += data_len;
  6499. } else {
  6500. ok = false;
  6501. }
  6502. }
  6503. return ok;
  6504. };
  6505. while (ok && offset < content_length) {
  6506. if (!content_provider(offset, content_length - offset, data_sink)) {
  6507. error = Error::Canceled;
  6508. output_error_log(error, &req);
  6509. return nullptr;
  6510. }
  6511. }
  6512. } else {
  6513. if (!compressor.compress(body, content_length, true,
  6514. [&](const char *data, size_t data_len) {
  6515. req.body.append(data, data_len);
  6516. return true;
  6517. })) {
  6518. error = Error::Compression;
  6519. output_error_log(error, &req);
  6520. return nullptr;
  6521. }
  6522. }
  6523. } else
  6524. #endif
  6525. {
  6526. if (content_provider) {
  6527. req.content_length_ = content_length;
  6528. req.content_provider_ = std::move(content_provider);
  6529. req.is_chunked_content_provider_ = false;
  6530. } else if (content_provider_without_length) {
  6531. req.content_length_ = 0;
  6532. req.content_provider_ = detail::ContentProviderAdapter(
  6533. std::move(content_provider_without_length));
  6534. req.is_chunked_content_provider_ = true;
  6535. req.set_header("Transfer-Encoding", "chunked");
  6536. } else {
  6537. req.body.assign(body, content_length);
  6538. }
  6539. }
  6540. if (content_receiver) {
  6541. req.content_receiver =
  6542. [content_receiver](const char *data, size_t data_length,
  6543. size_t /*offset*/, size_t /*total_length*/) {
  6544. return content_receiver(data, data_length);
  6545. };
  6546. }
  6547. auto res = detail::make_unique<Response>();
  6548. return send(req, *res, error) ? std::move(res) : nullptr;
  6549. }
  6550. Result ClientImpl::send_with_content_provider_and_receiver(
  6551. const std::string &method, const std::string &path, const Headers &headers,
  6552. const char *body, size_t content_length, ContentProvider content_provider,
  6553. ContentProviderWithoutLength content_provider_without_length,
  6554. const std::string &content_type, ContentReceiver content_receiver,
  6555. UploadProgress progress) {
  6556. Request req;
  6557. req.method = method;
  6558. req.headers = headers;
  6559. req.path = path;
  6560. req.upload_progress = std::move(progress);
  6561. if (max_timeout_msec_ > 0) {
  6562. req.start_time_ = std::chrono::steady_clock::now();
  6563. }
  6564. auto error = Error::Success;
  6565. auto res = send_with_content_provider_and_receiver(
  6566. req, body, content_length, std::move(content_provider),
  6567. std::move(content_provider_without_length), content_type,
  6568. std::move(content_receiver), error);
  6569. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  6570. return Result{std::move(res), error, std::move(req.headers), last_ssl_error_,
  6571. last_openssl_error_};
  6572. #else
  6573. return Result{std::move(res), error, std::move(req.headers)};
  6574. #endif
  6575. }
  6576. void ClientImpl::output_log(const Request &req,
  6577. const Response &res) const {
  6578. if (logger_) {
  6579. std::lock_guard<std::mutex> guard(logger_mutex_);
  6580. logger_(req, res);
  6581. }
  6582. }
  6583. void ClientImpl::output_error_log(const Error &err,
  6584. const Request *req) const {
  6585. if (error_logger_) {
  6586. std::lock_guard<std::mutex> guard(logger_mutex_);
  6587. error_logger_(err, req);
  6588. }
  6589. }
  6590. bool ClientImpl::process_request(Stream &strm, Request &req,
  6591. Response &res, bool close_connection,
  6592. Error &error) {
  6593. // Send request
  6594. if (!write_request(strm, req, close_connection, error)) { return false; }
  6595. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  6596. if (is_ssl()) {
  6597. auto is_proxy_enabled = !proxy_host_.empty() && proxy_port_ != -1;
  6598. if (!is_proxy_enabled) {
  6599. if (detail::is_ssl_peer_could_be_closed(socket_.ssl, socket_.sock)) {
  6600. error = Error::SSLPeerCouldBeClosed_;
  6601. output_error_log(error, &req);
  6602. return false;
  6603. }
  6604. }
  6605. }
  6606. #endif
  6607. // Receive response and headers
  6608. if (!read_response_line(strm, req, res) ||
  6609. !detail::read_headers(strm, res.headers)) {
  6610. error = Error::Read;
  6611. output_error_log(error, &req);
  6612. return false;
  6613. }
  6614. // Body
  6615. if ((res.status != StatusCode::NoContent_204) && req.method != "HEAD" &&
  6616. req.method != "CONNECT") {
  6617. auto redirect = 300 < res.status && res.status < 400 &&
  6618. res.status != StatusCode::NotModified_304 &&
  6619. follow_location_;
  6620. if (req.response_handler && !redirect) {
  6621. if (!req.response_handler(res)) {
  6622. error = Error::Canceled;
  6623. output_error_log(error, &req);
  6624. return false;
  6625. }
  6626. }
  6627. auto out =
  6628. req.content_receiver
  6629. ? static_cast<ContentReceiverWithProgress>(
  6630. [&](const char *buf, size_t n, size_t off, size_t len) {
  6631. if (redirect) { return true; }
  6632. auto ret = req.content_receiver(buf, n, off, len);
  6633. if (!ret) {
  6634. error = Error::Canceled;
  6635. output_error_log(error, &req);
  6636. }
  6637. return ret;
  6638. })
  6639. : static_cast<ContentReceiverWithProgress>(
  6640. [&](const char *buf, size_t n, size_t /*off*/,
  6641. size_t /*len*/) {
  6642. assert(res.body.size() + n <= res.body.max_size());
  6643. res.body.append(buf, n);
  6644. return true;
  6645. });
  6646. auto progress = [&](size_t current, size_t total) {
  6647. if (!req.download_progress || redirect) { return true; }
  6648. auto ret = req.download_progress(current, total);
  6649. if (!ret) {
  6650. error = Error::Canceled;
  6651. output_error_log(error, &req);
  6652. }
  6653. return ret;
  6654. };
  6655. if (res.has_header("Content-Length")) {
  6656. if (!req.content_receiver) {
  6657. auto len = res.get_header_value_u64("Content-Length");
  6658. if (len > res.body.max_size()) {
  6659. error = Error::Read;
  6660. output_error_log(error, &req);
  6661. return false;
  6662. }
  6663. res.body.reserve(static_cast<size_t>(len));
  6664. }
  6665. }
  6666. if (res.status != StatusCode::NotModified_304) {
  6667. int dummy_status;
  6668. if (!detail::read_content(strm, res, (std::numeric_limits<size_t>::max)(),
  6669. dummy_status, std::move(progress),
  6670. std::move(out), decompress_)) {
  6671. if (error != Error::Canceled) { error = Error::Read; }
  6672. output_error_log(error, &req);
  6673. return false;
  6674. }
  6675. }
  6676. }
  6677. // Log
  6678. output_log(req, res);
  6679. return true;
  6680. }
  6681. ContentProviderWithoutLength ClientImpl::get_multipart_content_provider(
  6682. const std::string &boundary, const UploadFormDataItems &items,
  6683. const FormDataProviderItems &provider_items) const {
  6684. size_t cur_item = 0;
  6685. size_t cur_start = 0;
  6686. // cur_item and cur_start are copied to within the std::function and
  6687. // maintain state between successive calls
  6688. return [&, cur_item, cur_start](size_t offset,
  6689. DataSink &sink) mutable -> bool {
  6690. if (!offset && !items.empty()) {
  6691. sink.os << detail::serialize_multipart_formdata(items, boundary, false);
  6692. return true;
  6693. } else if (cur_item < provider_items.size()) {
  6694. if (!cur_start) {
  6695. const auto &begin = detail::serialize_multipart_formdata_item_begin(
  6696. provider_items[cur_item], boundary);
  6697. offset += begin.size();
  6698. cur_start = offset;
  6699. sink.os << begin;
  6700. }
  6701. DataSink cur_sink;
  6702. auto has_data = true;
  6703. cur_sink.write = sink.write;
  6704. cur_sink.done = [&]() { has_data = false; };
  6705. if (!provider_items[cur_item].provider(offset - cur_start, cur_sink)) {
  6706. return false;
  6707. }
  6708. if (!has_data) {
  6709. sink.os << detail::serialize_multipart_formdata_item_end();
  6710. cur_item++;
  6711. cur_start = 0;
  6712. }
  6713. return true;
  6714. } else {
  6715. sink.os << detail::serialize_multipart_formdata_finish(boundary);
  6716. sink.done();
  6717. return true;
  6718. }
  6719. };
  6720. }
  6721. bool ClientImpl::process_socket(
  6722. const Socket &socket,
  6723. std::chrono::time_point<std::chrono::steady_clock> start_time,
  6724. std::function<bool(Stream &strm)> callback) {
  6725. return detail::process_client_socket(
  6726. socket.sock, read_timeout_sec_, read_timeout_usec_, write_timeout_sec_,
  6727. write_timeout_usec_, max_timeout_msec_, start_time, std::move(callback));
  6728. }
  6729. bool ClientImpl::is_ssl() const { return false; }
  6730. Result ClientImpl::Get(const std::string &path,
  6731. DownloadProgress progress) {
  6732. return Get(path, Headers(), std::move(progress));
  6733. }
  6734. Result ClientImpl::Get(const std::string &path, const Params &params,
  6735. const Headers &headers,
  6736. DownloadProgress progress) {
  6737. if (params.empty()) { return Get(path, headers); }
  6738. std::string path_with_query = append_query_params(path, params);
  6739. return Get(path_with_query, headers, std::move(progress));
  6740. }
  6741. Result ClientImpl::Get(const std::string &path, const Headers &headers,
  6742. DownloadProgress progress) {
  6743. Request req;
  6744. req.method = "GET";
  6745. req.path = path;
  6746. req.headers = headers;
  6747. req.download_progress = std::move(progress);
  6748. if (max_timeout_msec_ > 0) {
  6749. req.start_time_ = std::chrono::steady_clock::now();
  6750. }
  6751. return send_(std::move(req));
  6752. }
  6753. Result ClientImpl::Get(const std::string &path,
  6754. ContentReceiver content_receiver,
  6755. DownloadProgress progress) {
  6756. return Get(path, Headers(), nullptr, std::move(content_receiver),
  6757. std::move(progress));
  6758. }
  6759. Result ClientImpl::Get(const std::string &path, const Headers &headers,
  6760. ContentReceiver content_receiver,
  6761. DownloadProgress progress) {
  6762. return Get(path, headers, nullptr, std::move(content_receiver),
  6763. std::move(progress));
  6764. }
  6765. Result ClientImpl::Get(const std::string &path,
  6766. ResponseHandler response_handler,
  6767. ContentReceiver content_receiver,
  6768. DownloadProgress progress) {
  6769. return Get(path, Headers(), std::move(response_handler),
  6770. std::move(content_receiver), std::move(progress));
  6771. }
  6772. Result ClientImpl::Get(const std::string &path, const Headers &headers,
  6773. ResponseHandler response_handler,
  6774. ContentReceiver content_receiver,
  6775. DownloadProgress progress) {
  6776. Request req;
  6777. req.method = "GET";
  6778. req.path = path;
  6779. req.headers = headers;
  6780. req.response_handler = std::move(response_handler);
  6781. req.content_receiver =
  6782. [content_receiver](const char *data, size_t data_length,
  6783. size_t /*offset*/, size_t /*total_length*/) {
  6784. return content_receiver(data, data_length);
  6785. };
  6786. req.download_progress = std::move(progress);
  6787. if (max_timeout_msec_ > 0) {
  6788. req.start_time_ = std::chrono::steady_clock::now();
  6789. }
  6790. return send_(std::move(req));
  6791. }
  6792. Result ClientImpl::Get(const std::string &path, const Params &params,
  6793. const Headers &headers,
  6794. ContentReceiver content_receiver,
  6795. DownloadProgress progress) {
  6796. return Get(path, params, headers, nullptr, std::move(content_receiver),
  6797. std::move(progress));
  6798. }
  6799. Result ClientImpl::Get(const std::string &path, const Params &params,
  6800. const Headers &headers,
  6801. ResponseHandler response_handler,
  6802. ContentReceiver content_receiver,
  6803. DownloadProgress progress) {
  6804. if (params.empty()) {
  6805. return Get(path, headers, std::move(response_handler),
  6806. std::move(content_receiver), std::move(progress));
  6807. }
  6808. std::string path_with_query = append_query_params(path, params);
  6809. return Get(path_with_query, headers, std::move(response_handler),
  6810. std::move(content_receiver), std::move(progress));
  6811. }
  6812. Result ClientImpl::Head(const std::string &path) {
  6813. return Head(path, Headers());
  6814. }
  6815. Result ClientImpl::Head(const std::string &path,
  6816. const Headers &headers) {
  6817. Request req;
  6818. req.method = "HEAD";
  6819. req.headers = headers;
  6820. req.path = path;
  6821. if (max_timeout_msec_ > 0) {
  6822. req.start_time_ = std::chrono::steady_clock::now();
  6823. }
  6824. return send_(std::move(req));
  6825. }
  6826. Result ClientImpl::Post(const std::string &path) {
  6827. return Post(path, std::string(), std::string());
  6828. }
  6829. Result ClientImpl::Post(const std::string &path,
  6830. const Headers &headers) {
  6831. return Post(path, headers, nullptr, 0, std::string());
  6832. }
  6833. Result ClientImpl::Post(const std::string &path, const char *body,
  6834. size_t content_length,
  6835. const std::string &content_type,
  6836. UploadProgress progress) {
  6837. return Post(path, Headers(), body, content_length, content_type, progress);
  6838. }
  6839. Result ClientImpl::Post(const std::string &path, const std::string &body,
  6840. const std::string &content_type,
  6841. UploadProgress progress) {
  6842. return Post(path, Headers(), body, content_type, progress);
  6843. }
  6844. Result ClientImpl::Post(const std::string &path, const Params &params) {
  6845. return Post(path, Headers(), params);
  6846. }
  6847. Result ClientImpl::Post(const std::string &path, size_t content_length,
  6848. ContentProvider content_provider,
  6849. const std::string &content_type,
  6850. UploadProgress progress) {
  6851. return Post(path, Headers(), content_length, std::move(content_provider),
  6852. content_type, progress);
  6853. }
  6854. Result ClientImpl::Post(const std::string &path, size_t content_length,
  6855. ContentProvider content_provider,
  6856. const std::string &content_type,
  6857. ContentReceiver content_receiver,
  6858. UploadProgress progress) {
  6859. return Post(path, Headers(), content_length, std::move(content_provider),
  6860. content_type, std::move(content_receiver), progress);
  6861. }
  6862. Result ClientImpl::Post(const std::string &path,
  6863. ContentProviderWithoutLength content_provider,
  6864. const std::string &content_type,
  6865. UploadProgress progress) {
  6866. return Post(path, Headers(), std::move(content_provider), content_type,
  6867. progress);
  6868. }
  6869. Result ClientImpl::Post(const std::string &path,
  6870. ContentProviderWithoutLength content_provider,
  6871. const std::string &content_type,
  6872. ContentReceiver content_receiver,
  6873. UploadProgress progress) {
  6874. return Post(path, Headers(), std::move(content_provider), content_type,
  6875. std::move(content_receiver), progress);
  6876. }
  6877. Result ClientImpl::Post(const std::string &path, const Headers &headers,
  6878. const Params &params) {
  6879. auto query = detail::params_to_query_str(params);
  6880. return Post(path, headers, query, "application/x-www-form-urlencoded");
  6881. }
  6882. Result ClientImpl::Post(const std::string &path,
  6883. const UploadFormDataItems &items,
  6884. UploadProgress progress) {
  6885. return Post(path, Headers(), items, progress);
  6886. }
  6887. Result ClientImpl::Post(const std::string &path, const Headers &headers,
  6888. const UploadFormDataItems &items,
  6889. UploadProgress progress) {
  6890. const auto &boundary = detail::make_multipart_data_boundary();
  6891. const auto &content_type =
  6892. detail::serialize_multipart_formdata_get_content_type(boundary);
  6893. const auto &body = detail::serialize_multipart_formdata(items, boundary);
  6894. return Post(path, headers, body, content_type, progress);
  6895. }
  6896. Result ClientImpl::Post(const std::string &path, const Headers &headers,
  6897. const UploadFormDataItems &items,
  6898. const std::string &boundary,
  6899. UploadProgress progress) {
  6900. if (!detail::is_multipart_boundary_chars_valid(boundary)) {
  6901. return Result{nullptr, Error::UnsupportedMultipartBoundaryChars};
  6902. }
  6903. const auto &content_type =
  6904. detail::serialize_multipart_formdata_get_content_type(boundary);
  6905. const auto &body = detail::serialize_multipart_formdata(items, boundary);
  6906. return Post(path, headers, body, content_type, progress);
  6907. }
  6908. Result ClientImpl::Post(const std::string &path, const Headers &headers,
  6909. const char *body, size_t content_length,
  6910. const std::string &content_type,
  6911. UploadProgress progress) {
  6912. return send_with_content_provider_and_receiver(
  6913. "POST", path, headers, body, content_length, nullptr, nullptr,
  6914. content_type, nullptr, progress);
  6915. }
  6916. Result ClientImpl::Post(const std::string &path, const Headers &headers,
  6917. const std::string &body,
  6918. const std::string &content_type,
  6919. UploadProgress progress) {
  6920. return send_with_content_provider_and_receiver(
  6921. "POST", path, headers, body.data(), body.size(), nullptr, nullptr,
  6922. content_type, nullptr, progress);
  6923. }
  6924. Result ClientImpl::Post(const std::string &path, const Headers &headers,
  6925. size_t content_length,
  6926. ContentProvider content_provider,
  6927. const std::string &content_type,
  6928. UploadProgress progress) {
  6929. return send_with_content_provider_and_receiver(
  6930. "POST", path, headers, nullptr, content_length,
  6931. std::move(content_provider), nullptr, content_type, nullptr, progress);
  6932. }
  6933. Result ClientImpl::Post(const std::string &path, const Headers &headers,
  6934. size_t content_length,
  6935. ContentProvider content_provider,
  6936. const std::string &content_type,
  6937. ContentReceiver content_receiver,
  6938. DownloadProgress progress) {
  6939. return send_with_content_provider_and_receiver(
  6940. "POST", path, headers, nullptr, content_length,
  6941. std::move(content_provider), nullptr, content_type,
  6942. std::move(content_receiver), std::move(progress));
  6943. }
  6944. Result ClientImpl::Post(const std::string &path, const Headers &headers,
  6945. ContentProviderWithoutLength content_provider,
  6946. const std::string &content_type,
  6947. UploadProgress progress) {
  6948. return send_with_content_provider_and_receiver(
  6949. "POST", path, headers, nullptr, 0, nullptr, std::move(content_provider),
  6950. content_type, nullptr, progress);
  6951. }
  6952. Result ClientImpl::Post(const std::string &path, const Headers &headers,
  6953. ContentProviderWithoutLength content_provider,
  6954. const std::string &content_type,
  6955. ContentReceiver content_receiver,
  6956. DownloadProgress progress) {
  6957. return send_with_content_provider_and_receiver(
  6958. "POST", path, headers, nullptr, 0, nullptr, std::move(content_provider),
  6959. content_type, std::move(content_receiver), std::move(progress));
  6960. }
  6961. Result ClientImpl::Post(const std::string &path, const Headers &headers,
  6962. const UploadFormDataItems &items,
  6963. const FormDataProviderItems &provider_items,
  6964. UploadProgress progress) {
  6965. const auto &boundary = detail::make_multipart_data_boundary();
  6966. const auto &content_type =
  6967. detail::serialize_multipart_formdata_get_content_type(boundary);
  6968. return send_with_content_provider_and_receiver(
  6969. "POST", path, headers, nullptr, 0, nullptr,
  6970. get_multipart_content_provider(boundary, items, provider_items),
  6971. content_type, nullptr, progress);
  6972. }
  6973. Result ClientImpl::Post(const std::string &path, const Headers &headers,
  6974. const std::string &body,
  6975. const std::string &content_type,
  6976. ContentReceiver content_receiver,
  6977. DownloadProgress progress) {
  6978. Request req;
  6979. req.method = "POST";
  6980. req.path = path;
  6981. req.headers = headers;
  6982. req.body = body;
  6983. req.content_receiver =
  6984. [content_receiver](const char *data, size_t data_length,
  6985. size_t /*offset*/, size_t /*total_length*/) {
  6986. return content_receiver(data, data_length);
  6987. };
  6988. req.download_progress = std::move(progress);
  6989. if (max_timeout_msec_ > 0) {
  6990. req.start_time_ = std::chrono::steady_clock::now();
  6991. }
  6992. if (!content_type.empty()) { req.set_header("Content-Type", content_type); }
  6993. return send_(std::move(req));
  6994. }
  6995. Result ClientImpl::Put(const std::string &path) {
  6996. return Put(path, std::string(), std::string());
  6997. }
  6998. Result ClientImpl::Put(const std::string &path, const Headers &headers) {
  6999. return Put(path, headers, nullptr, 0, std::string());
  7000. }
  7001. Result ClientImpl::Put(const std::string &path, const char *body,
  7002. size_t content_length,
  7003. const std::string &content_type,
  7004. UploadProgress progress) {
  7005. return Put(path, Headers(), body, content_length, content_type, progress);
  7006. }
  7007. Result ClientImpl::Put(const std::string &path, const std::string &body,
  7008. const std::string &content_type,
  7009. UploadProgress progress) {
  7010. return Put(path, Headers(), body, content_type, progress);
  7011. }
  7012. Result ClientImpl::Put(const std::string &path, const Params &params) {
  7013. return Put(path, Headers(), params);
  7014. }
  7015. Result ClientImpl::Put(const std::string &path, size_t content_length,
  7016. ContentProvider content_provider,
  7017. const std::string &content_type,
  7018. UploadProgress progress) {
  7019. return Put(path, Headers(), content_length, std::move(content_provider),
  7020. content_type, progress);
  7021. }
  7022. Result ClientImpl::Put(const std::string &path, size_t content_length,
  7023. ContentProvider content_provider,
  7024. const std::string &content_type,
  7025. ContentReceiver content_receiver,
  7026. UploadProgress progress) {
  7027. return Put(path, Headers(), content_length, std::move(content_provider),
  7028. content_type, std::move(content_receiver), progress);
  7029. }
  7030. Result ClientImpl::Put(const std::string &path,
  7031. ContentProviderWithoutLength content_provider,
  7032. const std::string &content_type,
  7033. UploadProgress progress) {
  7034. return Put(path, Headers(), std::move(content_provider), content_type,
  7035. progress);
  7036. }
  7037. Result ClientImpl::Put(const std::string &path,
  7038. ContentProviderWithoutLength content_provider,
  7039. const std::string &content_type,
  7040. ContentReceiver content_receiver,
  7041. UploadProgress progress) {
  7042. return Put(path, Headers(), std::move(content_provider), content_type,
  7043. std::move(content_receiver), progress);
  7044. }
  7045. Result ClientImpl::Put(const std::string &path, const Headers &headers,
  7046. const Params &params) {
  7047. auto query = detail::params_to_query_str(params);
  7048. return Put(path, headers, query, "application/x-www-form-urlencoded");
  7049. }
  7050. Result ClientImpl::Put(const std::string &path,
  7051. const UploadFormDataItems &items,
  7052. UploadProgress progress) {
  7053. return Put(path, Headers(), items, progress);
  7054. }
  7055. Result ClientImpl::Put(const std::string &path, const Headers &headers,
  7056. const UploadFormDataItems &items,
  7057. UploadProgress progress) {
  7058. const auto &boundary = detail::make_multipart_data_boundary();
  7059. const auto &content_type =
  7060. detail::serialize_multipart_formdata_get_content_type(boundary);
  7061. const auto &body = detail::serialize_multipart_formdata(items, boundary);
  7062. return Put(path, headers, body, content_type, progress);
  7063. }
  7064. Result ClientImpl::Put(const std::string &path, const Headers &headers,
  7065. const UploadFormDataItems &items,
  7066. const std::string &boundary,
  7067. UploadProgress progress) {
  7068. if (!detail::is_multipart_boundary_chars_valid(boundary)) {
  7069. return Result{nullptr, Error::UnsupportedMultipartBoundaryChars};
  7070. }
  7071. const auto &content_type =
  7072. detail::serialize_multipart_formdata_get_content_type(boundary);
  7073. const auto &body = detail::serialize_multipart_formdata(items, boundary);
  7074. return Put(path, headers, body, content_type, progress);
  7075. }
  7076. Result ClientImpl::Put(const std::string &path, const Headers &headers,
  7077. const char *body, size_t content_length,
  7078. const std::string &content_type,
  7079. UploadProgress progress) {
  7080. return send_with_content_provider_and_receiver(
  7081. "PUT", path, headers, body, content_length, nullptr, nullptr,
  7082. content_type, nullptr, progress);
  7083. }
  7084. Result ClientImpl::Put(const std::string &path, const Headers &headers,
  7085. const std::string &body,
  7086. const std::string &content_type,
  7087. UploadProgress progress) {
  7088. return send_with_content_provider_and_receiver(
  7089. "PUT", path, headers, body.data(), body.size(), nullptr, nullptr,
  7090. content_type, nullptr, progress);
  7091. }
  7092. Result ClientImpl::Put(const std::string &path, const Headers &headers,
  7093. size_t content_length,
  7094. ContentProvider content_provider,
  7095. const std::string &content_type,
  7096. UploadProgress progress) {
  7097. return send_with_content_provider_and_receiver(
  7098. "PUT", path, headers, nullptr, content_length,
  7099. std::move(content_provider), nullptr, content_type, nullptr, progress);
  7100. }
  7101. Result ClientImpl::Put(const std::string &path, const Headers &headers,
  7102. size_t content_length,
  7103. ContentProvider content_provider,
  7104. const std::string &content_type,
  7105. ContentReceiver content_receiver,
  7106. UploadProgress progress) {
  7107. return send_with_content_provider_and_receiver(
  7108. "PUT", path, headers, nullptr, content_length,
  7109. std::move(content_provider), nullptr, content_type,
  7110. std::move(content_receiver), progress);
  7111. }
  7112. Result ClientImpl::Put(const std::string &path, const Headers &headers,
  7113. ContentProviderWithoutLength content_provider,
  7114. const std::string &content_type,
  7115. UploadProgress progress) {
  7116. return send_with_content_provider_and_receiver(
  7117. "PUT", path, headers, nullptr, 0, nullptr, std::move(content_provider),
  7118. content_type, nullptr, progress);
  7119. }
  7120. Result ClientImpl::Put(const std::string &path, const Headers &headers,
  7121. ContentProviderWithoutLength content_provider,
  7122. const std::string &content_type,
  7123. ContentReceiver content_receiver,
  7124. UploadProgress progress) {
  7125. return send_with_content_provider_and_receiver(
  7126. "PUT", path, headers, nullptr, 0, nullptr, std::move(content_provider),
  7127. content_type, std::move(content_receiver), progress);
  7128. }
  7129. Result ClientImpl::Put(const std::string &path, const Headers &headers,
  7130. const UploadFormDataItems &items,
  7131. const FormDataProviderItems &provider_items,
  7132. UploadProgress progress) {
  7133. const auto &boundary = detail::make_multipart_data_boundary();
  7134. const auto &content_type =
  7135. detail::serialize_multipart_formdata_get_content_type(boundary);
  7136. return send_with_content_provider_and_receiver(
  7137. "PUT", path, headers, nullptr, 0, nullptr,
  7138. get_multipart_content_provider(boundary, items, provider_items),
  7139. content_type, nullptr, progress);
  7140. }
  7141. Result ClientImpl::Put(const std::string &path, const Headers &headers,
  7142. const std::string &body,
  7143. const std::string &content_type,
  7144. ContentReceiver content_receiver,
  7145. DownloadProgress progress) {
  7146. Request req;
  7147. req.method = "PUT";
  7148. req.path = path;
  7149. req.headers = headers;
  7150. req.body = body;
  7151. req.content_receiver =
  7152. [content_receiver](const char *data, size_t data_length,
  7153. size_t /*offset*/, size_t /*total_length*/) {
  7154. return content_receiver(data, data_length);
  7155. };
  7156. req.download_progress = std::move(progress);
  7157. if (max_timeout_msec_ > 0) {
  7158. req.start_time_ = std::chrono::steady_clock::now();
  7159. }
  7160. if (!content_type.empty()) { req.set_header("Content-Type", content_type); }
  7161. return send_(std::move(req));
  7162. }
  7163. Result ClientImpl::Patch(const std::string &path) {
  7164. return Patch(path, std::string(), std::string());
  7165. }
  7166. Result ClientImpl::Patch(const std::string &path, const Headers &headers,
  7167. UploadProgress progress) {
  7168. return Patch(path, headers, nullptr, 0, std::string(), progress);
  7169. }
  7170. Result ClientImpl::Patch(const std::string &path, const char *body,
  7171. size_t content_length,
  7172. const std::string &content_type,
  7173. UploadProgress progress) {
  7174. return Patch(path, Headers(), body, content_length, content_type, progress);
  7175. }
  7176. Result ClientImpl::Patch(const std::string &path,
  7177. const std::string &body,
  7178. const std::string &content_type,
  7179. UploadProgress progress) {
  7180. return Patch(path, Headers(), body, content_type, progress);
  7181. }
  7182. Result ClientImpl::Patch(const std::string &path, const Params &params) {
  7183. return Patch(path, Headers(), params);
  7184. }
  7185. Result ClientImpl::Patch(const std::string &path, size_t content_length,
  7186. ContentProvider content_provider,
  7187. const std::string &content_type,
  7188. UploadProgress progress) {
  7189. return Patch(path, Headers(), content_length, std::move(content_provider),
  7190. content_type, progress);
  7191. }
  7192. Result ClientImpl::Patch(const std::string &path, size_t content_length,
  7193. ContentProvider content_provider,
  7194. const std::string &content_type,
  7195. ContentReceiver content_receiver,
  7196. UploadProgress progress) {
  7197. return Patch(path, Headers(), content_length, std::move(content_provider),
  7198. content_type, std::move(content_receiver), progress);
  7199. }
  7200. Result ClientImpl::Patch(const std::string &path,
  7201. ContentProviderWithoutLength content_provider,
  7202. const std::string &content_type,
  7203. UploadProgress progress) {
  7204. return Patch(path, Headers(), std::move(content_provider), content_type,
  7205. progress);
  7206. }
  7207. Result ClientImpl::Patch(const std::string &path,
  7208. ContentProviderWithoutLength content_provider,
  7209. const std::string &content_type,
  7210. ContentReceiver content_receiver,
  7211. UploadProgress progress) {
  7212. return Patch(path, Headers(), std::move(content_provider), content_type,
  7213. std::move(content_receiver), progress);
  7214. }
  7215. Result ClientImpl::Patch(const std::string &path, const Headers &headers,
  7216. const Params &params) {
  7217. auto query = detail::params_to_query_str(params);
  7218. return Patch(path, headers, query, "application/x-www-form-urlencoded");
  7219. }
  7220. Result ClientImpl::Patch(const std::string &path,
  7221. const UploadFormDataItems &items,
  7222. UploadProgress progress) {
  7223. return Patch(path, Headers(), items, progress);
  7224. }
  7225. Result ClientImpl::Patch(const std::string &path, const Headers &headers,
  7226. const UploadFormDataItems &items,
  7227. UploadProgress progress) {
  7228. const auto &boundary = detail::make_multipart_data_boundary();
  7229. const auto &content_type =
  7230. detail::serialize_multipart_formdata_get_content_type(boundary);
  7231. const auto &body = detail::serialize_multipart_formdata(items, boundary);
  7232. return Patch(path, headers, body, content_type, progress);
  7233. }
  7234. Result ClientImpl::Patch(const std::string &path, const Headers &headers,
  7235. const UploadFormDataItems &items,
  7236. const std::string &boundary,
  7237. UploadProgress progress) {
  7238. if (!detail::is_multipart_boundary_chars_valid(boundary)) {
  7239. return Result{nullptr, Error::UnsupportedMultipartBoundaryChars};
  7240. }
  7241. const auto &content_type =
  7242. detail::serialize_multipart_formdata_get_content_type(boundary);
  7243. const auto &body = detail::serialize_multipart_formdata(items, boundary);
  7244. return Patch(path, headers, body, content_type, progress);
  7245. }
  7246. Result ClientImpl::Patch(const std::string &path, const Headers &headers,
  7247. const char *body, size_t content_length,
  7248. const std::string &content_type,
  7249. UploadProgress progress) {
  7250. return send_with_content_provider_and_receiver(
  7251. "PATCH", path, headers, body, content_length, nullptr, nullptr,
  7252. content_type, nullptr, progress);
  7253. }
  7254. Result ClientImpl::Patch(const std::string &path, const Headers &headers,
  7255. const std::string &body,
  7256. const std::string &content_type,
  7257. UploadProgress progress) {
  7258. return send_with_content_provider_and_receiver(
  7259. "PATCH", path, headers, body.data(), body.size(), nullptr, nullptr,
  7260. content_type, nullptr, progress);
  7261. }
  7262. Result ClientImpl::Patch(const std::string &path, const Headers &headers,
  7263. size_t content_length,
  7264. ContentProvider content_provider,
  7265. const std::string &content_type,
  7266. UploadProgress progress) {
  7267. return send_with_content_provider_and_receiver(
  7268. "PATCH", path, headers, nullptr, content_length,
  7269. std::move(content_provider), nullptr, content_type, nullptr, progress);
  7270. }
  7271. Result ClientImpl::Patch(const std::string &path, const Headers &headers,
  7272. size_t content_length,
  7273. ContentProvider content_provider,
  7274. const std::string &content_type,
  7275. ContentReceiver content_receiver,
  7276. UploadProgress progress) {
  7277. return send_with_content_provider_and_receiver(
  7278. "PATCH", path, headers, nullptr, content_length,
  7279. std::move(content_provider), nullptr, content_type,
  7280. std::move(content_receiver), progress);
  7281. }
  7282. Result ClientImpl::Patch(const std::string &path, const Headers &headers,
  7283. ContentProviderWithoutLength content_provider,
  7284. const std::string &content_type,
  7285. UploadProgress progress) {
  7286. return send_with_content_provider_and_receiver(
  7287. "PATCH", path, headers, nullptr, 0, nullptr, std::move(content_provider),
  7288. content_type, nullptr, progress);
  7289. }
  7290. Result ClientImpl::Patch(const std::string &path, const Headers &headers,
  7291. ContentProviderWithoutLength content_provider,
  7292. const std::string &content_type,
  7293. ContentReceiver content_receiver,
  7294. UploadProgress progress) {
  7295. return send_with_content_provider_and_receiver(
  7296. "PATCH", path, headers, nullptr, 0, nullptr, std::move(content_provider),
  7297. content_type, std::move(content_receiver), progress);
  7298. }
  7299. Result ClientImpl::Patch(const std::string &path, const Headers &headers,
  7300. const UploadFormDataItems &items,
  7301. const FormDataProviderItems &provider_items,
  7302. UploadProgress progress) {
  7303. const auto &boundary = detail::make_multipart_data_boundary();
  7304. const auto &content_type =
  7305. detail::serialize_multipart_formdata_get_content_type(boundary);
  7306. return send_with_content_provider_and_receiver(
  7307. "PATCH", path, headers, nullptr, 0, nullptr,
  7308. get_multipart_content_provider(boundary, items, provider_items),
  7309. content_type, nullptr, progress);
  7310. }
  7311. Result ClientImpl::Patch(const std::string &path, const Headers &headers,
  7312. const std::string &body,
  7313. const std::string &content_type,
  7314. ContentReceiver content_receiver,
  7315. DownloadProgress progress) {
  7316. Request req;
  7317. req.method = "PATCH";
  7318. req.path = path;
  7319. req.headers = headers;
  7320. req.body = body;
  7321. req.content_receiver =
  7322. [content_receiver](const char *data, size_t data_length,
  7323. size_t /*offset*/, size_t /*total_length*/) {
  7324. return content_receiver(data, data_length);
  7325. };
  7326. req.download_progress = std::move(progress);
  7327. if (max_timeout_msec_ > 0) {
  7328. req.start_time_ = std::chrono::steady_clock::now();
  7329. }
  7330. if (!content_type.empty()) { req.set_header("Content-Type", content_type); }
  7331. return send_(std::move(req));
  7332. }
  7333. Result ClientImpl::Delete(const std::string &path,
  7334. DownloadProgress progress) {
  7335. return Delete(path, Headers(), std::string(), std::string(), progress);
  7336. }
  7337. Result ClientImpl::Delete(const std::string &path,
  7338. const Headers &headers,
  7339. DownloadProgress progress) {
  7340. return Delete(path, headers, std::string(), std::string(), progress);
  7341. }
  7342. Result ClientImpl::Delete(const std::string &path, const char *body,
  7343. size_t content_length,
  7344. const std::string &content_type,
  7345. DownloadProgress progress) {
  7346. return Delete(path, Headers(), body, content_length, content_type, progress);
  7347. }
  7348. Result ClientImpl::Delete(const std::string &path,
  7349. const std::string &body,
  7350. const std::string &content_type,
  7351. DownloadProgress progress) {
  7352. return Delete(path, Headers(), body.data(), body.size(), content_type,
  7353. progress);
  7354. }
  7355. Result ClientImpl::Delete(const std::string &path,
  7356. const Headers &headers,
  7357. const std::string &body,
  7358. const std::string &content_type,
  7359. DownloadProgress progress) {
  7360. return Delete(path, headers, body.data(), body.size(), content_type,
  7361. progress);
  7362. }
  7363. Result ClientImpl::Delete(const std::string &path, const Params &params,
  7364. DownloadProgress progress) {
  7365. return Delete(path, Headers(), params, progress);
  7366. }
  7367. Result ClientImpl::Delete(const std::string &path,
  7368. const Headers &headers, const Params &params,
  7369. DownloadProgress progress) {
  7370. auto query = detail::params_to_query_str(params);
  7371. return Delete(path, headers, query, "application/x-www-form-urlencoded",
  7372. progress);
  7373. }
  7374. Result ClientImpl::Delete(const std::string &path,
  7375. const Headers &headers, const char *body,
  7376. size_t content_length,
  7377. const std::string &content_type,
  7378. DownloadProgress progress) {
  7379. Request req;
  7380. req.method = "DELETE";
  7381. req.headers = headers;
  7382. req.path = path;
  7383. req.download_progress = std::move(progress);
  7384. if (max_timeout_msec_ > 0) {
  7385. req.start_time_ = std::chrono::steady_clock::now();
  7386. }
  7387. if (!content_type.empty()) { req.set_header("Content-Type", content_type); }
  7388. req.body.assign(body, content_length);
  7389. return send_(std::move(req));
  7390. }
  7391. Result ClientImpl::Options(const std::string &path) {
  7392. return Options(path, Headers());
  7393. }
  7394. Result ClientImpl::Options(const std::string &path,
  7395. const Headers &headers) {
  7396. Request req;
  7397. req.method = "OPTIONS";
  7398. req.headers = headers;
  7399. req.path = path;
  7400. if (max_timeout_msec_ > 0) {
  7401. req.start_time_ = std::chrono::steady_clock::now();
  7402. }
  7403. return send_(std::move(req));
  7404. }
  7405. void ClientImpl::stop() {
  7406. std::lock_guard<std::mutex> guard(socket_mutex_);
  7407. // If there is anything ongoing right now, the ONLY thread-safe thing we can
  7408. // do is to shutdown_socket, so that threads using this socket suddenly
  7409. // discover they can't read/write any more and error out. Everything else
  7410. // (closing the socket, shutting ssl down) is unsafe because these actions
  7411. // are not thread-safe.
  7412. if (socket_requests_in_flight_ > 0) {
  7413. shutdown_socket(socket_);
  7414. // Aside from that, we set a flag for the socket to be closed when we're
  7415. // done.
  7416. socket_should_be_closed_when_request_is_done_ = true;
  7417. return;
  7418. }
  7419. // Otherwise, still holding the mutex, we can shut everything down ourselves
  7420. shutdown_ssl(socket_, true);
  7421. shutdown_socket(socket_);
  7422. close_socket(socket_);
  7423. }
  7424. std::string ClientImpl::host() const { return host_; }
  7425. int ClientImpl::port() const { return port_; }
  7426. size_t ClientImpl::is_socket_open() const {
  7427. std::lock_guard<std::mutex> guard(socket_mutex_);
  7428. return socket_.is_open();
  7429. }
  7430. socket_t ClientImpl::socket() const { return socket_.sock; }
  7431. void ClientImpl::set_connection_timeout(time_t sec, time_t usec) {
  7432. connection_timeout_sec_ = sec;
  7433. connection_timeout_usec_ = usec;
  7434. }
  7435. void ClientImpl::set_read_timeout(time_t sec, time_t usec) {
  7436. read_timeout_sec_ = sec;
  7437. read_timeout_usec_ = usec;
  7438. }
  7439. void ClientImpl::set_write_timeout(time_t sec, time_t usec) {
  7440. write_timeout_sec_ = sec;
  7441. write_timeout_usec_ = usec;
  7442. }
  7443. void ClientImpl::set_max_timeout(time_t msec) {
  7444. max_timeout_msec_ = msec;
  7445. }
  7446. void ClientImpl::set_basic_auth(const std::string &username,
  7447. const std::string &password) {
  7448. basic_auth_username_ = username;
  7449. basic_auth_password_ = password;
  7450. }
  7451. void ClientImpl::set_bearer_token_auth(const std::string &token) {
  7452. bearer_token_auth_token_ = token;
  7453. }
  7454. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  7455. void ClientImpl::set_digest_auth(const std::string &username,
  7456. const std::string &password) {
  7457. digest_auth_username_ = username;
  7458. digest_auth_password_ = password;
  7459. }
  7460. #endif
  7461. void ClientImpl::set_keep_alive(bool on) { keep_alive_ = on; }
  7462. void ClientImpl::set_follow_location(bool on) { follow_location_ = on; }
  7463. void ClientImpl::set_path_encode(bool on) { path_encode_ = on; }
  7464. void
  7465. ClientImpl::set_hostname_addr_map(std::map<std::string, std::string> addr_map) {
  7466. addr_map_ = std::move(addr_map);
  7467. }
  7468. void ClientImpl::set_default_headers(Headers headers) {
  7469. default_headers_ = std::move(headers);
  7470. }
  7471. void ClientImpl::set_header_writer(
  7472. std::function<ssize_t(Stream &, Headers &)> const &writer) {
  7473. header_writer_ = writer;
  7474. }
  7475. void ClientImpl::set_address_family(int family) {
  7476. address_family_ = family;
  7477. }
  7478. void ClientImpl::set_tcp_nodelay(bool on) { tcp_nodelay_ = on; }
  7479. void ClientImpl::set_ipv6_v6only(bool on) { ipv6_v6only_ = on; }
  7480. void ClientImpl::set_socket_options(SocketOptions socket_options) {
  7481. socket_options_ = std::move(socket_options);
  7482. }
  7483. void ClientImpl::set_compress(bool on) { compress_ = on; }
  7484. void ClientImpl::set_decompress(bool on) { decompress_ = on; }
  7485. void ClientImpl::set_interface(const std::string &intf) {
  7486. interface_ = intf;
  7487. }
  7488. void ClientImpl::set_proxy(const std::string &host, int port) {
  7489. proxy_host_ = host;
  7490. proxy_port_ = port;
  7491. }
  7492. void ClientImpl::set_proxy_basic_auth(const std::string &username,
  7493. const std::string &password) {
  7494. proxy_basic_auth_username_ = username;
  7495. proxy_basic_auth_password_ = password;
  7496. }
  7497. void ClientImpl::set_proxy_bearer_token_auth(const std::string &token) {
  7498. proxy_bearer_token_auth_token_ = token;
  7499. }
  7500. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  7501. void ClientImpl::set_proxy_digest_auth(const std::string &username,
  7502. const std::string &password) {
  7503. proxy_digest_auth_username_ = username;
  7504. proxy_digest_auth_password_ = password;
  7505. }
  7506. void ClientImpl::set_ca_cert_path(const std::string &ca_cert_file_path,
  7507. const std::string &ca_cert_dir_path) {
  7508. ca_cert_file_path_ = ca_cert_file_path;
  7509. ca_cert_dir_path_ = ca_cert_dir_path;
  7510. }
  7511. void ClientImpl::set_ca_cert_store(X509_STORE *ca_cert_store) {
  7512. if (ca_cert_store && ca_cert_store != ca_cert_store_) {
  7513. ca_cert_store_ = ca_cert_store;
  7514. }
  7515. }
  7516. X509_STORE *ClientImpl::create_ca_cert_store(const char *ca_cert,
  7517. std::size_t size) const {
  7518. auto mem = BIO_new_mem_buf(ca_cert, static_cast<int>(size));
  7519. auto se = detail::scope_exit([&] { BIO_free_all(mem); });
  7520. if (!mem) { return nullptr; }
  7521. auto inf = PEM_X509_INFO_read_bio(mem, nullptr, nullptr, nullptr);
  7522. if (!inf) { return nullptr; }
  7523. auto cts = X509_STORE_new();
  7524. if (cts) {
  7525. for (auto i = 0; i < static_cast<int>(sk_X509_INFO_num(inf)); i++) {
  7526. auto itmp = sk_X509_INFO_value(inf, i);
  7527. if (!itmp) { continue; }
  7528. if (itmp->x509) { X509_STORE_add_cert(cts, itmp->x509); }
  7529. if (itmp->crl) { X509_STORE_add_crl(cts, itmp->crl); }
  7530. }
  7531. }
  7532. sk_X509_INFO_pop_free(inf, X509_INFO_free);
  7533. return cts;
  7534. }
  7535. void ClientImpl::enable_server_certificate_verification(bool enabled) {
  7536. server_certificate_verification_ = enabled;
  7537. }
  7538. void ClientImpl::enable_server_hostname_verification(bool enabled) {
  7539. server_hostname_verification_ = enabled;
  7540. }
  7541. void ClientImpl::set_server_certificate_verifier(
  7542. std::function<SSLVerifierResponse(SSL *ssl)> verifier) {
  7543. server_certificate_verifier_ = verifier;
  7544. }
  7545. #endif
  7546. void ClientImpl::set_logger(Logger logger) {
  7547. logger_ = std::move(logger);
  7548. }
  7549. void ClientImpl::set_error_logger(ErrorLogger error_logger) {
  7550. error_logger_ = std::move(error_logger);
  7551. }
  7552. /*
  7553. * SSL Implementation
  7554. */
  7555. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  7556. namespace detail {
  7557. bool is_ip_address(const std::string &host) {
  7558. struct in_addr addr4;
  7559. struct in6_addr addr6;
  7560. return inet_pton(AF_INET, host.c_str(), &addr4) == 1 ||
  7561. inet_pton(AF_INET6, host.c_str(), &addr6) == 1;
  7562. }
  7563. template <typename U, typename V>
  7564. SSL *ssl_new(socket_t sock, SSL_CTX *ctx, std::mutex &ctx_mutex,
  7565. U SSL_connect_or_accept, V setup) {
  7566. SSL *ssl = nullptr;
  7567. {
  7568. std::lock_guard<std::mutex> guard(ctx_mutex);
  7569. ssl = SSL_new(ctx);
  7570. }
  7571. if (ssl) {
  7572. set_nonblocking(sock, true);
  7573. auto bio = BIO_new_socket(static_cast<int>(sock), BIO_NOCLOSE);
  7574. BIO_set_nbio(bio, 1);
  7575. SSL_set_bio(ssl, bio, bio);
  7576. if (!setup(ssl) || SSL_connect_or_accept(ssl) != 1) {
  7577. SSL_shutdown(ssl);
  7578. {
  7579. std::lock_guard<std::mutex> guard(ctx_mutex);
  7580. SSL_free(ssl);
  7581. }
  7582. set_nonblocking(sock, false);
  7583. return nullptr;
  7584. }
  7585. BIO_set_nbio(bio, 0);
  7586. set_nonblocking(sock, false);
  7587. }
  7588. return ssl;
  7589. }
  7590. void ssl_delete(std::mutex &ctx_mutex, SSL *ssl, socket_t sock,
  7591. bool shutdown_gracefully) {
  7592. // sometimes we may want to skip this to try to avoid SIGPIPE if we know
  7593. // the remote has closed the network connection
  7594. // Note that it is not always possible to avoid SIGPIPE, this is merely a
  7595. // best-efforts.
  7596. if (shutdown_gracefully) {
  7597. (void)(sock);
  7598. // SSL_shutdown() returns 0 on first call (indicating close_notify alert
  7599. // sent) and 1 on subsequent call (indicating close_notify alert received)
  7600. if (SSL_shutdown(ssl) == 0) {
  7601. // Expected to return 1, but even if it doesn't, we free ssl
  7602. SSL_shutdown(ssl);
  7603. }
  7604. }
  7605. std::lock_guard<std::mutex> guard(ctx_mutex);
  7606. SSL_free(ssl);
  7607. }
  7608. template <typename U>
  7609. bool ssl_connect_or_accept_nonblocking(socket_t sock, SSL *ssl,
  7610. U ssl_connect_or_accept,
  7611. time_t timeout_sec, time_t timeout_usec,
  7612. int *ssl_error) {
  7613. auto res = 0;
  7614. while ((res = ssl_connect_or_accept(ssl)) != 1) {
  7615. auto err = SSL_get_error(ssl, res);
  7616. switch (err) {
  7617. case SSL_ERROR_WANT_READ:
  7618. if (select_read(sock, timeout_sec, timeout_usec) > 0) { continue; }
  7619. break;
  7620. case SSL_ERROR_WANT_WRITE:
  7621. if (select_write(sock, timeout_sec, timeout_usec) > 0) { continue; }
  7622. break;
  7623. default: break;
  7624. }
  7625. if (ssl_error) { *ssl_error = err; }
  7626. return false;
  7627. }
  7628. return true;
  7629. }
  7630. template <typename T>
  7631. bool process_server_socket_ssl(
  7632. const std::atomic<socket_t> &svr_sock, SSL *ssl, socket_t sock,
  7633. size_t keep_alive_max_count, time_t keep_alive_timeout_sec,
  7634. time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec,
  7635. time_t write_timeout_usec, T callback) {
  7636. return process_server_socket_core(
  7637. svr_sock, sock, keep_alive_max_count, keep_alive_timeout_sec,
  7638. [&](bool close_connection, bool &connection_closed) {
  7639. SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec,
  7640. write_timeout_sec, write_timeout_usec);
  7641. return callback(strm, close_connection, connection_closed);
  7642. });
  7643. }
  7644. template <typename T>
  7645. bool process_client_socket_ssl(
  7646. SSL *ssl, socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec,
  7647. time_t write_timeout_sec, time_t write_timeout_usec,
  7648. time_t max_timeout_msec,
  7649. std::chrono::time_point<std::chrono::steady_clock> start_time, T callback) {
  7650. SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec,
  7651. write_timeout_sec, write_timeout_usec, max_timeout_msec,
  7652. start_time);
  7653. return callback(strm);
  7654. }
  7655. // SSL socket stream implementation
  7656. SSLSocketStream::SSLSocketStream(
  7657. socket_t sock, SSL *ssl, time_t read_timeout_sec, time_t read_timeout_usec,
  7658. time_t write_timeout_sec, time_t write_timeout_usec,
  7659. time_t max_timeout_msec,
  7660. std::chrono::time_point<std::chrono::steady_clock> start_time)
  7661. : sock_(sock), ssl_(ssl), read_timeout_sec_(read_timeout_sec),
  7662. read_timeout_usec_(read_timeout_usec),
  7663. write_timeout_sec_(write_timeout_sec),
  7664. write_timeout_usec_(write_timeout_usec),
  7665. max_timeout_msec_(max_timeout_msec), start_time_(start_time) {
  7666. SSL_clear_mode(ssl, SSL_MODE_AUTO_RETRY);
  7667. }
  7668. SSLSocketStream::~SSLSocketStream() = default;
  7669. bool SSLSocketStream::is_readable() const {
  7670. return SSL_pending(ssl_) > 0;
  7671. }
  7672. bool SSLSocketStream::wait_readable() const {
  7673. if (max_timeout_msec_ <= 0) {
  7674. return select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0;
  7675. }
  7676. time_t read_timeout_sec;
  7677. time_t read_timeout_usec;
  7678. calc_actual_timeout(max_timeout_msec_, duration(), read_timeout_sec_,
  7679. read_timeout_usec_, read_timeout_sec, read_timeout_usec);
  7680. return select_read(sock_, read_timeout_sec, read_timeout_usec) > 0;
  7681. }
  7682. bool SSLSocketStream::wait_writable() const {
  7683. return select_write(sock_, write_timeout_sec_, write_timeout_usec_) > 0 &&
  7684. is_socket_alive(sock_) && !is_ssl_peer_could_be_closed(ssl_, sock_);
  7685. }
  7686. ssize_t SSLSocketStream::read(char *ptr, size_t size) {
  7687. if (SSL_pending(ssl_) > 0) {
  7688. auto ret = SSL_read(ssl_, ptr, static_cast<int>(size));
  7689. if (ret == 0) { error_ = Error::ConnectionClosed; }
  7690. return ret;
  7691. } else if (wait_readable()) {
  7692. auto ret = SSL_read(ssl_, ptr, static_cast<int>(size));
  7693. if (ret < 0) {
  7694. auto err = SSL_get_error(ssl_, ret);
  7695. auto n = 1000;
  7696. #ifdef _WIN32
  7697. while (--n >= 0 && (err == SSL_ERROR_WANT_READ ||
  7698. (err == SSL_ERROR_SYSCALL &&
  7699. WSAGetLastError() == WSAETIMEDOUT))) {
  7700. #else
  7701. while (--n >= 0 && err == SSL_ERROR_WANT_READ) {
  7702. #endif
  7703. if (SSL_pending(ssl_) > 0) {
  7704. return SSL_read(ssl_, ptr, static_cast<int>(size));
  7705. } else if (wait_readable()) {
  7706. std::this_thread::sleep_for(std::chrono::microseconds{10});
  7707. ret = SSL_read(ssl_, ptr, static_cast<int>(size));
  7708. if (ret >= 0) { return ret; }
  7709. err = SSL_get_error(ssl_, ret);
  7710. } else {
  7711. break;
  7712. }
  7713. }
  7714. assert(ret < 0);
  7715. } else if (ret == 0) {
  7716. error_ = Error::ConnectionClosed;
  7717. }
  7718. return ret;
  7719. } else {
  7720. error_ = Error::Timeout;
  7721. return -1;
  7722. }
  7723. }
  7724. ssize_t SSLSocketStream::write(const char *ptr, size_t size) {
  7725. if (wait_writable()) {
  7726. auto handle_size = static_cast<int>(
  7727. std::min<size_t>(size, (std::numeric_limits<int>::max)()));
  7728. auto ret = SSL_write(ssl_, ptr, static_cast<int>(handle_size));
  7729. if (ret < 0) {
  7730. auto err = SSL_get_error(ssl_, ret);
  7731. auto n = 1000;
  7732. #ifdef _WIN32
  7733. while (--n >= 0 && (err == SSL_ERROR_WANT_WRITE ||
  7734. (err == SSL_ERROR_SYSCALL &&
  7735. WSAGetLastError() == WSAETIMEDOUT))) {
  7736. #else
  7737. while (--n >= 0 && err == SSL_ERROR_WANT_WRITE) {
  7738. #endif
  7739. if (wait_writable()) {
  7740. std::this_thread::sleep_for(std::chrono::microseconds{10});
  7741. ret = SSL_write(ssl_, ptr, static_cast<int>(handle_size));
  7742. if (ret >= 0) { return ret; }
  7743. err = SSL_get_error(ssl_, ret);
  7744. } else {
  7745. break;
  7746. }
  7747. }
  7748. assert(ret < 0);
  7749. }
  7750. return ret;
  7751. }
  7752. return -1;
  7753. }
  7754. void SSLSocketStream::get_remote_ip_and_port(std::string &ip,
  7755. int &port) const {
  7756. detail::get_remote_ip_and_port(sock_, ip, port);
  7757. }
  7758. void SSLSocketStream::get_local_ip_and_port(std::string &ip,
  7759. int &port) const {
  7760. detail::get_local_ip_and_port(sock_, ip, port);
  7761. }
  7762. socket_t SSLSocketStream::socket() const { return sock_; }
  7763. time_t SSLSocketStream::duration() const {
  7764. return std::chrono::duration_cast<std::chrono::milliseconds>(
  7765. std::chrono::steady_clock::now() - start_time_)
  7766. .count();
  7767. }
  7768. } // namespace detail
  7769. // SSL HTTP server implementation
  7770. SSLServer::SSLServer(const char *cert_path, const char *private_key_path,
  7771. const char *client_ca_cert_file_path,
  7772. const char *client_ca_cert_dir_path,
  7773. const char *private_key_password) {
  7774. ctx_ = SSL_CTX_new(TLS_server_method());
  7775. if (ctx_) {
  7776. SSL_CTX_set_options(ctx_,
  7777. SSL_OP_NO_COMPRESSION |
  7778. SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION);
  7779. SSL_CTX_set_min_proto_version(ctx_, TLS1_2_VERSION);
  7780. if (private_key_password != nullptr && (private_key_password[0] != '\0')) {
  7781. SSL_CTX_set_default_passwd_cb_userdata(
  7782. ctx_,
  7783. reinterpret_cast<void *>(const_cast<char *>(private_key_password)));
  7784. }
  7785. if (SSL_CTX_use_certificate_chain_file(ctx_, cert_path) != 1 ||
  7786. SSL_CTX_use_PrivateKey_file(ctx_, private_key_path, SSL_FILETYPE_PEM) !=
  7787. 1 ||
  7788. SSL_CTX_check_private_key(ctx_) != 1) {
  7789. last_ssl_error_ = static_cast<int>(ERR_get_error());
  7790. SSL_CTX_free(ctx_);
  7791. ctx_ = nullptr;
  7792. } else if (client_ca_cert_file_path || client_ca_cert_dir_path) {
  7793. SSL_CTX_load_verify_locations(ctx_, client_ca_cert_file_path,
  7794. client_ca_cert_dir_path);
  7795. // Set client CA list to be sent to clients during TLS handshake
  7796. if (client_ca_cert_file_path) {
  7797. auto ca_list = SSL_load_client_CA_file(client_ca_cert_file_path);
  7798. if (ca_list != nullptr) {
  7799. SSL_CTX_set_client_CA_list(ctx_, ca_list);
  7800. } else {
  7801. // Failed to load client CA list, but we continue since
  7802. // SSL_CTX_load_verify_locations already succeeded and
  7803. // certificate verification will still work
  7804. last_ssl_error_ = static_cast<int>(ERR_get_error());
  7805. }
  7806. }
  7807. SSL_CTX_set_verify(
  7808. ctx_, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nullptr);
  7809. }
  7810. }
  7811. }
  7812. SSLServer::SSLServer(X509 *cert, EVP_PKEY *private_key,
  7813. X509_STORE *client_ca_cert_store) {
  7814. ctx_ = SSL_CTX_new(TLS_server_method());
  7815. if (ctx_) {
  7816. SSL_CTX_set_options(ctx_,
  7817. SSL_OP_NO_COMPRESSION |
  7818. SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION);
  7819. SSL_CTX_set_min_proto_version(ctx_, TLS1_2_VERSION);
  7820. if (SSL_CTX_use_certificate(ctx_, cert) != 1 ||
  7821. SSL_CTX_use_PrivateKey(ctx_, private_key) != 1) {
  7822. SSL_CTX_free(ctx_);
  7823. ctx_ = nullptr;
  7824. } else if (client_ca_cert_store) {
  7825. SSL_CTX_set_cert_store(ctx_, client_ca_cert_store);
  7826. // Extract CA names from the store and set them as the client CA list
  7827. auto ca_list = extract_ca_names_from_x509_store(client_ca_cert_store);
  7828. if (ca_list) {
  7829. SSL_CTX_set_client_CA_list(ctx_, ca_list);
  7830. } else {
  7831. // Failed to extract CA names, record the error
  7832. last_ssl_error_ = static_cast<int>(ERR_get_error());
  7833. }
  7834. SSL_CTX_set_verify(
  7835. ctx_, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nullptr);
  7836. }
  7837. }
  7838. }
  7839. SSLServer::SSLServer(
  7840. const std::function<bool(SSL_CTX &ssl_ctx)> &setup_ssl_ctx_callback) {
  7841. ctx_ = SSL_CTX_new(TLS_method());
  7842. if (ctx_) {
  7843. if (!setup_ssl_ctx_callback(*ctx_)) {
  7844. SSL_CTX_free(ctx_);
  7845. ctx_ = nullptr;
  7846. }
  7847. }
  7848. }
  7849. SSLServer::~SSLServer() {
  7850. if (ctx_) { SSL_CTX_free(ctx_); }
  7851. }
  7852. bool SSLServer::is_valid() const { return ctx_; }
  7853. SSL_CTX *SSLServer::ssl_context() const { return ctx_; }
  7854. void SSLServer::update_certs(X509 *cert, EVP_PKEY *private_key,
  7855. X509_STORE *client_ca_cert_store) {
  7856. std::lock_guard<std::mutex> guard(ctx_mutex_);
  7857. SSL_CTX_use_certificate(ctx_, cert);
  7858. SSL_CTX_use_PrivateKey(ctx_, private_key);
  7859. if (client_ca_cert_store != nullptr) {
  7860. SSL_CTX_set_cert_store(ctx_, client_ca_cert_store);
  7861. }
  7862. }
  7863. bool SSLServer::process_and_close_socket(socket_t sock) {
  7864. auto ssl = detail::ssl_new(
  7865. sock, ctx_, ctx_mutex_,
  7866. [&](SSL *ssl2) {
  7867. return detail::ssl_connect_or_accept_nonblocking(
  7868. sock, ssl2, SSL_accept, read_timeout_sec_, read_timeout_usec_,
  7869. &last_ssl_error_);
  7870. },
  7871. [](SSL * /*ssl2*/) { return true; });
  7872. auto ret = false;
  7873. if (ssl) {
  7874. std::string remote_addr;
  7875. int remote_port = 0;
  7876. detail::get_remote_ip_and_port(sock, remote_addr, remote_port);
  7877. std::string local_addr;
  7878. int local_port = 0;
  7879. detail::get_local_ip_and_port(sock, local_addr, local_port);
  7880. ret = detail::process_server_socket_ssl(
  7881. svr_sock_, ssl, sock, keep_alive_max_count_, keep_alive_timeout_sec_,
  7882. read_timeout_sec_, read_timeout_usec_, write_timeout_sec_,
  7883. write_timeout_usec_,
  7884. [&](Stream &strm, bool close_connection, bool &connection_closed) {
  7885. return process_request(strm, remote_addr, remote_port, local_addr,
  7886. local_port, close_connection,
  7887. connection_closed,
  7888. [&](Request &req) { req.ssl = ssl; });
  7889. });
  7890. // Shutdown gracefully if the result seemed successful, non-gracefully if
  7891. // the connection appeared to be closed.
  7892. const bool shutdown_gracefully = ret;
  7893. detail::ssl_delete(ctx_mutex_, ssl, sock, shutdown_gracefully);
  7894. }
  7895. detail::shutdown_socket(sock);
  7896. detail::close_socket(sock);
  7897. return ret;
  7898. }
  7899. STACK_OF(X509_NAME) * SSLServer::extract_ca_names_from_x509_store(
  7900. X509_STORE *store) {
  7901. if (!store) { return nullptr; }
  7902. auto ca_list = sk_X509_NAME_new_null();
  7903. if (!ca_list) { return nullptr; }
  7904. // Get all objects from the store
  7905. auto objs = X509_STORE_get0_objects(store);
  7906. if (!objs) {
  7907. sk_X509_NAME_free(ca_list);
  7908. return nullptr;
  7909. }
  7910. // Iterate through objects and extract certificate subject names
  7911. for (int i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  7912. auto obj = sk_X509_OBJECT_value(objs, i);
  7913. if (X509_OBJECT_get_type(obj) == X509_LU_X509) {
  7914. auto cert = X509_OBJECT_get0_X509(obj);
  7915. if (cert) {
  7916. auto subject = X509_get_subject_name(cert);
  7917. if (subject) {
  7918. auto name_dup = X509_NAME_dup(subject);
  7919. if (name_dup) { sk_X509_NAME_push(ca_list, name_dup); }
  7920. }
  7921. }
  7922. }
  7923. }
  7924. // If no names were extracted, free the list and return nullptr
  7925. if (sk_X509_NAME_num(ca_list) == 0) {
  7926. sk_X509_NAME_free(ca_list);
  7927. return nullptr;
  7928. }
  7929. return ca_list;
  7930. }
  7931. // SSL HTTP client implementation
  7932. SSLClient::SSLClient(const std::string &host)
  7933. : SSLClient(host, 443, std::string(), std::string()) {}
  7934. SSLClient::SSLClient(const std::string &host, int port)
  7935. : SSLClient(host, port, std::string(), std::string()) {}
  7936. SSLClient::SSLClient(const std::string &host, int port,
  7937. const std::string &client_cert_path,
  7938. const std::string &client_key_path,
  7939. const std::string &private_key_password)
  7940. : ClientImpl(host, port, client_cert_path, client_key_path) {
  7941. ctx_ = SSL_CTX_new(TLS_client_method());
  7942. SSL_CTX_set_min_proto_version(ctx_, TLS1_2_VERSION);
  7943. detail::split(&host_[0], &host_[host_.size()], '.',
  7944. [&](const char *b, const char *e) {
  7945. host_components_.emplace_back(b, e);
  7946. });
  7947. if (!client_cert_path.empty() && !client_key_path.empty()) {
  7948. if (!private_key_password.empty()) {
  7949. SSL_CTX_set_default_passwd_cb_userdata(
  7950. ctx_, reinterpret_cast<void *>(
  7951. const_cast<char *>(private_key_password.c_str())));
  7952. }
  7953. if (SSL_CTX_use_certificate_file(ctx_, client_cert_path.c_str(),
  7954. SSL_FILETYPE_PEM) != 1 ||
  7955. SSL_CTX_use_PrivateKey_file(ctx_, client_key_path.c_str(),
  7956. SSL_FILETYPE_PEM) != 1) {
  7957. last_openssl_error_ = ERR_get_error();
  7958. SSL_CTX_free(ctx_);
  7959. ctx_ = nullptr;
  7960. }
  7961. }
  7962. }
  7963. SSLClient::SSLClient(const std::string &host, int port,
  7964. X509 *client_cert, EVP_PKEY *client_key,
  7965. const std::string &private_key_password)
  7966. : ClientImpl(host, port) {
  7967. ctx_ = SSL_CTX_new(TLS_client_method());
  7968. detail::split(&host_[0], &host_[host_.size()], '.',
  7969. [&](const char *b, const char *e) {
  7970. host_components_.emplace_back(b, e);
  7971. });
  7972. if (client_cert != nullptr && client_key != nullptr) {
  7973. if (!private_key_password.empty()) {
  7974. SSL_CTX_set_default_passwd_cb_userdata(
  7975. ctx_, reinterpret_cast<void *>(
  7976. const_cast<char *>(private_key_password.c_str())));
  7977. }
  7978. if (SSL_CTX_use_certificate(ctx_, client_cert) != 1 ||
  7979. SSL_CTX_use_PrivateKey(ctx_, client_key) != 1) {
  7980. last_openssl_error_ = ERR_get_error();
  7981. SSL_CTX_free(ctx_);
  7982. ctx_ = nullptr;
  7983. }
  7984. }
  7985. }
  7986. SSLClient::~SSLClient() {
  7987. if (ctx_) { SSL_CTX_free(ctx_); }
  7988. // Make sure to shut down SSL since shutdown_ssl will resolve to the
  7989. // base function rather than the derived function once we get to the
  7990. // base class destructor, and won't free the SSL (causing a leak).
  7991. shutdown_ssl_impl(socket_, true);
  7992. }
  7993. bool SSLClient::is_valid() const { return ctx_; }
  7994. void SSLClient::set_ca_cert_store(X509_STORE *ca_cert_store) {
  7995. if (ca_cert_store) {
  7996. if (ctx_) {
  7997. if (SSL_CTX_get_cert_store(ctx_) != ca_cert_store) {
  7998. // Free memory allocated for old cert and use new store
  7999. // `ca_cert_store`
  8000. SSL_CTX_set_cert_store(ctx_, ca_cert_store);
  8001. ca_cert_store_ = ca_cert_store;
  8002. }
  8003. } else {
  8004. X509_STORE_free(ca_cert_store);
  8005. }
  8006. }
  8007. }
  8008. void SSLClient::load_ca_cert_store(const char *ca_cert,
  8009. std::size_t size) {
  8010. set_ca_cert_store(ClientImpl::create_ca_cert_store(ca_cert, size));
  8011. }
  8012. long SSLClient::get_openssl_verify_result() const {
  8013. return verify_result_;
  8014. }
  8015. SSL_CTX *SSLClient::ssl_context() const { return ctx_; }
  8016. bool SSLClient::create_and_connect_socket(Socket &socket, Error &error) {
  8017. if (!is_valid()) {
  8018. error = Error::SSLConnection;
  8019. return false;
  8020. }
  8021. return ClientImpl::create_and_connect_socket(socket, error);
  8022. }
  8023. // Assumes that socket_mutex_ is locked and that there are no requests in
  8024. // flight
  8025. bool SSLClient::connect_with_proxy(
  8026. Socket &socket,
  8027. std::chrono::time_point<std::chrono::steady_clock> start_time,
  8028. Response &res, bool &success, Error &error) {
  8029. success = true;
  8030. Response proxy_res;
  8031. if (!detail::process_client_socket(
  8032. socket.sock, read_timeout_sec_, read_timeout_usec_,
  8033. write_timeout_sec_, write_timeout_usec_, max_timeout_msec_,
  8034. start_time, [&](Stream &strm) {
  8035. Request req2;
  8036. req2.method = "CONNECT";
  8037. req2.path =
  8038. detail::make_host_and_port_string_always_port(host_, port_);
  8039. if (max_timeout_msec_ > 0) {
  8040. req2.start_time_ = std::chrono::steady_clock::now();
  8041. }
  8042. return process_request(strm, req2, proxy_res, false, error);
  8043. })) {
  8044. // Thread-safe to close everything because we are assuming there are no
  8045. // requests in flight
  8046. shutdown_ssl(socket, true);
  8047. shutdown_socket(socket);
  8048. close_socket(socket);
  8049. success = false;
  8050. return false;
  8051. }
  8052. if (proxy_res.status == StatusCode::ProxyAuthenticationRequired_407) {
  8053. if (!proxy_digest_auth_username_.empty() &&
  8054. !proxy_digest_auth_password_.empty()) {
  8055. std::map<std::string, std::string> auth;
  8056. if (detail::parse_www_authenticate(proxy_res, auth, true)) {
  8057. // Close the current socket and create a new one for the authenticated
  8058. // request
  8059. shutdown_ssl(socket, true);
  8060. shutdown_socket(socket);
  8061. close_socket(socket);
  8062. // Create a new socket for the authenticated CONNECT request
  8063. if (!ensure_socket_connection(socket, error)) {
  8064. success = false;
  8065. output_error_log(error, nullptr);
  8066. return false;
  8067. }
  8068. proxy_res = Response();
  8069. if (!detail::process_client_socket(
  8070. socket.sock, read_timeout_sec_, read_timeout_usec_,
  8071. write_timeout_sec_, write_timeout_usec_, max_timeout_msec_,
  8072. start_time, [&](Stream &strm) {
  8073. Request req3;
  8074. req3.method = "CONNECT";
  8075. req3.path = detail::make_host_and_port_string_always_port(
  8076. host_, port_);
  8077. req3.headers.insert(detail::make_digest_authentication_header(
  8078. req3, auth, 1, detail::random_string(10),
  8079. proxy_digest_auth_username_, proxy_digest_auth_password_,
  8080. true));
  8081. if (max_timeout_msec_ > 0) {
  8082. req3.start_time_ = std::chrono::steady_clock::now();
  8083. }
  8084. return process_request(strm, req3, proxy_res, false, error);
  8085. })) {
  8086. // Thread-safe to close everything because we are assuming there are
  8087. // no requests in flight
  8088. shutdown_ssl(socket, true);
  8089. shutdown_socket(socket);
  8090. close_socket(socket);
  8091. success = false;
  8092. return false;
  8093. }
  8094. }
  8095. }
  8096. }
  8097. // If status code is not 200, proxy request is failed.
  8098. // Set error to ProxyConnection and return proxy response
  8099. // as the response of the request
  8100. if (proxy_res.status != StatusCode::OK_200) {
  8101. error = Error::ProxyConnection;
  8102. output_error_log(error, nullptr);
  8103. res = std::move(proxy_res);
  8104. // Thread-safe to close everything because we are assuming there are
  8105. // no requests in flight
  8106. shutdown_ssl(socket, true);
  8107. shutdown_socket(socket);
  8108. close_socket(socket);
  8109. return false;
  8110. }
  8111. return true;
  8112. }
  8113. bool SSLClient::load_certs() {
  8114. auto ret = true;
  8115. std::call_once(initialize_cert_, [&]() {
  8116. std::lock_guard<std::mutex> guard(ctx_mutex_);
  8117. if (!ca_cert_file_path_.empty()) {
  8118. if (!SSL_CTX_load_verify_locations(ctx_, ca_cert_file_path_.c_str(),
  8119. nullptr)) {
  8120. last_openssl_error_ = ERR_get_error();
  8121. ret = false;
  8122. }
  8123. } else if (!ca_cert_dir_path_.empty()) {
  8124. if (!SSL_CTX_load_verify_locations(ctx_, nullptr,
  8125. ca_cert_dir_path_.c_str())) {
  8126. last_openssl_error_ = ERR_get_error();
  8127. ret = false;
  8128. }
  8129. } else {
  8130. auto loaded = false;
  8131. #ifdef _WIN32
  8132. loaded =
  8133. detail::load_system_certs_on_windows(SSL_CTX_get_cert_store(ctx_));
  8134. #elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && TARGET_OS_MAC
  8135. loaded = detail::load_system_certs_on_macos(SSL_CTX_get_cert_store(ctx_));
  8136. #endif // _WIN32
  8137. if (!loaded) { SSL_CTX_set_default_verify_paths(ctx_); }
  8138. }
  8139. });
  8140. return ret;
  8141. }
  8142. bool SSLClient::initialize_ssl(Socket &socket, Error &error) {
  8143. auto ssl = detail::ssl_new(
  8144. socket.sock, ctx_, ctx_mutex_,
  8145. [&](SSL *ssl2) {
  8146. if (server_certificate_verification_) {
  8147. if (!load_certs()) {
  8148. error = Error::SSLLoadingCerts;
  8149. output_error_log(error, nullptr);
  8150. return false;
  8151. }
  8152. SSL_set_verify(ssl2, SSL_VERIFY_NONE, nullptr);
  8153. }
  8154. if (!detail::ssl_connect_or_accept_nonblocking(
  8155. socket.sock, ssl2, SSL_connect, connection_timeout_sec_,
  8156. connection_timeout_usec_, &last_ssl_error_)) {
  8157. error = Error::SSLConnection;
  8158. output_error_log(error, nullptr);
  8159. return false;
  8160. }
  8161. if (server_certificate_verification_) {
  8162. auto verification_status = SSLVerifierResponse::NoDecisionMade;
  8163. if (server_certificate_verifier_) {
  8164. verification_status = server_certificate_verifier_(ssl2);
  8165. }
  8166. if (verification_status == SSLVerifierResponse::CertificateRejected) {
  8167. last_openssl_error_ = ERR_get_error();
  8168. error = Error::SSLServerVerification;
  8169. output_error_log(error, nullptr);
  8170. return false;
  8171. }
  8172. if (verification_status == SSLVerifierResponse::NoDecisionMade) {
  8173. verify_result_ = SSL_get_verify_result(ssl2);
  8174. if (verify_result_ != X509_V_OK) {
  8175. last_openssl_error_ = static_cast<unsigned long>(verify_result_);
  8176. error = Error::SSLServerVerification;
  8177. output_error_log(error, nullptr);
  8178. return false;
  8179. }
  8180. auto server_cert = SSL_get1_peer_certificate(ssl2);
  8181. auto se = detail::scope_exit([&] { X509_free(server_cert); });
  8182. if (server_cert == nullptr) {
  8183. last_openssl_error_ = ERR_get_error();
  8184. error = Error::SSLServerVerification;
  8185. output_error_log(error, nullptr);
  8186. return false;
  8187. }
  8188. if (server_hostname_verification_) {
  8189. if (!verify_host(server_cert)) {
  8190. last_openssl_error_ = X509_V_ERR_HOSTNAME_MISMATCH;
  8191. error = Error::SSLServerHostnameVerification;
  8192. output_error_log(error, nullptr);
  8193. return false;
  8194. }
  8195. }
  8196. }
  8197. }
  8198. return true;
  8199. },
  8200. [&](SSL *ssl2) {
  8201. // Set SNI only if host is not IP address
  8202. if (!detail::is_ip_address(host_)) {
  8203. #if defined(OPENSSL_IS_BORINGSSL)
  8204. SSL_set_tlsext_host_name(ssl2, host_.c_str());
  8205. #else
  8206. // NOTE: Direct call instead of using the OpenSSL macro to suppress
  8207. // -Wold-style-cast warning
  8208. SSL_ctrl(ssl2, SSL_CTRL_SET_TLSEXT_HOSTNAME,
  8209. TLSEXT_NAMETYPE_host_name,
  8210. static_cast<void *>(const_cast<char *>(host_.c_str())));
  8211. #endif
  8212. }
  8213. return true;
  8214. });
  8215. if (ssl) {
  8216. socket.ssl = ssl;
  8217. return true;
  8218. }
  8219. if (ctx_ == nullptr) {
  8220. error = Error::SSLConnection;
  8221. last_openssl_error_ = ERR_get_error();
  8222. }
  8223. shutdown_socket(socket);
  8224. close_socket(socket);
  8225. return false;
  8226. }
  8227. void SSLClient::shutdown_ssl(Socket &socket, bool shutdown_gracefully) {
  8228. shutdown_ssl_impl(socket, shutdown_gracefully);
  8229. }
  8230. void SSLClient::shutdown_ssl_impl(Socket &socket,
  8231. bool shutdown_gracefully) {
  8232. if (socket.sock == INVALID_SOCKET) {
  8233. assert(socket.ssl == nullptr);
  8234. return;
  8235. }
  8236. if (socket.ssl) {
  8237. detail::ssl_delete(ctx_mutex_, socket.ssl, socket.sock,
  8238. shutdown_gracefully);
  8239. socket.ssl = nullptr;
  8240. }
  8241. assert(socket.ssl == nullptr);
  8242. }
  8243. bool SSLClient::process_socket(
  8244. const Socket &socket,
  8245. std::chrono::time_point<std::chrono::steady_clock> start_time,
  8246. std::function<bool(Stream &strm)> callback) {
  8247. assert(socket.ssl);
  8248. return detail::process_client_socket_ssl(
  8249. socket.ssl, socket.sock, read_timeout_sec_, read_timeout_usec_,
  8250. write_timeout_sec_, write_timeout_usec_, max_timeout_msec_, start_time,
  8251. std::move(callback));
  8252. }
  8253. bool SSLClient::is_ssl() const { return true; }
  8254. bool SSLClient::verify_host(X509 *server_cert) const {
  8255. /* Quote from RFC2818 section 3.1 "Server Identity"
  8256. If a subjectAltName extension of type dNSName is present, that MUST
  8257. be used as the identity. Otherwise, the (most specific) Common Name
  8258. field in the Subject field of the certificate MUST be used. Although
  8259. the use of the Common Name is existing practice, it is deprecated and
  8260. Certification Authorities are encouraged to use the dNSName instead.
  8261. Matching is performed using the matching rules specified by
  8262. [RFC2459]. If more than one identity of a given type is present in
  8263. the certificate (e.g., more than one dNSName name, a match in any one
  8264. of the set is considered acceptable.) Names may contain the wildcard
  8265. character * which is considered to match any single domain name
  8266. component or component fragment. E.g., *.a.com matches foo.a.com but
  8267. not bar.foo.a.com. f*.com matches foo.com but not bar.com.
  8268. In some cases, the URI is specified as an IP address rather than a
  8269. hostname. In this case, the iPAddress subjectAltName must be present
  8270. in the certificate and must exactly match the IP in the URI.
  8271. */
  8272. return verify_host_with_subject_alt_name(server_cert) ||
  8273. verify_host_with_common_name(server_cert);
  8274. }
  8275. bool
  8276. SSLClient::verify_host_with_subject_alt_name(X509 *server_cert) const {
  8277. auto ret = false;
  8278. auto type = GEN_DNS;
  8279. struct in6_addr addr6 = {};
  8280. struct in_addr addr = {};
  8281. size_t addr_len = 0;
  8282. #ifndef __MINGW32__
  8283. if (inet_pton(AF_INET6, host_.c_str(), &addr6)) {
  8284. type = GEN_IPADD;
  8285. addr_len = sizeof(struct in6_addr);
  8286. } else if (inet_pton(AF_INET, host_.c_str(), &addr)) {
  8287. type = GEN_IPADD;
  8288. addr_len = sizeof(struct in_addr);
  8289. }
  8290. #endif
  8291. auto alt_names = static_cast<const struct stack_st_GENERAL_NAME *>(
  8292. X509_get_ext_d2i(server_cert, NID_subject_alt_name, nullptr, nullptr));
  8293. if (alt_names) {
  8294. auto dsn_matched = false;
  8295. auto ip_matched = false;
  8296. auto count = sk_GENERAL_NAME_num(alt_names);
  8297. for (decltype(count) i = 0; i < count && !dsn_matched; i++) {
  8298. auto val = sk_GENERAL_NAME_value(alt_names, i);
  8299. if (!val || val->type != type) { continue; }
  8300. auto name =
  8301. reinterpret_cast<const char *>(ASN1_STRING_get0_data(val->d.ia5));
  8302. if (name == nullptr) { continue; }
  8303. auto name_len = static_cast<size_t>(ASN1_STRING_length(val->d.ia5));
  8304. switch (type) {
  8305. case GEN_DNS: dsn_matched = check_host_name(name, name_len); break;
  8306. case GEN_IPADD:
  8307. if (!memcmp(&addr6, name, addr_len) || !memcmp(&addr, name, addr_len)) {
  8308. ip_matched = true;
  8309. }
  8310. break;
  8311. }
  8312. }
  8313. if (dsn_matched || ip_matched) { ret = true; }
  8314. }
  8315. GENERAL_NAMES_free(const_cast<STACK_OF(GENERAL_NAME) *>(
  8316. reinterpret_cast<const STACK_OF(GENERAL_NAME) *>(alt_names)));
  8317. return ret;
  8318. }
  8319. bool SSLClient::verify_host_with_common_name(X509 *server_cert) const {
  8320. const auto subject_name = X509_get_subject_name(server_cert);
  8321. if (subject_name != nullptr) {
  8322. char name[BUFSIZ];
  8323. auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName,
  8324. name, sizeof(name));
  8325. if (name_len != -1) {
  8326. return check_host_name(name, static_cast<size_t>(name_len));
  8327. }
  8328. }
  8329. return false;
  8330. }
  8331. bool SSLClient::check_host_name(const char *pattern,
  8332. size_t pattern_len) const {
  8333. if (host_.size() == pattern_len && host_ == pattern) { return true; }
  8334. // Wildcard match
  8335. // https://bugs.launchpad.net/ubuntu/+source/firefox-3.0/+bug/376484
  8336. std::vector<std::string> pattern_components;
  8337. detail::split(&pattern[0], &pattern[pattern_len], '.',
  8338. [&](const char *b, const char *e) {
  8339. pattern_components.emplace_back(b, e);
  8340. });
  8341. if (host_components_.size() != pattern_components.size()) { return false; }
  8342. auto itr = pattern_components.begin();
  8343. for (const auto &h : host_components_) {
  8344. auto &p = *itr;
  8345. if (p != h && p != "*") {
  8346. auto partial_match = (p.size() > 0 && p[p.size() - 1] == '*' &&
  8347. !p.compare(0, p.size() - 1, h));
  8348. if (!partial_match) { return false; }
  8349. }
  8350. ++itr;
  8351. }
  8352. return true;
  8353. }
  8354. #endif
  8355. // Universal client implementation
  8356. Client::Client(const std::string &scheme_host_port)
  8357. : Client(scheme_host_port, std::string(), std::string()) {}
  8358. Client::Client(const std::string &scheme_host_port,
  8359. const std::string &client_cert_path,
  8360. const std::string &client_key_path) {
  8361. const static std::regex re(
  8362. R"((?:([a-z]+):\/\/)?(?:\[([a-fA-F\d:]+)\]|([^:/?#]+))(?::(\d+))?)");
  8363. std::smatch m;
  8364. if (std::regex_match(scheme_host_port, m, re)) {
  8365. auto scheme = m[1].str();
  8366. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  8367. if (!scheme.empty() && (scheme != "http" && scheme != "https")) {
  8368. #else
  8369. if (!scheme.empty() && scheme != "http") {
  8370. #endif
  8371. #ifndef CPPHTTPLIB_NO_EXCEPTIONS
  8372. std::string msg = "'" + scheme + "' scheme is not supported.";
  8373. throw std::invalid_argument(msg);
  8374. #endif
  8375. return;
  8376. }
  8377. auto is_ssl = scheme == "https";
  8378. auto host = m[2].str();
  8379. if (host.empty()) { host = m[3].str(); }
  8380. auto port_str = m[4].str();
  8381. auto port = !port_str.empty() ? std::stoi(port_str) : (is_ssl ? 443 : 80);
  8382. if (is_ssl) {
  8383. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  8384. cli_ = detail::make_unique<SSLClient>(host, port, client_cert_path,
  8385. client_key_path);
  8386. is_ssl_ = is_ssl;
  8387. #endif
  8388. } else {
  8389. cli_ = detail::make_unique<ClientImpl>(host, port, client_cert_path,
  8390. client_key_path);
  8391. }
  8392. } else {
  8393. // NOTE: Update TEST(UniversalClientImplTest, Ipv6LiteralAddress)
  8394. // if port param below changes.
  8395. cli_ = detail::make_unique<ClientImpl>(scheme_host_port, 80,
  8396. client_cert_path, client_key_path);
  8397. }
  8398. } // namespace detail
  8399. Client::Client(const std::string &host, int port)
  8400. : cli_(detail::make_unique<ClientImpl>(host, port)) {}
  8401. Client::Client(const std::string &host, int port,
  8402. const std::string &client_cert_path,
  8403. const std::string &client_key_path)
  8404. : cli_(detail::make_unique<ClientImpl>(host, port, client_cert_path,
  8405. client_key_path)) {}
  8406. Client::~Client() = default;
  8407. bool Client::is_valid() const {
  8408. return cli_ != nullptr && cli_->is_valid();
  8409. }
  8410. Result Client::Get(const std::string &path, DownloadProgress progress) {
  8411. return cli_->Get(path, std::move(progress));
  8412. }
  8413. Result Client::Get(const std::string &path, const Headers &headers,
  8414. DownloadProgress progress) {
  8415. return cli_->Get(path, headers, std::move(progress));
  8416. }
  8417. Result Client::Get(const std::string &path,
  8418. ContentReceiver content_receiver,
  8419. DownloadProgress progress) {
  8420. return cli_->Get(path, std::move(content_receiver), std::move(progress));
  8421. }
  8422. Result Client::Get(const std::string &path, const Headers &headers,
  8423. ContentReceiver content_receiver,
  8424. DownloadProgress progress) {
  8425. return cli_->Get(path, headers, std::move(content_receiver),
  8426. std::move(progress));
  8427. }
  8428. Result Client::Get(const std::string &path,
  8429. ResponseHandler response_handler,
  8430. ContentReceiver content_receiver,
  8431. DownloadProgress progress) {
  8432. return cli_->Get(path, std::move(response_handler),
  8433. std::move(content_receiver), std::move(progress));
  8434. }
  8435. Result Client::Get(const std::string &path, const Headers &headers,
  8436. ResponseHandler response_handler,
  8437. ContentReceiver content_receiver,
  8438. DownloadProgress progress) {
  8439. return cli_->Get(path, headers, std::move(response_handler),
  8440. std::move(content_receiver), std::move(progress));
  8441. }
  8442. Result Client::Get(const std::string &path, const Params &params,
  8443. const Headers &headers, DownloadProgress progress) {
  8444. return cli_->Get(path, params, headers, std::move(progress));
  8445. }
  8446. Result Client::Get(const std::string &path, const Params &params,
  8447. const Headers &headers,
  8448. ContentReceiver content_receiver,
  8449. DownloadProgress progress) {
  8450. return cli_->Get(path, params, headers, std::move(content_receiver),
  8451. std::move(progress));
  8452. }
  8453. Result Client::Get(const std::string &path, const Params &params,
  8454. const Headers &headers,
  8455. ResponseHandler response_handler,
  8456. ContentReceiver content_receiver,
  8457. DownloadProgress progress) {
  8458. return cli_->Get(path, params, headers, std::move(response_handler),
  8459. std::move(content_receiver), std::move(progress));
  8460. }
  8461. Result Client::Head(const std::string &path) { return cli_->Head(path); }
  8462. Result Client::Head(const std::string &path, const Headers &headers) {
  8463. return cli_->Head(path, headers);
  8464. }
  8465. Result Client::Post(const std::string &path) { return cli_->Post(path); }
  8466. Result Client::Post(const std::string &path, const Headers &headers) {
  8467. return cli_->Post(path, headers);
  8468. }
  8469. Result Client::Post(const std::string &path, const char *body,
  8470. size_t content_length,
  8471. const std::string &content_type,
  8472. UploadProgress progress) {
  8473. return cli_->Post(path, body, content_length, content_type, progress);
  8474. }
  8475. Result Client::Post(const std::string &path, const Headers &headers,
  8476. const char *body, size_t content_length,
  8477. const std::string &content_type,
  8478. UploadProgress progress) {
  8479. return cli_->Post(path, headers, body, content_length, content_type,
  8480. progress);
  8481. }
  8482. Result Client::Post(const std::string &path, const std::string &body,
  8483. const std::string &content_type,
  8484. UploadProgress progress) {
  8485. return cli_->Post(path, body, content_type, progress);
  8486. }
  8487. Result Client::Post(const std::string &path, const Headers &headers,
  8488. const std::string &body,
  8489. const std::string &content_type,
  8490. UploadProgress progress) {
  8491. return cli_->Post(path, headers, body, content_type, progress);
  8492. }
  8493. Result Client::Post(const std::string &path, size_t content_length,
  8494. ContentProvider content_provider,
  8495. const std::string &content_type,
  8496. UploadProgress progress) {
  8497. return cli_->Post(path, content_length, std::move(content_provider),
  8498. content_type, progress);
  8499. }
  8500. Result Client::Post(const std::string &path, size_t content_length,
  8501. ContentProvider content_provider,
  8502. const std::string &content_type,
  8503. ContentReceiver content_receiver,
  8504. UploadProgress progress) {
  8505. return cli_->Post(path, content_length, std::move(content_provider),
  8506. content_type, std::move(content_receiver), progress);
  8507. }
  8508. Result Client::Post(const std::string &path,
  8509. ContentProviderWithoutLength content_provider,
  8510. const std::string &content_type,
  8511. UploadProgress progress) {
  8512. return cli_->Post(path, std::move(content_provider), content_type, progress);
  8513. }
  8514. Result Client::Post(const std::string &path,
  8515. ContentProviderWithoutLength content_provider,
  8516. const std::string &content_type,
  8517. ContentReceiver content_receiver,
  8518. UploadProgress progress) {
  8519. return cli_->Post(path, std::move(content_provider), content_type,
  8520. std::move(content_receiver), progress);
  8521. }
  8522. Result Client::Post(const std::string &path, const Headers &headers,
  8523. size_t content_length,
  8524. ContentProvider content_provider,
  8525. const std::string &content_type,
  8526. UploadProgress progress) {
  8527. return cli_->Post(path, headers, content_length, std::move(content_provider),
  8528. content_type, progress);
  8529. }
  8530. Result Client::Post(const std::string &path, const Headers &headers,
  8531. size_t content_length,
  8532. ContentProvider content_provider,
  8533. const std::string &content_type,
  8534. ContentReceiver content_receiver,
  8535. DownloadProgress progress) {
  8536. return cli_->Post(path, headers, content_length, std::move(content_provider),
  8537. content_type, std::move(content_receiver), progress);
  8538. }
  8539. Result Client::Post(const std::string &path, const Headers &headers,
  8540. ContentProviderWithoutLength content_provider,
  8541. const std::string &content_type,
  8542. UploadProgress progress) {
  8543. return cli_->Post(path, headers, std::move(content_provider), content_type,
  8544. progress);
  8545. }
  8546. Result Client::Post(const std::string &path, const Headers &headers,
  8547. ContentProviderWithoutLength content_provider,
  8548. const std::string &content_type,
  8549. ContentReceiver content_receiver,
  8550. DownloadProgress progress) {
  8551. return cli_->Post(path, headers, std::move(content_provider), content_type,
  8552. std::move(content_receiver), progress);
  8553. }
  8554. Result Client::Post(const std::string &path, const Params &params) {
  8555. return cli_->Post(path, params);
  8556. }
  8557. Result Client::Post(const std::string &path, const Headers &headers,
  8558. const Params &params) {
  8559. return cli_->Post(path, headers, params);
  8560. }
  8561. Result Client::Post(const std::string &path,
  8562. const UploadFormDataItems &items,
  8563. UploadProgress progress) {
  8564. return cli_->Post(path, items, progress);
  8565. }
  8566. Result Client::Post(const std::string &path, const Headers &headers,
  8567. const UploadFormDataItems &items,
  8568. UploadProgress progress) {
  8569. return cli_->Post(path, headers, items, progress);
  8570. }
  8571. Result Client::Post(const std::string &path, const Headers &headers,
  8572. const UploadFormDataItems &items,
  8573. const std::string &boundary,
  8574. UploadProgress progress) {
  8575. return cli_->Post(path, headers, items, boundary, progress);
  8576. }
  8577. Result Client::Post(const std::string &path, const Headers &headers,
  8578. const UploadFormDataItems &items,
  8579. const FormDataProviderItems &provider_items,
  8580. UploadProgress progress) {
  8581. return cli_->Post(path, headers, items, provider_items, progress);
  8582. }
  8583. Result Client::Post(const std::string &path, const Headers &headers,
  8584. const std::string &body,
  8585. const std::string &content_type,
  8586. ContentReceiver content_receiver,
  8587. DownloadProgress progress) {
  8588. return cli_->Post(path, headers, body, content_type,
  8589. std::move(content_receiver), progress);
  8590. }
  8591. Result Client::Put(const std::string &path) { return cli_->Put(path); }
  8592. Result Client::Put(const std::string &path, const Headers &headers) {
  8593. return cli_->Put(path, headers);
  8594. }
  8595. Result Client::Put(const std::string &path, const char *body,
  8596. size_t content_length,
  8597. const std::string &content_type,
  8598. UploadProgress progress) {
  8599. return cli_->Put(path, body, content_length, content_type, progress);
  8600. }
  8601. Result Client::Put(const std::string &path, const Headers &headers,
  8602. const char *body, size_t content_length,
  8603. const std::string &content_type,
  8604. UploadProgress progress) {
  8605. return cli_->Put(path, headers, body, content_length, content_type, progress);
  8606. }
  8607. Result Client::Put(const std::string &path, const std::string &body,
  8608. const std::string &content_type,
  8609. UploadProgress progress) {
  8610. return cli_->Put(path, body, content_type, progress);
  8611. }
  8612. Result Client::Put(const std::string &path, const Headers &headers,
  8613. const std::string &body,
  8614. const std::string &content_type,
  8615. UploadProgress progress) {
  8616. return cli_->Put(path, headers, body, content_type, progress);
  8617. }
  8618. Result Client::Put(const std::string &path, size_t content_length,
  8619. ContentProvider content_provider,
  8620. const std::string &content_type,
  8621. UploadProgress progress) {
  8622. return cli_->Put(path, content_length, std::move(content_provider),
  8623. content_type, progress);
  8624. }
  8625. Result Client::Put(const std::string &path, size_t content_length,
  8626. ContentProvider content_provider,
  8627. const std::string &content_type,
  8628. ContentReceiver content_receiver,
  8629. UploadProgress progress) {
  8630. return cli_->Put(path, content_length, std::move(content_provider),
  8631. content_type, std::move(content_receiver), progress);
  8632. }
  8633. Result Client::Put(const std::string &path,
  8634. ContentProviderWithoutLength content_provider,
  8635. const std::string &content_type,
  8636. UploadProgress progress) {
  8637. return cli_->Put(path, std::move(content_provider), content_type, progress);
  8638. }
  8639. Result Client::Put(const std::string &path,
  8640. ContentProviderWithoutLength content_provider,
  8641. const std::string &content_type,
  8642. ContentReceiver content_receiver,
  8643. UploadProgress progress) {
  8644. return cli_->Put(path, std::move(content_provider), content_type,
  8645. std::move(content_receiver), progress);
  8646. }
  8647. Result Client::Put(const std::string &path, const Headers &headers,
  8648. size_t content_length,
  8649. ContentProvider content_provider,
  8650. const std::string &content_type,
  8651. UploadProgress progress) {
  8652. return cli_->Put(path, headers, content_length, std::move(content_provider),
  8653. content_type, progress);
  8654. }
  8655. Result Client::Put(const std::string &path, const Headers &headers,
  8656. size_t content_length,
  8657. ContentProvider content_provider,
  8658. const std::string &content_type,
  8659. ContentReceiver content_receiver,
  8660. UploadProgress progress) {
  8661. return cli_->Put(path, headers, content_length, std::move(content_provider),
  8662. content_type, std::move(content_receiver), progress);
  8663. }
  8664. Result Client::Put(const std::string &path, const Headers &headers,
  8665. ContentProviderWithoutLength content_provider,
  8666. const std::string &content_type,
  8667. UploadProgress progress) {
  8668. return cli_->Put(path, headers, std::move(content_provider), content_type,
  8669. progress);
  8670. }
  8671. Result Client::Put(const std::string &path, const Headers &headers,
  8672. ContentProviderWithoutLength content_provider,
  8673. const std::string &content_type,
  8674. ContentReceiver content_receiver,
  8675. UploadProgress progress) {
  8676. return cli_->Put(path, headers, std::move(content_provider), content_type,
  8677. std::move(content_receiver), progress);
  8678. }
  8679. Result Client::Put(const std::string &path, const Params &params) {
  8680. return cli_->Put(path, params);
  8681. }
  8682. Result Client::Put(const std::string &path, const Headers &headers,
  8683. const Params &params) {
  8684. return cli_->Put(path, headers, params);
  8685. }
  8686. Result Client::Put(const std::string &path,
  8687. const UploadFormDataItems &items,
  8688. UploadProgress progress) {
  8689. return cli_->Put(path, items, progress);
  8690. }
  8691. Result Client::Put(const std::string &path, const Headers &headers,
  8692. const UploadFormDataItems &items,
  8693. UploadProgress progress) {
  8694. return cli_->Put(path, headers, items, progress);
  8695. }
  8696. Result Client::Put(const std::string &path, const Headers &headers,
  8697. const UploadFormDataItems &items,
  8698. const std::string &boundary,
  8699. UploadProgress progress) {
  8700. return cli_->Put(path, headers, items, boundary, progress);
  8701. }
  8702. Result Client::Put(const std::string &path, const Headers &headers,
  8703. const UploadFormDataItems &items,
  8704. const FormDataProviderItems &provider_items,
  8705. UploadProgress progress) {
  8706. return cli_->Put(path, headers, items, provider_items, progress);
  8707. }
  8708. Result Client::Put(const std::string &path, const Headers &headers,
  8709. const std::string &body,
  8710. const std::string &content_type,
  8711. ContentReceiver content_receiver,
  8712. DownloadProgress progress) {
  8713. return cli_->Put(path, headers, body, content_type, content_receiver,
  8714. progress);
  8715. }
  8716. Result Client::Patch(const std::string &path) {
  8717. return cli_->Patch(path);
  8718. }
  8719. Result Client::Patch(const std::string &path, const Headers &headers) {
  8720. return cli_->Patch(path, headers);
  8721. }
  8722. Result Client::Patch(const std::string &path, const char *body,
  8723. size_t content_length,
  8724. const std::string &content_type,
  8725. UploadProgress progress) {
  8726. return cli_->Patch(path, body, content_length, content_type, progress);
  8727. }
  8728. Result Client::Patch(const std::string &path, const Headers &headers,
  8729. const char *body, size_t content_length,
  8730. const std::string &content_type,
  8731. UploadProgress progress) {
  8732. return cli_->Patch(path, headers, body, content_length, content_type,
  8733. progress);
  8734. }
  8735. Result Client::Patch(const std::string &path, const std::string &body,
  8736. const std::string &content_type,
  8737. UploadProgress progress) {
  8738. return cli_->Patch(path, body, content_type, progress);
  8739. }
  8740. Result Client::Patch(const std::string &path, const Headers &headers,
  8741. const std::string &body,
  8742. const std::string &content_type,
  8743. UploadProgress progress) {
  8744. return cli_->Patch(path, headers, body, content_type, progress);
  8745. }
  8746. Result Client::Patch(const std::string &path, size_t content_length,
  8747. ContentProvider content_provider,
  8748. const std::string &content_type,
  8749. UploadProgress progress) {
  8750. return cli_->Patch(path, content_length, std::move(content_provider),
  8751. content_type, progress);
  8752. }
  8753. Result Client::Patch(const std::string &path, size_t content_length,
  8754. ContentProvider content_provider,
  8755. const std::string &content_type,
  8756. ContentReceiver content_receiver,
  8757. UploadProgress progress) {
  8758. return cli_->Patch(path, content_length, std::move(content_provider),
  8759. content_type, std::move(content_receiver), progress);
  8760. }
  8761. Result Client::Patch(const std::string &path,
  8762. ContentProviderWithoutLength content_provider,
  8763. const std::string &content_type,
  8764. UploadProgress progress) {
  8765. return cli_->Patch(path, std::move(content_provider), content_type, progress);
  8766. }
  8767. Result Client::Patch(const std::string &path,
  8768. ContentProviderWithoutLength content_provider,
  8769. const std::string &content_type,
  8770. ContentReceiver content_receiver,
  8771. UploadProgress progress) {
  8772. return cli_->Patch(path, std::move(content_provider), content_type,
  8773. std::move(content_receiver), progress);
  8774. }
  8775. Result Client::Patch(const std::string &path, const Headers &headers,
  8776. size_t content_length,
  8777. ContentProvider content_provider,
  8778. const std::string &content_type,
  8779. UploadProgress progress) {
  8780. return cli_->Patch(path, headers, content_length, std::move(content_provider),
  8781. content_type, progress);
  8782. }
  8783. Result Client::Patch(const std::string &path, const Headers &headers,
  8784. size_t content_length,
  8785. ContentProvider content_provider,
  8786. const std::string &content_type,
  8787. ContentReceiver content_receiver,
  8788. UploadProgress progress) {
  8789. return cli_->Patch(path, headers, content_length, std::move(content_provider),
  8790. content_type, std::move(content_receiver), progress);
  8791. }
  8792. Result Client::Patch(const std::string &path, const Headers &headers,
  8793. ContentProviderWithoutLength content_provider,
  8794. const std::string &content_type,
  8795. UploadProgress progress) {
  8796. return cli_->Patch(path, headers, std::move(content_provider), content_type,
  8797. progress);
  8798. }
  8799. Result Client::Patch(const std::string &path, const Headers &headers,
  8800. ContentProviderWithoutLength content_provider,
  8801. const std::string &content_type,
  8802. ContentReceiver content_receiver,
  8803. UploadProgress progress) {
  8804. return cli_->Patch(path, headers, std::move(content_provider), content_type,
  8805. std::move(content_receiver), progress);
  8806. }
  8807. Result Client::Patch(const std::string &path, const Params &params) {
  8808. return cli_->Patch(path, params);
  8809. }
  8810. Result Client::Patch(const std::string &path, const Headers &headers,
  8811. const Params &params) {
  8812. return cli_->Patch(path, headers, params);
  8813. }
  8814. Result Client::Patch(const std::string &path,
  8815. const UploadFormDataItems &items,
  8816. UploadProgress progress) {
  8817. return cli_->Patch(path, items, progress);
  8818. }
  8819. Result Client::Patch(const std::string &path, const Headers &headers,
  8820. const UploadFormDataItems &items,
  8821. UploadProgress progress) {
  8822. return cli_->Patch(path, headers, items, progress);
  8823. }
  8824. Result Client::Patch(const std::string &path, const Headers &headers,
  8825. const UploadFormDataItems &items,
  8826. const std::string &boundary,
  8827. UploadProgress progress) {
  8828. return cli_->Patch(path, headers, items, boundary, progress);
  8829. }
  8830. Result Client::Patch(const std::string &path, const Headers &headers,
  8831. const UploadFormDataItems &items,
  8832. const FormDataProviderItems &provider_items,
  8833. UploadProgress progress) {
  8834. return cli_->Patch(path, headers, items, provider_items, progress);
  8835. }
  8836. Result Client::Patch(const std::string &path, const Headers &headers,
  8837. const std::string &body,
  8838. const std::string &content_type,
  8839. ContentReceiver content_receiver,
  8840. DownloadProgress progress) {
  8841. return cli_->Patch(path, headers, body, content_type, content_receiver,
  8842. progress);
  8843. }
  8844. Result Client::Delete(const std::string &path,
  8845. DownloadProgress progress) {
  8846. return cli_->Delete(path, progress);
  8847. }
  8848. Result Client::Delete(const std::string &path, const Headers &headers,
  8849. DownloadProgress progress) {
  8850. return cli_->Delete(path, headers, progress);
  8851. }
  8852. Result Client::Delete(const std::string &path, const char *body,
  8853. size_t content_length,
  8854. const std::string &content_type,
  8855. DownloadProgress progress) {
  8856. return cli_->Delete(path, body, content_length, content_type, progress);
  8857. }
  8858. Result Client::Delete(const std::string &path, const Headers &headers,
  8859. const char *body, size_t content_length,
  8860. const std::string &content_type,
  8861. DownloadProgress progress) {
  8862. return cli_->Delete(path, headers, body, content_length, content_type,
  8863. progress);
  8864. }
  8865. Result Client::Delete(const std::string &path, const std::string &body,
  8866. const std::string &content_type,
  8867. DownloadProgress progress) {
  8868. return cli_->Delete(path, body, content_type, progress);
  8869. }
  8870. Result Client::Delete(const std::string &path, const Headers &headers,
  8871. const std::string &body,
  8872. const std::string &content_type,
  8873. DownloadProgress progress) {
  8874. return cli_->Delete(path, headers, body, content_type, progress);
  8875. }
  8876. Result Client::Delete(const std::string &path, const Params &params,
  8877. DownloadProgress progress) {
  8878. return cli_->Delete(path, params, progress);
  8879. }
  8880. Result Client::Delete(const std::string &path, const Headers &headers,
  8881. const Params &params, DownloadProgress progress) {
  8882. return cli_->Delete(path, headers, params, progress);
  8883. }
  8884. Result Client::Options(const std::string &path) {
  8885. return cli_->Options(path);
  8886. }
  8887. Result Client::Options(const std::string &path, const Headers &headers) {
  8888. return cli_->Options(path, headers);
  8889. }
  8890. ClientImpl::StreamHandle
  8891. Client::open_stream(const std::string &method, const std::string &path,
  8892. const Params &params, const Headers &headers,
  8893. const std::string &body, const std::string &content_type) {
  8894. return cli_->open_stream(method, path, params, headers, body, content_type);
  8895. }
  8896. bool Client::send(Request &req, Response &res, Error &error) {
  8897. return cli_->send(req, res, error);
  8898. }
  8899. Result Client::send(const Request &req) { return cli_->send(req); }
  8900. void Client::stop() { cli_->stop(); }
  8901. std::string Client::host() const { return cli_->host(); }
  8902. int Client::port() const { return cli_->port(); }
  8903. size_t Client::is_socket_open() const { return cli_->is_socket_open(); }
  8904. socket_t Client::socket() const { return cli_->socket(); }
  8905. void
  8906. Client::set_hostname_addr_map(std::map<std::string, std::string> addr_map) {
  8907. cli_->set_hostname_addr_map(std::move(addr_map));
  8908. }
  8909. void Client::set_default_headers(Headers headers) {
  8910. cli_->set_default_headers(std::move(headers));
  8911. }
  8912. void Client::set_header_writer(
  8913. std::function<ssize_t(Stream &, Headers &)> const &writer) {
  8914. cli_->set_header_writer(writer);
  8915. }
  8916. void Client::set_address_family(int family) {
  8917. cli_->set_address_family(family);
  8918. }
  8919. void Client::set_tcp_nodelay(bool on) { cli_->set_tcp_nodelay(on); }
  8920. void Client::set_socket_options(SocketOptions socket_options) {
  8921. cli_->set_socket_options(std::move(socket_options));
  8922. }
  8923. void Client::set_connection_timeout(time_t sec, time_t usec) {
  8924. cli_->set_connection_timeout(sec, usec);
  8925. }
  8926. void Client::set_read_timeout(time_t sec, time_t usec) {
  8927. cli_->set_read_timeout(sec, usec);
  8928. }
  8929. void Client::set_write_timeout(time_t sec, time_t usec) {
  8930. cli_->set_write_timeout(sec, usec);
  8931. }
  8932. void Client::set_basic_auth(const std::string &username,
  8933. const std::string &password) {
  8934. cli_->set_basic_auth(username, password);
  8935. }
  8936. void Client::set_bearer_token_auth(const std::string &token) {
  8937. cli_->set_bearer_token_auth(token);
  8938. }
  8939. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  8940. void Client::set_digest_auth(const std::string &username,
  8941. const std::string &password) {
  8942. cli_->set_digest_auth(username, password);
  8943. }
  8944. #endif
  8945. void Client::set_keep_alive(bool on) { cli_->set_keep_alive(on); }
  8946. void Client::set_follow_location(bool on) {
  8947. cli_->set_follow_location(on);
  8948. }
  8949. void Client::set_path_encode(bool on) { cli_->set_path_encode(on); }
  8950. [[deprecated("Use set_path_encode instead")]]
  8951. void Client::set_url_encode(bool on) {
  8952. cli_->set_path_encode(on);
  8953. }
  8954. void Client::set_compress(bool on) { cli_->set_compress(on); }
  8955. void Client::set_decompress(bool on) { cli_->set_decompress(on); }
  8956. void Client::set_interface(const std::string &intf) {
  8957. cli_->set_interface(intf);
  8958. }
  8959. void Client::set_proxy(const std::string &host, int port) {
  8960. cli_->set_proxy(host, port);
  8961. }
  8962. void Client::set_proxy_basic_auth(const std::string &username,
  8963. const std::string &password) {
  8964. cli_->set_proxy_basic_auth(username, password);
  8965. }
  8966. void Client::set_proxy_bearer_token_auth(const std::string &token) {
  8967. cli_->set_proxy_bearer_token_auth(token);
  8968. }
  8969. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  8970. void Client::set_proxy_digest_auth(const std::string &username,
  8971. const std::string &password) {
  8972. cli_->set_proxy_digest_auth(username, password);
  8973. }
  8974. #endif
  8975. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  8976. void Client::enable_server_certificate_verification(bool enabled) {
  8977. cli_->enable_server_certificate_verification(enabled);
  8978. }
  8979. void Client::enable_server_hostname_verification(bool enabled) {
  8980. cli_->enable_server_hostname_verification(enabled);
  8981. }
  8982. void Client::set_server_certificate_verifier(
  8983. std::function<SSLVerifierResponse(SSL *ssl)> verifier) {
  8984. cli_->set_server_certificate_verifier(verifier);
  8985. }
  8986. #endif
  8987. void Client::set_logger(Logger logger) {
  8988. cli_->set_logger(std::move(logger));
  8989. }
  8990. void Client::set_error_logger(ErrorLogger error_logger) {
  8991. cli_->set_error_logger(std::move(error_logger));
  8992. }
  8993. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  8994. void Client::set_ca_cert_path(const std::string &ca_cert_file_path,
  8995. const std::string &ca_cert_dir_path) {
  8996. cli_->set_ca_cert_path(ca_cert_file_path, ca_cert_dir_path);
  8997. }
  8998. void Client::set_ca_cert_store(X509_STORE *ca_cert_store) {
  8999. if (is_ssl_) {
  9000. static_cast<SSLClient &>(*cli_).set_ca_cert_store(ca_cert_store);
  9001. } else {
  9002. cli_->set_ca_cert_store(ca_cert_store);
  9003. }
  9004. }
  9005. void Client::load_ca_cert_store(const char *ca_cert, std::size_t size) {
  9006. set_ca_cert_store(cli_->create_ca_cert_store(ca_cert, size));
  9007. }
  9008. long Client::get_openssl_verify_result() const {
  9009. if (is_ssl_) {
  9010. return static_cast<SSLClient &>(*cli_).get_openssl_verify_result();
  9011. }
  9012. return -1; // NOTE: -1 doesn't match any of X509_V_ERR_???
  9013. }
  9014. SSL_CTX *Client::ssl_context() const {
  9015. if (is_ssl_) { return static_cast<SSLClient &>(*cli_).ssl_context(); }
  9016. return nullptr;
  9017. }
  9018. #endif
  9019. } // namespace httplib