-
Notifications
You must be signed in to change notification settings - Fork 16
/
solr_functions_client.c
executable file
·523 lines (413 loc) · 17.2 KB
/
solr_functions_client.c
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
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2009 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Israel Ekpo <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id: solr_functions_client.c 311801 2011-06-04 08:36:36Z iekpo $ */
#include "php_solr.h"
/* {{{ PHP_SOLR_API int solr_init_options(solr_client_options_t *options TSRMLS_DC) */
PHP_SOLR_API int solr_init_options(solr_client_options_t *options TSRMLS_DC)
{
solr_string_init(&(options->hostname));
solr_string_init(&(options->path));
solr_string_init(&(options->http_auth_credentials));
solr_string_init(&(options->proxy_hostname));
solr_string_init(&(options->proxy_auth_credentials));
solr_string_init(&(options->ssl_cert));
solr_string_init(&(options->ssl_key));
solr_string_init(&(options->ssl_keypassword));
solr_string_init(&(options->ssl_cainfo));
solr_string_init(&(options->ssl_capath));
solr_string_init(&(options->qs_delimiter));
solr_string_init(&(options->response_writer));
solr_string_init(&(options->update_url));
solr_string_init(&(options->search_url));
solr_string_init(&(options->thread_url));
solr_string_init(&(options->ping_url));
solr_string_init(&(options->terms_url));
solr_string_init(&(options->update_servlet));
solr_string_init(&(options->search_servlet));
solr_string_init(&(options->thread_servlet));
solr_string_init(&(options->ping_servlet));
solr_string_init(&(options->terms_servlet));
return SUCCESS;
}
/* }}} */
/* {{{ static void solr_set_initial_handle_options(solr_curl_t **sch, solr_client_options_t *options TSRMLS_DC) */
static void solr_set_initial_curl_handle_options(solr_curl_t **sch_ptr, solr_client_options_t *options TSRMLS_DC)
{
solr_curl_t * sch = *sch_ptr;
/** Setup all the required CURL options here **/
curl_easy_setopt(sch->curl_handle, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(sch->curl_handle, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(sch->curl_handle, CURLOPT_ERRORBUFFER, sch->err.str);
curl_easy_setopt(sch->curl_handle, CURLOPT_WRITEFUNCTION, solr_curl_write);
curl_easy_setopt(sch->curl_handle, CURLOPT_WRITEDATA, (void *) sch);
curl_easy_setopt(sch->curl_handle, CURLOPT_HEADERFUNCTION, solr_curl_write_header);
curl_easy_setopt(sch->curl_handle, CURLOPT_HEADERDATA, (void *) sch);
curl_easy_setopt(sch->curl_handle, CURLOPT_DEBUGFUNCTION, solr_curl_debug_callback);
curl_easy_setopt(sch->curl_handle, CURLOPT_DEBUGDATA, (void *) sch);
curl_easy_setopt(sch->curl_handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
/**
#if LIBCURL_VERSION_NUM >= 0x071304
curl_easy_setopt(sch->curl_handle, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
#endif
*/
curl_easy_setopt(sch->curl_handle, CURLOPT_DNS_CACHE_TIMEOUT, 120L);
curl_easy_setopt(sch->curl_handle, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(sch->curl_handle, CURLOPT_MAXREDIRS, 16L); /* prevent infinite redirects */
curl_easy_setopt(sch->curl_handle, CURLOPT_UNRESTRICTED_AUTH, 0L);
#ifdef ZTS
curl_easy_setopt(sch->curl_handle, CURLOPT_NOSIGNAL, 1L); /** Needed in multi-threaded environments **/
#endif
curl_easy_setopt(sch->curl_handle, CURLOPT_TIMEOUT, options->timeout);
curl_easy_setopt(sch->curl_handle, CURLOPT_USERAGENT, SOLR_CLIENT_USER_AGENT);
/* If the login and password options are passed, send the authentication headers */
if (options->http_auth_credentials.len) {
curl_easy_setopt(sch->curl_handle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_easy_setopt(sch->curl_handle, CURLOPT_USERPWD, options->http_auth_credentials.str);
}
/* If there is proxy info, send it too */
if (options->proxy_hostname.len) {
curl_easy_setopt(sch->curl_handle, CURLOPT_PROXY, options->proxy_hostname.str);
if (options->proxy_port) {
curl_easy_setopt(sch->curl_handle, CURLOPT_PROXYPORT, options->proxy_port);
}
if (options->proxy_auth_credentials.len) {
curl_easy_setopt(sch->curl_handle, CURLOPT_PROXYUSERPWD, options->proxy_auth_credentials.str);
}
}
/*
* When negotiating an SSL connection, the server sends a certificate indicating its identity.
* cURL verifies whether the certificate is authentic
*/
if (options->secure) {
/* The name of the PEM-formatted private key and private certificate concatenated */
if (options->ssl_cert.len) {
curl_easy_setopt(sch->curl_handle, CURLOPT_SSLCERT, options->ssl_cert.str);
curl_easy_setopt(sch->curl_handle, CURLOPT_SSLCERTTYPE, "PEM");
}
/* The name of the PEM-formatted private key, if certificate and private key are separate */
if (options->ssl_key.len) {
curl_easy_setopt(sch->curl_handle, CURLOPT_SSLKEY, options->ssl_key.str);
curl_easy_setopt(sch->curl_handle, CURLOPT_SSLKEYTYPE, "PEM");
}
/* Password for the PEM-formatted private key */
if (options->ssl_keypassword.len) {
curl_easy_setopt(sch->curl_handle, CURLOPT_KEYPASSWD, options->ssl_keypassword.str);
}
/* The file holding one or more CA certificates to verify the peer with */
if (options->ssl_cainfo.len) {
curl_easy_setopt(sch->curl_handle, CURLOPT_CAINFO, options->ssl_cainfo.str);
}
/* The directory holding multiple CA certificates to verify the peer with */
if (options->ssl_capath.len) {
curl_easy_setopt(sch->curl_handle, CURLOPT_CAPATH , options->ssl_capath.str);
}
/*
* Whether curl verifies the authenticity of the host name of server
* Based on the Common name section of the certificate
*/
if (options->ssl_verify_peer && options->ssl_verify_host) {
curl_easy_setopt(sch->curl_handle, CURLOPT_SSL_VERIFYHOST, options->ssl_verify_host);
}
/*
* Verify the authenticity of the peer's certificate
* This authentication is based on a chain of digital signatures,
* rooted in certification authority (CA) certificates.
*
* If something is not right, the connection will not be valid
*/
curl_easy_setopt(sch->curl_handle, CURLOPT_SSL_VERIFYPEER, options->ssl_verify_peer);
}
}
/* }}} */
/* {{{ PHP_SOLR_API int solr_init_handle(solr_curl_t *sch, solr_client_options_t *options TSRMLS_DC) */
PHP_SOLR_API int solr_init_handle(solr_curl_t *sch, solr_client_options_t *options TSRMLS_DC)
{
sch->response_header.response_code = 0L;
memset(sch->err.str, 0, sizeof(sch->err.str));
sch->curl_handle = curl_easy_init();
if (NULL == sch->curl_handle) {
return FAILURE;
}
sch->result_code = CURLE_OK;
sch->handle_status = 1;
#ifdef ZTS
sch->tsrm_ls = TSRMLS_C;
#endif
solr_string_init(&(sch->request_header.buffer));
solr_string_init(&(sch->request_body.buffer));
solr_string_init(&(sch->request_body_debug.buffer));
solr_string_init(&(sch->response_header.buffer));
solr_string_init(&(sch->response_body.buffer));
solr_string_init(&(sch->debug_data_buffer));
solr_set_initial_curl_handle_options(&(sch), options TSRMLS_CC);
return SUCCESS;
}
/* }}} */
/**
* solr_curl_write
*
* Called by libcurl as soon as there is response body data received that needs to be saved.
*
* The size of the data pointed to by *data is size multiplied with nmemb, it will not be zero terminated
*
* The *ctx parameter is the solr_curl pointer
*/
/* {{{ size_t solr_curl_write(solr_char_t *data, size_t size, size_t nmemb, void *ctx) */
size_t solr_curl_write(solr_char_t *data, size_t size, size_t nmemb, void *ctx)
{
size_t length = (size * nmemb);
solr_curl_t *sch = (solr_curl_t *) ctx;
solr_string_appends(&(sch->response_body.buffer), data, length);
return length;
}
/* }}} */
/**
* solr_curl_write_header
*
* Called by libcurl as soon as it has received header data
*
* It will be called once for each header and only complete header lines are passed on to the callback.
*
* Note: header lines may not be null-terminated so beware.
*
* The *ctx parameter is the solr_curl pointer
*/
/* {{{ size_t solr_curl_write_header(solr_char_t *data, size_t size, size_t nmemb, void *ctx) */
size_t solr_curl_write_header(solr_char_t *data, size_t size, size_t nmemb, void *ctx)
{
size_t length = (size * nmemb);
solr_curl_t *sch = (solr_curl_t *) ctx;
solr_string_appends(&(sch->response_header.buffer), data, length);
return length;
}
/* }}} */
/**
* solr_curl_debug_callback
*
* The infotype parameter is one of :
*
* 0 = CURLINFO_TEXT
* 1 = CURLINFO_HEADER_IN (Response Headers)
* 2 = CURLINFO_HEADER_OUT (Request Headers)
* 3 = CURLINFO_DATA_IN (Response Body)
* 4 = CURLINFO_DATA_OUT (Request Body)
* 5 = CURLINFO_SSL_DATA_IN
* 6 = CURLINFO_SSL_DATA_OUT
*
* The debug_data parameter is not null-terminated but is exactly size characters
*
* The ctx parameter is a solr_curl * pointer
*/
/* {{{ int solr_curl_debug_callback(CURL *curl_handle, curl_infotype infotype, solr_char_t *debug_data, size_t size, void *ctx) */
int solr_curl_debug_callback(CURL *curl_handle, curl_infotype infotype, solr_char_t *debug_data, size_t size, void *ctx)
{
solr_curl_t *sch = (solr_curl_t *) ctx;
/* TODO : Check why this function is still called after destructor has been called */
if (!sch->handle_status)
{
return 0;
}
switch(infotype)
{
case CURLINFO_HEADER_OUT : /* Capture the Actual Request Headers Sent to Server */
{
solr_string_appends(&(sch->request_header.buffer), debug_data, size);
}
break;
case CURLINFO_DATA_OUT : /* Capture the Actual Request Body Sent to Server */
{
solr_string_appends(&(sch->request_body_debug.buffer), debug_data, size);
}
break;
default :
{
/* do nothing */
}
break;
}
/* Captures ALL debug information */
solr_string_appends(&(sch->debug_data_buffer), debug_data, size);
return 0;
}
/* }}} */
/* {{{ PHP_SOLR_API int solr_make_request(solr_client_t *client, solr_request_type_t request_type TSRMLS_DC) */
PHP_SOLR_API int solr_make_request(solr_client_t *client, solr_request_type_t request_type TSRMLS_DC)
{
solr_curl_t *sch = &(client->handle);
solr_client_options_t *options = &(client->options);
solr_http_header_list_t *header_list = NULL;
int return_status = SUCCESS;
CURLcode info_status = CURLE_OK;
header_list = curl_slist_append(header_list, "Accept-Charset: utf-8");
header_list = curl_slist_append(header_list, "Keep-Alive: 300");
header_list = curl_slist_append(header_list, "Connection: keep-alive");
/* Disable the Expect: 100-continue header. Jetty gets confused with this header */
header_list = curl_slist_append(header_list, "Expect:");
/* Reset the buffers */
solr_string_free(&sch->request_header.buffer);
solr_string_free(&sch->request_body_debug.buffer);
solr_string_free(&sch->response_body.buffer);
solr_string_free(&sch->response_header.buffer);
solr_string_free(&sch->debug_data_buffer);
curl_easy_reset(sch->curl_handle);
solr_set_initial_curl_handle_options(&(sch), options TSRMLS_CC);
/* Reset the CURL options if the handle is reused */
curl_easy_setopt(sch->curl_handle, CURLOPT_HEADER, 0L);
curl_easy_setopt(sch->curl_handle, CURLOPT_POST, 0L);
curl_easy_setopt(sch->curl_handle, CURLOPT_HTTPGET, 0L);
curl_easy_setopt(sch->curl_handle, CURLOPT_NOBODY, 0L);
curl_easy_setopt(sch->curl_handle, CURLOPT_POSTFIELDSIZE, 0L);
curl_easy_setopt(sch->curl_handle, CURLOPT_POSTFIELDS, NULL);
curl_easy_setopt(sch->curl_handle, CURLOPT_URL, NULL);
curl_easy_setopt(sch->curl_handle, CURLOPT_HTTPHEADER, NULL);
switch(request_type)
{
case SOLR_REQUEST_SEARCH : /* HTTP FORM POST */
{
header_list = curl_slist_append(header_list, "Content-Type: application/x-www-form-urlencoded;charset=UTF-8");
curl_easy_setopt(sch->curl_handle, CURLOPT_POST, 1L);
curl_easy_setopt(sch->curl_handle, CURLOPT_URL, options->search_url.str);
curl_easy_setopt(sch->curl_handle, CURLOPT_HTTPHEADER, header_list);
curl_easy_setopt(sch->curl_handle, CURLOPT_POSTFIELDSIZE, sch->request_body.buffer.len);
curl_easy_setopt(sch->curl_handle, CURLOPT_POSTFIELDS, sch->request_body.buffer.str);
}
break;
case SOLR_REQUEST_TERMS : /* HTTP FORM POST */
{
header_list = curl_slist_append(header_list, "Content-Type: application/x-www-form-urlencoded;charset=UTF-8");
curl_easy_setopt(sch->curl_handle, CURLOPT_POST, 1L);
curl_easy_setopt(sch->curl_handle, CURLOPT_URL, options->terms_url.str);
curl_easy_setopt(sch->curl_handle, CURLOPT_HTTPHEADER, header_list);
curl_easy_setopt(sch->curl_handle, CURLOPT_POSTFIELDSIZE, sch->request_body.buffer.len);
curl_easy_setopt(sch->curl_handle, CURLOPT_POSTFIELDS, sch->request_body.buffer.str);
}
break;
case SOLR_REQUEST_UPDATE : /* HTTP XML POST */
{
header_list = curl_slist_append(header_list, "Content-Type: text/xml;charset=UTF-8");
curl_easy_setopt(sch->curl_handle, CURLOPT_POST, 1L);
curl_easy_setopt(sch->curl_handle, CURLOPT_URL, options->update_url.str);
curl_easy_setopt(sch->curl_handle, CURLOPT_HTTPHEADER, header_list);
curl_easy_setopt(sch->curl_handle, CURLOPT_POSTFIELDSIZE, sch->request_body.buffer.len);
curl_easy_setopt(sch->curl_handle, CURLOPT_POSTFIELDS, sch->request_body.buffer.str);
}
break;
case SOLR_REQUEST_THREADS : /* HTTP GET */
{
curl_easy_setopt(sch->curl_handle, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(sch->curl_handle, CURLOPT_URL, options->thread_url.str);
curl_easy_setopt(sch->curl_handle, CURLOPT_HTTPHEADER, header_list);
}
break;
case SOLR_REQUEST_PING : /* HTTP HEAD REQUEST. */
{
curl_easy_setopt(sch->curl_handle, CURLOPT_HEADER, 1L);
curl_easy_setopt(sch->curl_handle, CURLOPT_NOBODY, 1L);
curl_easy_setopt(sch->curl_handle, CURLOPT_URL, options->ping_url.str);
curl_easy_setopt(sch->curl_handle, CURLOPT_HTTPHEADER, header_list);
}
break;
default :
{
return_status = FAILURE;
}
break;
}
sch->result_code = curl_easy_perform(sch->curl_handle);
info_status = curl_easy_getinfo(sch->curl_handle, CURLINFO_RESPONSE_CODE, &(sch->response_header.response_code));
if (info_status != CURLE_OK)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "HTTP Transfer status could not be retrieved successfully");
return_status = FAILURE;
}
if (sch->result_code != CURLE_OK)
{
/* @TODO change this to an exception */
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Solr HTTP Error : '%s' ", curl_easy_strerror(sch->result_code));
return_status = FAILURE;
}
if (sch->response_header.response_code != 200L)
{
return_status = FAILURE;
}
curl_slist_free_all(header_list);
return return_status;
}
/* }}} */
/** Deallocates memory associated with the Solr CURL Handle. **/
/* {{{ PHP_SOLR_API void solr_free_handle(solr_curl_t *sch) */
PHP_SOLR_API void solr_free_handle(solr_curl_t *sch)
{
solr_string_free(&((sch)->request_header.buffer));
solr_string_free(&((sch)->request_body.buffer));
solr_string_free(&((sch)->request_body_debug.buffer));
solr_string_free(&((sch)->response_header.buffer));
solr_string_free(&((sch)->response_body.buffer));
solr_string_free(&((sch)->debug_data_buffer));
sch->handle_status = 0;
curl_easy_cleanup((sch)->curl_handle);
sch->curl_handle = NULL;
}
/* }}} */
/* {{{ PHP_SOLR_API void solr_free_options(solr_client_options_t *options) */
PHP_SOLR_API void solr_free_options(solr_client_options_t *options)
{
solr_string_free(&((options)->hostname));
solr_string_free(&((options)->http_auth_credentials));
solr_string_free(&((options)->path));
solr_string_free(&((options)->proxy_auth_credentials));
solr_string_free(&((options)->ssl_cert));
solr_string_free(&((options)->ssl_key));
solr_string_free(&((options)->ssl_keypassword));
solr_string_free(&((options)->ssl_cainfo));
solr_string_free(&((options)->ssl_capath));
solr_string_free(&((options)->proxy_hostname));
solr_string_free(&((options)->qs_delimiter));
solr_string_free(&((options)->response_writer));
solr_string_free(&((options)->update_url));
solr_string_free(&((options)->search_url));
solr_string_free(&((options)->thread_url));
solr_string_free(&((options)->ping_url));
solr_string_free(&((options)->terms_url));
solr_string_free(&((options)->update_servlet));
solr_string_free(&((options)->search_servlet));
solr_string_free(&((options)->thread_servlet));
solr_string_free(&((options)->ping_servlet));
solr_string_free(&((options)->terms_servlet));
}
/* }}} */
/* {{{ PHP_SOLR_API void solr_destroy_client(void *client) */
PHP_SOLR_API void solr_destroy_client(void *client)
{
solr_client_t *solr_client = (solr_client_t *) client;
if (solr_client)
{
solr_free_options(&(solr_client->options));
solr_free_handle(&(solr_client->handle));
solr_client = NULL;
}
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: fdm=marker
* vim: noet sw=4 ts=4
*/