|
| 1 | +/* |
| 2 | + +----------------------------------------------------------------------+ |
| 3 | + | APM stands for Alternative PHP Monitor | |
| 4 | + +----------------------------------------------------------------------+ |
| 5 | + | Copyright (c) 2011-2016 David Strauss | |
| 6 | + +----------------------------------------------------------------------+ |
| 7 | + | This source file is subject to version 3.01 of the PHP license, | |
| 8 | + | that is bundled with this package in the file LICENSE, and is | |
| 9 | + | available through the world-wide-web at the following url: | |
| 10 | + | http://www.php.net/license/3_01.txt | |
| 11 | + | If you did not receive a copy of the PHP license and are unable to | |
| 12 | + | obtain it through the world-wide-web, please send a note to | |
| 13 | + | license@php.net so we can mail you a copy immediately. | |
| 14 | + +----------------------------------------------------------------------+ |
| 15 | + | Authors: David Strauss <david@davidstrauss.net> | |
| 16 | + +----------------------------------------------------------------------+ |
| 17 | +*/ |
| 18 | + |
| 19 | +#include <stdio.h> |
| 20 | +#include <curl/curl.h> |
| 21 | +#include "php_apm.h" |
| 22 | +#include "php_ini.h" |
| 23 | + |
| 24 | +#include "driver_http.h" |
| 25 | + |
| 26 | +ZEND_EXTERN_MODULE_GLOBALS(apm) |
| 27 | + |
| 28 | +APM_DRIVER_CREATE(http) |
| 29 | + |
| 30 | +char *truncate_data(char *input_str, size_t max_len) |
| 31 | +{ |
| 32 | + char *truncated; |
| 33 | + input_str = input_str ? input_str : NULL; |
| 34 | + if (max_len == 0) |
| 35 | + return strdup(input_str); |
| 36 | + truncated = strndup(input_str, max_len); |
| 37 | + return truncated; |
| 38 | +} |
| 39 | + |
| 40 | +/* Insert an event in the backend */ |
| 41 | +void apm_driver_http_process_event(PROCESS_EVENT_ARGS) |
| 42 | +{ |
| 43 | + CURL *curl; |
| 44 | + CURLcode res; |
| 45 | + |
| 46 | + curl_global_init(CURL_GLOBAL_ALL); |
| 47 | + curl = curl_easy_init(); |
| 48 | + if(curl) { |
| 49 | + struct curl_httppost *formpost = NULL; |
| 50 | + struct curl_httppost *lastptr = NULL; |
| 51 | + struct curl_slist *headerlist = NULL; |
| 52 | + static const char buf[] = "Expect:"; |
| 53 | + char int2string[64]; |
| 54 | + char *trace_to_send; |
| 55 | + size_t max_len = 0; |
| 56 | + |
| 57 | + if (APM_G(http_max_backtrace_length) >= 0) |
| 58 | + max_len = APM_G(http_max_backtrace_length); |
| 59 | + |
| 60 | + trace_to_send = truncate_data(trace, max_len); |
| 61 | + |
| 62 | + sprintf(int2string, "%d", type); |
| 63 | + curl_formadd(&formpost, |
| 64 | + &lastptr, |
| 65 | + CURLFORM_COPYNAME, "type", |
| 66 | + CURLFORM_COPYCONTENTS, int2string, |
| 67 | + CURLFORM_END); |
| 68 | + |
| 69 | + curl_formadd(&formpost, |
| 70 | + &lastptr, |
| 71 | + CURLFORM_COPYNAME, "file", |
| 72 | + CURLFORM_COPYCONTENTS, error_filename ? error_filename : "", |
| 73 | + CURLFORM_END); |
| 74 | + |
| 75 | + sprintf(int2string, "%d", error_lineno); |
| 76 | + curl_formadd(&formpost, |
| 77 | + &lastptr, |
| 78 | + CURLFORM_COPYNAME, "line", |
| 79 | + CURLFORM_COPYCONTENTS, int2string, |
| 80 | + CURLFORM_END); |
| 81 | + |
| 82 | + curl_formadd(&formpost, |
| 83 | + &lastptr, |
| 84 | + CURLFORM_COPYNAME, "message", |
| 85 | + CURLFORM_COPYCONTENTS, msg ? msg : "", |
| 86 | + CURLFORM_END); |
| 87 | + |
| 88 | + curl_formadd(&formpost, |
| 89 | + &lastptr, |
| 90 | + CURLFORM_COPYNAME, "backtrace", |
| 91 | + CURLFORM_COPYCONTENTS, trace_to_send, |
| 92 | + CURLFORM_END); |
| 93 | + |
| 94 | + headerlist = curl_slist_append(headerlist, buf); |
| 95 | + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist); |
| 96 | + curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); |
| 97 | + |
| 98 | + curl_easy_setopt(curl, CURLOPT_URL, APM_G(http_server)); |
| 99 | + curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, APM_G(http_request_timeout)); |
| 100 | + if (APM_G(http_client_certificate) != NULL) { |
| 101 | + curl_easy_setopt(curl, CURLOPT_SSLCERT, APM_G(http_client_certificate)); |
| 102 | + } |
| 103 | + if (APM_G(http_client_key) != NULL) { |
| 104 | + curl_easy_setopt(curl, CURLOPT_SSLKEY, APM_G(http_client_key)); |
| 105 | + } |
| 106 | + if (APM_G(http_certificate_authorities) != NULL) { |
| 107 | + curl_easy_setopt(curl, CURLOPT_CAINFO, APM_G(http_certificate_authorities)); |
| 108 | + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L); |
| 109 | + } |
| 110 | + |
| 111 | + res = curl_easy_perform(curl); |
| 112 | + |
| 113 | + APM_DEBUG("[HTTP driver] Result: %s\n", curl_easy_strerror(res)); |
| 114 | + |
| 115 | + /* Always clean up. */ |
| 116 | + curl_easy_cleanup(curl); |
| 117 | + free(trace_to_send); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +int apm_driver_http_minit(int module_number) |
| 122 | +{ |
| 123 | + return SUCCESS; |
| 124 | +} |
| 125 | + |
| 126 | +int apm_driver_http_rinit() |
| 127 | +{ |
| 128 | + return SUCCESS; |
| 129 | +} |
| 130 | + |
| 131 | +int apm_driver_http_mshutdown() |
| 132 | +{ |
| 133 | + return SUCCESS; |
| 134 | +} |
| 135 | + |
| 136 | +int apm_driver_http_rshutdown() |
| 137 | +{ |
| 138 | + return SUCCESS; |
| 139 | +} |
| 140 | + |
| 141 | +void apm_driver_http_process_stats(TSRMLS_D) |
| 142 | +{ |
| 143 | +} |
0 commit comments