Repository navigation
Expand file tree
/
Copy pathsearchdhttp.cpp
More file actions
678 lines (578 loc) · 17.8 KB
/
Copy pathsearchdhttp.cpp
File metadata and controls
678 lines (578 loc) · 17.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
//
// $Id$
//
//
// Copyright (c) 2001-2016, Andrew Aksyonoff
// Copyright (c) 2008-2016, Sphinx Technologies Inc
// All rights reserved
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License. You should have
// received a copy of the GPL license along with this program; if you
// did not, you can find it at http://www.gnu.org/
//
#include "sphinx.h"
#include "sphinxint.h"
#include "sphinxjson.h"
#include "http/http_parser.h"
#include "searchdaemon.h"
struct EscapeJsonString_t
{
static bool IsEscapeChar ( char c )
{
return ( c=='"' || c=='\\' || c=='/' || c=='\b' || c=='\n' || c=='\r' || c=='\t'|| c=='\f' );
}
static char GetEscapedChar ( char c )
{
switch ( c )
{
case '\b': return 'b';
case '\n': return 'n';
case '\r': return 'r';
case '\t': return 't';
case '\f': return 'f';
default: return c;
}
}
};
typedef SphStringBuilder_T<EscapeJsonString_t> CSphStringBuilderJson;
static void AppendJsonKey ( const char * sName, CSphStringBuilderJson & tOut )
{
tOut += "\"";
tOut += sName;
tOut += "\":";
}
static void EncodeResultJson ( const AggrResult_t & tRes, CSphStringBuilderJson & tOut )
{
const CSphRsetSchema & tSchema = tRes.m_tSchema;
CSphVector<BYTE> dTmp;
int iAttrsCount = tSchema.GetAttrsCount();
if ( iAttrsCount && sphIsSortStringInternal ( tSchema.GetAttr ( iAttrsCount-1 ).m_sName.cstr() ) )
{
for ( int i=iAttrsCount-1; i>=0 && sphIsSortStringInternal ( tSchema.GetAttr(i).m_sName.cstr() ); i-- )
iAttrsCount = i;
}
tOut += "{";
// column names
AppendJsonKey ( "attrs", tOut );
tOut += "[";
for ( int i=0; i<iAttrsCount; i++ )
tOut.Appendf ( "%s\"%s\"", ( i==0 ? "" : "," ), tSchema.GetAttr ( i ).m_sName.cstr() );
tOut += "],";
// attribute values
AppendJsonKey ( "matches", tOut );
tOut += "[";
for ( int iMatch=tRes.m_iOffset; iMatch<tRes.m_iOffset+tRes.m_iCount; iMatch++ )
{
tOut += ( iMatch==tRes.m_iOffset ? "[" : ",[" );
const CSphMatch & tMatch = tRes.m_dMatches [ iMatch ];
const char * sSep = "";
for ( int iAttr=0; iAttr<iAttrsCount; iAttr++ )
{
tOut += sSep;
CSphAttrLocator tLoc = tSchema.GetAttr(iAttr).m_tLocator;
ESphAttr eAttrType = tSchema.GetAttr(iAttr).m_eAttrType;
switch ( eAttrType )
{
case SPH_ATTR_FLOAT:
tOut.Appendf ( "%f", tMatch.GetAttrFloat(tLoc) );
break;
case SPH_ATTR_INT64SET:
case SPH_ATTR_UINT32SET:
{
tOut += "[";
assert ( tMatch.GetAttr ( tLoc )==0 || tRes.m_dTag2Pools [ tMatch.m_iTag ].m_pMva || ( MVA_DOWNSIZE ( tMatch.GetAttr ( tLoc ) ) & MVA_ARENA_FLAG ) );
const PoolPtrs_t & tPools = tRes.m_dTag2Pools [ tMatch.m_iTag ];
const DWORD * pValues = tMatch.GetAttrMVA ( tLoc, tPools.m_pMva, tPools.m_bArenaProhibit );
const char * sSep = "";
if ( pValues )
{
DWORD nValues = *pValues++;
assert ( eAttrType==SPH_ATTR_UINT32SET || ( nValues%2 )==0 );
bool bWide = ( eAttrType==SPH_ATTR_INT64SET );
int iStep = ( bWide ? 2 : 1 );
for ( ; nValues; nValues-=iStep, pValues+=iStep )
{
int64_t iVal = ( bWide ? MVA_UPSIZE ( pValues ) : *pValues );
tOut.Appendf ( "%s" INT64_FMT, sSep, iVal );
sSep = ",";
}
}
tOut += "]";
}
break;
case SPH_ATTR_STRING:
case SPH_ATTR_JSON:
{
const BYTE * pStrings = tRes.m_dTag2Pools [ tMatch.m_iTag ].m_pStrings;
bool bStr = ( eAttrType==SPH_ATTR_STRING );
// get that string
const BYTE * pStr = NULL;
int iLen = 0;
DWORD uOffset = (DWORD) tMatch.GetAttr ( tLoc );
assert ( !uOffset || pStrings );
if ( uOffset )
iLen = sphUnpackStr ( pStrings+uOffset, &pStr );
if ( eAttrType==SPH_ATTR_JSON )
{
// no object at all? return NULL
if ( !pStr )
{
tOut += "null";
break;
}
dTmp.Resize ( 0 );
sphJsonFormat ( dTmp, pStr );
if ( !dTmp.GetLength() )
{
// empty string (no objects) - return NULL
// (canonical "{}" and "[]" are handled by sphJsonFormat)
tOut += "null";
break;
}
} else
{
dTmp.Resize ( iLen );
memcpy ( dTmp.Begin(), pStr, iLen );
}
dTmp.Add ( '\0' );
if ( bStr )
{
tOut += "\"";
tOut.AppendEscaped ( (const char *)dTmp.Begin(), true, false );
tOut += "\"";
} else
{
tOut += (const char *)dTmp.Begin();
}
}
break;
case SPH_ATTR_STRINGPTR:
{
const char * pString = (const char *)tMatch.GetAttr ( tLoc );
int iLen = pString && *pString ? strlen ( pString ) : 0;
dTmp.Resize ( iLen+1 );
memcpy ( dTmp.Begin(), pString, iLen );
dTmp[iLen] = '\0';
tOut += "\"";
tOut.AppendEscaped ( ( const char *)dTmp.Begin(), true, false );
tOut += "\"";
}
break;
case SPH_ATTR_FACTORS:
case SPH_ATTR_FACTORS_JSON:
{
const unsigned int * pFactors = (unsigned int *)tMatch.GetAttr ( tLoc );
if ( pFactors )
{
bool bStr = ( eAttrType==SPH_ATTR_FACTORS );
dTmp.Resize ( 0 );
sphFormatFactors ( dTmp, pFactors, !bStr );
dTmp.Add ( '\0' );
if ( bStr )
{
tOut += "\"";
tOut.AppendEscaped ( (const char *)dTmp.Begin(), true, false );
tOut += "\"";
} else
{
tOut += (const char *)dTmp.Begin();
}
}
}
break;
case SPH_ATTR_JSON_FIELD:
{
uint64_t uTypeOffset = tMatch.GetAttr ( tLoc );
ESphJsonType eJson = ESphJsonType ( uTypeOffset>>32 );
DWORD uOff = (DWORD)uTypeOffset;
if ( !uOff || eJson==JSON_NULL )
{
// no key found - NULL value
tOut += "null";
} else
{
// send string to client
dTmp.Resize ( 0 );
const BYTE * pStrings = tRes.m_dTag2Pools [ tMatch.m_iTag ].m_pStrings;
sphJsonFieldFormat ( dTmp, pStrings+uOff, eJson, false );
dTmp.Add ( '\0' );
tOut += (const char *)dTmp.Begin();
}
}
break;
case SPH_ATTR_INTEGER:
case SPH_ATTR_TIMESTAMP:
case SPH_ATTR_BOOL:
case SPH_ATTR_TOKENCOUNT:
case SPH_ATTR_BIGINT:
default:
tOut.Appendf ( INT64_FMT, tMatch.GetAttr(tLoc) );
}
sSep = ",";
}
tOut += "]";
}
tOut += "],";
// meta information
AppendJsonKey ( "meta", tOut );
tOut += "{";
tOut.Appendf ( "\"total\":%d, \"total_found\":" INT64_FMT ", \"time\":%d.%03d,", tRes.m_iMatches, tRes.m_iTotalMatches, tRes.m_iQueryTime/1000, tRes.m_iQueryTime%1000 );
// word statistics
AppendJsonKey ( "words", tOut );
tOut += "[";
tRes.m_hWordStats.IterateStart();
const char * sSep = "";
while ( tRes.m_hWordStats.IterateNext() )
{
const CSphQueryResultMeta::WordStat_t & tStat = tRes.m_hWordStats.IterateGet();
tOut.Appendf ( "%s{\"word\":\"%s\", \"docs\":" INT64_FMT ", \"hits\":" INT64_FMT "}", sSep, tRes.m_hWordStats.IterateGetKey().cstr(), tStat.m_iDocs, tStat.m_iHits );
sSep = ",";
}
tOut += "]}";
if ( !tRes.m_sWarning.IsEmpty() )
tOut.Appendf ( ",\"warning\":\"%s\"", tRes.m_sWarning.cstr() );
tOut += "}";
}
enum ESphHttpStatus
{
SPH_HTTP_STATUS_200,
SPH_HTTP_STATUS_206,
SPH_HTTP_STATUS_400,
SPH_HTTP_STATUS_500,
SPH_HTTP_STATUS_501,
SPH_HTTP_STATUS_TOTAL
};
const char * g_dHttpStatus[] = { "200 OK", "206 Partial Content", "400 Bad Request", "500 Internal Server Error", "501 Not Implemented" };
STATIC_ASSERT ( sizeof(g_dHttpStatus)/sizeof(g_dHttpStatus[0])==SPH_HTTP_STATUS_TOTAL, SPH_HTTP_STATUS_SHOULD_BE_SAME_AS_SPH_HTTP_STATUS_TOTAL );
static void HttpBuildReply ( CSphVector<BYTE> & dData, ESphHttpStatus eCode, const char * sBody, int iBodyLen, bool bHtml )
{
assert ( sBody && iBodyLen );
const char * sContent = ( bHtml ? "text/html" : "application/json" );
CSphString sHttp;
sHttp.SetSprintf ( "HTTP/1.1 %s\r\nServer: %s\r\nContent-Type: %s; charset=UTF-8\r\nContent-Length:%d\r\n\r\n", g_dHttpStatus[eCode], SPHINX_VERSION, sContent, iBodyLen );
int iHeaderLen = sHttp.Length();
dData.Resize ( iHeaderLen + iBodyLen );
memcpy ( dData.Begin(), sHttp.cstr(), iHeaderLen );
memcpy ( dData.Begin() + iHeaderLen, sBody, iBodyLen );
}
static void HttpErrorReply ( CSphVector<BYTE> & dData, ESphHttpStatus eCode, const char * sTemplate, ... )
{
char sBuf[1024] = "{}";
char sErr[1008] = "\0";
if ( sTemplate && *sTemplate!='\0' )
{
va_list ap;
va_start ( ap, sTemplate );
vsnprintf ( sErr, sizeof(sErr), sTemplate, ap );
va_end ( ap );
snprintf ( sBuf, sizeof(sBuf), "{\"error\":\"%s\"}", sErr );
}
HttpBuildReply ( dData, eCode, sBuf, strnlen ( sBuf, sizeof(sBuf) ), false );
}
enum ESphHttpEndpoint
{
SPH_HTTP_ENDPOINT_INDEX,
SPH_HTTP_ENDPOINT_SEARCH,
SPH_HTTP_ENDPOINT_SQL,
SPH_HTTP_ENDPOINT_TOTAL,
SPH_HTTP_ENDPOINT_MISSED,
SPH_HTTP_ENDPOINT_DEFAULT = SPH_HTTP_ENDPOINT_MISSED
};
const char * g_sEndpoints[] = { "index.html", "search", "sql" };
STATIC_ASSERT ( sizeof(g_sEndpoints)/sizeof(g_sEndpoints[0])==SPH_HTTP_ENDPOINT_TOTAL, SPH_HTTP_ENDPOINT_SHOULD_BE_SAME_AS_SPH_HTTP_ENDPOINT_TOTAL );
struct HttpRequestParser_t : public ISphNoncopyable
{
SmallStringHash_T<CSphString> m_hOptions;
ESphHttpEndpoint m_eEndpoint;
bool m_bKeepAlive;
CSphString m_sCurField;
const char * m_sError;
CSphString m_sInvalidEndpoint;
HttpRequestParser_t ();
~HttpRequestParser_t () {}
bool Parse ( const CSphVector<BYTE> & dData );
bool ParseList ( const char * sAt, int iLen );
static int ParserUrl ( http_parser * pParser, const char * sAt, size_t iLen );
static int ParserHeaderField ( http_parser * pParser, const char * sAt, size_t iLen );
static int ParserHeaderValue ( http_parser * pParser, const char * sAt, size_t iLen );
static int ParserBody ( http_parser * pParser, const char * sAt, size_t iLen );
};
HttpRequestParser_t::HttpRequestParser_t ()
{
m_eEndpoint = SPH_HTTP_ENDPOINT_DEFAULT;
m_bKeepAlive = false;
m_sError = NULL;
}
bool HttpRequestParser_t::Parse ( const CSphVector<BYTE> & dData )
{
http_parser_settings tParserSettings;
http_parser_settings_init ( &tParserSettings );
tParserSettings.on_url = HttpRequestParser_t::ParserUrl;
tParserSettings.on_header_field = HttpRequestParser_t::ParserHeaderField;
tParserSettings.on_header_value = HttpRequestParser_t::ParserHeaderValue;
tParserSettings.on_body = HttpRequestParser_t::ParserBody;
http_parser tParser;
http_parser_init ( &tParser, HTTP_REQUEST );
tParser.data = this;
int iParsed = http_parser_execute ( &tParser, &tParserSettings, (const char *)dData.Begin(), dData.GetLength() );
if ( iParsed!=dData.GetLength() )
{
m_sError = http_errno_description ( (http_errno)tParser.http_errno );
return false;
}
// connection wide http options
m_bKeepAlive = ( http_should_keep_alive ( &tParser )!=0 );
return true;
}
static BYTE Char2Hex ( BYTE uChar )
{
if ( uChar>=0x41 && uChar<=0x46 )
return ( uChar - 'A' ) + 10;
else if ( uChar>=0x61 && uChar<=0x66 )
return ( uChar - 'a' ) + 10;
else
return uChar - '0';
}
static void UriPercentReplace ( CSphString & sEntity )
{
if ( sEntity.IsEmpty() )
return;
char * pDst = (char *)sEntity.cstr();
const char * pSrc = pDst;
while ( *pSrc )
{
if ( *pSrc=='%' && *(pSrc+1) && *(pSrc+2) )
{
BYTE uCode = Char2Hex ( *(pSrc+1) ) * 16 + Char2Hex ( *(pSrc+2) );
pSrc += 3;
*pDst++ = uCode;
} else
{
*pDst++ = ( *pSrc=='+' ? ' ' : *pSrc );
pSrc++;
}
}
*pDst = '\0';
}
bool HttpRequestParser_t::ParseList ( const char * sAt, int iLen )
{
const char * sCur = sAt;
const char * sEnd = sAt + iLen;
const char * sLast = sCur;
CSphString sName;
CSphString sVal;
for ( ; sCur<sEnd; sCur++ )
{
if ( *sCur!='&' && *sCur!='=' )
continue;
int iLen = sCur - sLast;
if ( *sCur=='&' )
{
sVal.SetBinary ( sLast, iLen );
UriPercentReplace ( sName );
UriPercentReplace ( sVal );
m_hOptions.Add ( sVal, sName );
sName = "";
sVal = "";
} else
{
sName.SetBinary ( sLast, iLen );
}
sLast = sCur+1;
}
if ( !sName.IsEmpty() )
{
sVal.SetBinary ( sLast, sCur - sLast );
UriPercentReplace ( sName );
UriPercentReplace ( sVal );
m_hOptions.Add ( sVal, sName );
}
return true;
}
static ESphHttpEndpoint ParseEndpointPath ( const char * sPath, int iLen )
{
if ( !iLen )
return SPH_HTTP_ENDPOINT_INDEX;
for ( int i=0; i<SPH_HTTP_ENDPOINT_TOTAL; i++ )
{
if ( strncmp ( sPath, g_sEndpoints[i], Min ( iLen, (int)strlen ( g_sEndpoints[i] ) ) )==0 )
return (ESphHttpEndpoint)i;
}
return SPH_HTTP_ENDPOINT_MISSED;
}
int HttpRequestParser_t::ParserUrl ( http_parser * pParser, const char * sAt, size_t iLen )
{
http_parser_url tUri;
if ( http_parser_parse_url ( sAt, iLen, 0, &tUri ) )
return 0;
DWORD uPath = ( 1UL<<UF_PATH );
DWORD uQuery = ( 1UL<<UF_QUERY );
if ( ( tUri.field_set & uPath )!=0 )
{
const char * sPath = sAt + tUri.field_data[UF_PATH].off;
int iLen = tUri.field_data[UF_PATH].len;
if ( *sPath=='/' )
{
sPath++;
iLen--;
}
ESphHttpEndpoint eEndpoint = ParseEndpointPath ( sPath, iLen );
( (HttpRequestParser_t *)pParser->data )->m_eEndpoint = eEndpoint;
if ( eEndpoint==SPH_HTTP_ENDPOINT_MISSED )
{
( (HttpRequestParser_t *)pParser->data )->m_sInvalidEndpoint.SetBinary ( sPath, iLen );
}
}
if ( ( tUri.field_set & uQuery )!=0 )
( (HttpRequestParser_t *)pParser->data )->ParseList ( sAt + tUri.field_data[UF_QUERY].off, tUri.field_data[UF_QUERY].len );
return 0;
}
int HttpRequestParser_t::ParserHeaderField ( http_parser * pParser, const char * sAt, size_t iLen )
{
assert ( pParser->data );
( (HttpRequestParser_t *)pParser->data )->m_sCurField.SetBinary ( sAt, iLen );
return 0;
}
int HttpRequestParser_t::ParserHeaderValue ( http_parser * pParser, const char * sAt, size_t iLen )
{
assert ( pParser->data );
CSphString sVal;
sVal.SetBinary ( sAt, iLen );
HttpRequestParser_t * pHttpParser = (HttpRequestParser_t *)pParser->data;
pHttpParser->m_hOptions.Add ( sVal, pHttpParser->m_sCurField );
pHttpParser->m_sCurField = "";
return 0;
}
int HttpRequestParser_t::ParserBody ( http_parser * pParser, const char * sAt, size_t iLen )
{
assert ( pParser->data );
( (HttpRequestParser_t *)pParser->data )->ParseList ( sAt, iLen );
return 0;
}
static const CSphString * GetAnyValue ( const SmallStringHash_T<CSphString> & hOptions, const char * sKey1, const char * sKey2 )
{
const CSphString * pVal1 = hOptions ( sKey1 );
return pVal1 ? pVal1 : hOptions ( sKey2 );
}
static void ParseSearchOptions ( const SmallStringHash_T<CSphString> & hOptions, CSphQuery & tQuery )
{
const CSphString * pMatch = hOptions ( "match" );
if ( pMatch )
tQuery.m_sQuery = *pMatch;
const CSphString * pIdx = GetAnyValue ( hOptions, "index", "indexes" );
if ( pIdx )
tQuery.m_sIndexes = *pIdx;
const CSphString * pSel = GetAnyValue ( hOptions, "select", "select_list" );
if ( pSel )
tQuery.m_sSelect = *pSel;
const CSphString * pGroup = GetAnyValue ( hOptions, "group", "group_by" );
if ( pGroup )
tQuery.m_sGroupBy = *pGroup;
const CSphString * pOrder = GetAnyValue ( hOptions, "order", "order_by" );
if ( pOrder )
tQuery.m_sSortBy = *pOrder;
const CSphString * pLimit = hOptions ( "limit" );
if ( pLimit )
tQuery.m_iLimit = atoi ( pLimit->cstr() );
}
static const char * g_sIndexPage =
"<!DOCTYPE html>"
"<html>"
"<head>"
"<title>Sphinx</title>"
"</head>"
"<body>"
"<h1>Sphinx daemon</h1>"
"<p>%s</p>"
"</body>"
"</html>";
static void HttpHandlerPage ( bool bPage, const CSphString & sInvalidEndpoint, CSphVector<BYTE> & dData )
{
if ( bPage )
{
CSphStringBuilder sIndexPage;
sIndexPage.Appendf ( g_sIndexPage, SPHINX_VERSION );
HttpBuildReply ( dData, SPH_HTTP_STATUS_200, sIndexPage.cstr(), sIndexPage.Length(), true );
} else
{
HttpErrorReply ( dData, SPH_HTTP_STATUS_501, "/%s - unsupported endpoint", sInvalidEndpoint.cstr() );
}
}
static void HttpHandlerSearch ( bool bQL, const SmallStringHash_T<CSphString> & hOptions, int iCID, CSphVector<BYTE> & dData )
{
CSphScopedPtr<ISphSearchHandler> tHandler ( sphCreateSearchHandler ( 1, true, true, iCID ) );
CSphQuery * pQuery = tHandler->GetQuery ( 0 );
if ( bQL )
{
const CSphString * pRawQl = hOptions ( "query" );
if ( !pRawQl || pRawQl->IsEmpty() )
{
HttpErrorReply ( dData, SPH_HTTP_STATUS_400, "query missed" );
return;
}
CSphString sError;
CSphVector<SqlStmt_t> dStmt;
if ( !sphParseSqlQuery ( pRawQl->cstr(), pRawQl->Length(), dStmt, sError, SPH_COLLATION_DEFAULT ) )
{
HttpErrorReply ( dData, SPH_HTTP_STATUS_400, sError.cstr() );
return;
}
*pQuery = dStmt[0].m_tQuery;
if ( dStmt.GetLength()>1 )
{
HttpErrorReply ( dData, SPH_HTTP_STATUS_501, "multiple queries not supported" );
return;
} else if ( dStmt[0].m_eStmt!=STMT_SELECT )
{
HttpErrorReply ( dData, SPH_HTTP_STATUS_501, "only SELECT queries supported" );
return;
}
} else
{
CSphString sError;
ParseSearchOptions ( hOptions, *pQuery );
if ( !pQuery->ParseSelectList ( sError ) )
{
HttpErrorReply ( dData, SPH_HTTP_STATUS_400, sError.cstr() );
return;
}
if ( !pQuery->m_sSortBy.IsEmpty() )
pQuery->m_eSort = SPH_SORT_EXTENDED;
}
// search
tHandler->RunQueries();
AggrResult_t * pRes = tHandler->GetResult ( 0 );
if ( !pRes->m_sError.IsEmpty() )
{
HttpErrorReply ( dData, SPH_HTTP_STATUS_500, pRes->m_sError.cstr() );
return;
}
// coping result set
CSphStringBuilderJson tResBuilder;
EncodeResultJson ( *pRes, tResBuilder );
HttpBuildReply ( dData, SPH_HTTP_STATUS_200, tResBuilder.cstr(), tResBuilder.Length(), false );
}
bool sphLoopClientHttp ( CSphVector<BYTE> & dData, int iCID )
{
HttpRequestParser_t tParser;
if ( !tParser.Parse ( dData ) )
{
HttpErrorReply ( dData, SPH_HTTP_STATUS_400, tParser.m_sError );
return tParser.m_bKeepAlive;
}
switch ( tParser.m_eEndpoint )
{
case SPH_HTTP_ENDPOINT_SEARCH:
case SPH_HTTP_ENDPOINT_SQL:
HttpHandlerSearch ( tParser.m_eEndpoint==SPH_HTTP_ENDPOINT_SQL, tParser.m_hOptions, iCID, dData );
break;
case SPH_HTTP_ENDPOINT_INDEX:
HttpHandlerPage ( true, tParser.m_sInvalidEndpoint, dData );
break;
case SPH_HTTP_ENDPOINT_MISSED:
default:
HttpHandlerPage ( false, tParser.m_sInvalidEndpoint, dData );
}
return tParser.m_bKeepAlive;
}