@@ -111,43 +111,42 @@ inline constexpr const char* HttpGetStatusName ( EHTTP_STATUS eStatus ) noexcept
111111 };
112112}
113113
114- extern CSphString g_sMySQLVersion;
115-
116- static void HttpBuildReply ( CSphVector<BYTE> & dData, EHTTP_STATUS eCode, const char * sBody , int iBodyLen, bool bHtml, bool bHeadReply )
114+ void HttpBuildReply ( const HttpReplyTrait_t & tReply, CSphVector<BYTE> & dData )
117115{
118- assert ( sBody && iBodyLen );
116+ assert ( tReply.m_sBody .first && tReply.m_sBody .second );
117+
118+ const char * sContentType = tReply.m_sContentType ;
119+ if ( !sContentType )
120+ sContentType = ( tReply.m_bHtml ? " text/html" : " application/json" );
119121
120- const char * sContent = ( bHtml ? " text/html" : " application/json" );
121122 CSphString sHttp ;
122- sHttp .SetSprintf ( " HTTP/1.1 %s\r\n Server: %s\r\n Content-Type: %s; charset=UTF-8\r\n Content-Length:%d\r\n\r\n " , HttpGetStatusName (eCode ), g_sMySQLVersion .cstr (), sContent , iBodyLen );
123+ sHttp .SetSprintf ( " HTTP/1.1 %s\r\n Server: %s\r\n Content-Type: %s; charset=UTF-8\r\n Content-Length:%d\r\n\r\n " , HttpGetStatusName ( tReply. m_eCode ), g_sStatusVersion .cstr (), sContentType , tReply. m_sBody . second );
123124
124125 int iHeaderLen = sHttp .Length ();
125126 int iBufLen = iHeaderLen;
126- if ( !bHeadReply )
127- iBufLen += iBodyLen ;
127+ if ( !tReply. m_bHeadReply )
128+ iBufLen += tReply. m_sBody . second ;
128129 dData.Resize ( iBufLen );
129130 memcpy ( dData.Begin (), sHttp .cstr (), iHeaderLen );
130- if ( !bHeadReply )
131- memcpy ( dData.Begin () + iHeaderLen, sBody , iBodyLen );
132- }
133-
134- void HttpBuildReply ( CSphVector<BYTE> & dData, EHTTP_STATUS eCode, const char * sBody , int iBodyLen, bool bHtml )
135- {
136- HttpBuildReply ( dData, eCode, sBody , iBodyLen, bHtml, false );
131+ if ( !tReply.m_bHeadReply )
132+ memcpy ( dData.Begin () + iHeaderLen, tReply.m_sBody .first , tReply.m_sBody .second );
137133}
138134
139135void HttpBuildReplyHead ( CSphVector<BYTE> & dData, EHTTP_STATUS eCode, const char * sBody , int iBodyLen, bool bHeadReply )
140136{
141- HttpBuildReply ( dData, eCode, sBody , iBodyLen, false , bHeadReply );
137+ HttpReplyTrait_t tReply { eCode, Str_t ( sBody , iBodyLen ) };
138+ tReply.m_bHeadReply = bHeadReply;
139+ HttpBuildReply ( tReply, dData );
142140}
143141
144-
145142void HttpErrorReply ( CSphVector<BYTE> & dData, EHTTP_STATUS eCode, const char * szError )
146143{
147144 JsonObj_c tErr;
148145 tErr.AddStr ( " error" , szError );
149146 CSphString sJsonError = tErr.AsString ();
150- HttpBuildReply ( dData, eCode, sJsonError .cstr (), sJsonError .Length (), false );
147+
148+ HttpReplyTrait_t tReply { eCode, FromStr ( sJsonError ) };
149+ HttpBuildReply ( tReply, dData );
151150}
152151
153152struct Endpoint_t
@@ -466,18 +465,6 @@ CSphString HttpEndpointToStr ( EHTTP_ENDPOINT eEndpoint )
466465 return g_dEndpoints[(int )eEndpoint].m_szName1 ;
467466}
468467
469- void HttpBuildReply ( CSphVector<BYTE> & dData, EHTTP_STATUS eCode, Str_t sReply , bool bHtml )
470- {
471- const char * sContent = ( bHtml ? " text/html" : " application/json" );
472- StringBuilder_c sHttp ;
473- sHttp .Sprintf ( " HTTP/1.1 %s\r\n Server: %s\r\n Content-Type: %s; charset=UTF-8\r\n Content-Length: %d\r\n\r\n " , HttpGetStatusName ( eCode ), g_sStatusVersion.cstr (), sContent , sReply .second );
474-
475- dData.Reserve ( sHttp .GetLength () + sReply .second );
476- dData.Append ( (Str_t)sHttp );
477- dData.Append ( sReply );
478- }
479-
480-
481468HttpRequestParser_c::HttpRequestParser_c ()
482469{
483470 http_parser_settings_init ( &m_tParserSettings );
@@ -892,7 +879,10 @@ static void HttpHandlerIndexPage ( CSphVector<BYTE> & dData )
892879{
893880 StringBuilder_c sIndexPage ;
894881 sIndexPage .Appendf ( g_sIndexPage, g_sStatusVersion.cstr () );
895- HttpBuildReply ( dData, EHTTP_STATUS::_200, (Str_t)sIndexPage , true );
882+
883+ HttpReplyTrait_t tReply { EHTTP_STATUS::_200, (Str_t)sIndexPage };
884+ tReply.m_bHtml = true ;
885+ HttpBuildReply ( tReply, dData );
896886}
897887
898888// ////////////////////////////////////////////////////////////////////////
@@ -1025,14 +1015,16 @@ StmtErrorReporter_i * CreateHttpErrorReporter()
10251015// ////////////////////////////////////////////////////////////////////////
10261016// all the handlers for http queries
10271017
1028- void ReplyBuf ( Str_t sResult , EHTTP_STATUS eStatus, bool bNeedHttpResponse , CSphVector<BYTE> & dData )
1018+ void ReplyBuf ( const HttpReplyTrait_t & tReply , CSphVector<BYTE> & dData )
10291019{
1030- if ( bNeedHttpResponse )
1031- HttpBuildReply ( dData, eStatus, sResult , false );
1020+ if ( tReply.m_bHtml )
1021+ {
1022+ HttpBuildReply ( tReply, dData );
1023+ }
10321024 else
10331025 {
10341026 dData.Resize ( 0 );
1035- dData.Append ( sResult );
1027+ dData.Append ( tReply. m_sBody );
10361028 }
10371029}
10381030
@@ -1109,25 +1101,29 @@ void HttpHandler_c::ReportError ( const char * sError, HttpErrorType_e eType, EH
11091101void HttpHandler_c::BuildReply ( const CSphString & sResult , EHTTP_STATUS eStatus )
11101102{
11111103 m_eHttpCode = eStatus;
1112- ReplyBuf ( FromStr ( sResult ), eStatus, m_bNeedHttpResponse, m_dData );
1104+ HttpReplyTrait_t tReply { eStatus, FromStr ( sResult ), m_bNeedHttpResponse };
1105+ ReplyBuf ( tReply, m_dData );
11131106}
11141107
11151108void HttpHandler_c::BuildReply ( const char * szResult, EHTTP_STATUS eStatus )
11161109{
11171110 m_eHttpCode = eStatus;
1118- ReplyBuf ( FromSz ( szResult ), eStatus, m_bNeedHttpResponse, m_dData );
1111+ HttpReplyTrait_t tReply { eStatus, FromSz ( szResult ), m_bNeedHttpResponse };
1112+ ReplyBuf ( tReply, m_dData );
11191113}
11201114
11211115void HttpHandler_c::BuildReply ( const StringBuilder_c & sResult , EHTTP_STATUS eStatus )
11221116{
11231117 m_eHttpCode = eStatus;
1124- ReplyBuf ( (Str_t)sResult , eStatus, m_bNeedHttpResponse, m_dData );
1118+ HttpReplyTrait_t tReply { eStatus, (Str_t)sResult , m_bNeedHttpResponse };
1119+ ReplyBuf ( tReply, m_dData );
11251120}
11261121
11271122void HttpHandler_c::BuildReply ( Str_t sResult , EHTTP_STATUS eStatus )
11281123{
11291124 m_eHttpCode = eStatus;
1130- ReplyBuf ( sResult , eStatus, m_bNeedHttpResponse, m_dData );
1125+ HttpReplyTrait_t tReply { eStatus, sResult , m_bNeedHttpResponse };
1126+ ReplyBuf ( tReply, m_dData );
11311127}
11321128
11331129// check whether given served index is exist and has requested type
@@ -2498,7 +2494,9 @@ void sphHttpErrorReply ( CSphVector<BYTE> & dData, EHTTP_STATUS eCode, const cha
24982494 JsonObj_c tErr;
24992495 tErr.AddStr ( " error" , szError );
25002496 CSphString sJsonError = tErr.AsString ();
2501- HttpBuildReply ( dData, eCode, FromStr (sJsonError ), false );
2497+
2498+ HttpReplyTrait_t tReply { eCode, FromStr ( sJsonError ) };
2499+ HttpBuildReply ( tReply, dData );
25022500}
25032501
25042502static void EncodePercolateMatchResult ( const PercolateMatchResult_t & tRes, const CSphFixedVector<int64_t > & dDocids, const CSphString & sIndex , JsonEscapedBuilder & tOut )
0 commit comments