]> granicus.if.org Git - php/commitdiff
small refactoring - factour out code from connection establishment.
authorandrey <andrey@php.net>
Wed, 30 May 2012 12:24:17 +0000 (15:24 +0300)
committerandrey <andrey@php.net>
Wed, 30 May 2012 12:24:17 +0000 (15:24 +0300)
Add a define for the default authentication protocol - get away from
the hardcoded magic value

ext/mysqlnd/mysqlnd.c
ext/mysqlnd/mysqlnd_debug.h
ext/mysqlnd/mysqlnd_enum_n_def.h
ext/mysqlnd/mysqlnd_structs.h

index 300a7eea56dccd2e5375659bff8dae8ae0ce8aa6..fc0e46a5daa5d7e37163d167c4bd6225e660aeea 100644 (file)
@@ -605,6 +605,148 @@ end:
 /* }}} */
 
 
+/* {{{ mysqlnd_conn_data::execute_init_commands */
+static enum_func_status
+MYSQLND_METHOD(mysqlnd_conn_data, execute_init_commands)(MYSQLND_CONN_DATA * conn TSRMLS_DC)
+{
+       enum_func_status ret = PASS;
+
+       DBG_ENTER("mysqlnd_conn_data::execute_init_commands");
+       if (conn->options->init_commands) {
+               unsigned int current_command = 0;
+               for (; current_command < conn->options->num_commands; ++current_command) {
+                       const char * const command = conn->options->init_commands[current_command];
+                       if (command) {
+                               MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_INIT_COMMAND_EXECUTED_COUNT);
+                               if (PASS != conn->m->query(conn, command, strlen(command) TSRMLS_CC)) {
+                                       MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_INIT_COMMAND_FAILED_COUNT);
+                                       ret = FAIL;
+                                       break;
+                               }
+                               if (conn->last_query_type == QUERY_SELECT) {
+                                       MYSQLND_RES * result = conn->m->use_result(conn TSRMLS_CC);
+                                       if (result) {
+                                               result->m.free_result(result, TRUE TSRMLS_CC);
+                                       }
+                               }
+                       }
+               }
+       }
+       DBG_RETURN(ret);
+}
+/* }}} */
+
+
+/* {{{ mysqlnd_conn_data::get_updated_connect_flags */
+static unsigned int
+MYSQLND_METHOD(mysqlnd_conn_data, get_updated_connect_flags)(MYSQLND_CONN_DATA * conn, unsigned int mysql_flags TSRMLS_DC)
+{
+       MYSQLND_NET * net = conn->net;
+
+       DBG_ENTER("mysqlnd_conn_data::get_updated_connect_flags");
+       /* we allow load data local infile by default */
+       mysql_flags |= MYSQLND_CAPABILITIES;
+
+       if (PG(open_basedir) && strlen(PG(open_basedir))) {
+               mysql_flags ^= CLIENT_LOCAL_FILES;
+       }
+
+#ifndef MYSQLND_COMPRESSION_ENABLED
+       if (mysql_flags & CLIENT_COMPRESS) {
+               mysql_flags &= ~CLIENT_COMPRESS;
+       }
+#else
+       if (net && net->data->options.flags & MYSQLND_NET_FLAG_USE_COMPRESSION) {
+               mysql_flags |= CLIENT_COMPRESS;
+       }
+#endif
+#ifndef MYSQLND_SSL_SUPPORTED
+       if (mysql_flags & CLIENT_SSL) {
+               mysql_flags &= ~CLIENT_SSL;
+       }
+#else
+       if (net && (net->data->options.ssl_key || net->data->options.ssl_cert ||
+               net->data->options.ssl_ca || net->data->options.ssl_capath || net->data->options.ssl_cipher))
+       {
+               mysql_flags |= CLIENT_SSL;
+       }
+#endif
+
+       DBG_RETURN(mysql_flags);
+}
+/* }}} */
+
+
+/* {{{ mysqlnd_conn_data::connect_handshake */
+static enum_func_status
+MYSQLND_METHOD(mysqlnd_conn_data, connect_handshake)(MYSQLND_CONN_DATA * conn,
+                                               const char * const host, const char * const user,
+                                               const char * const passwd, const unsigned int passwd_len,
+                                               const char * const db, const unsigned int db_len,
+                                               const unsigned int mysql_flags TSRMLS_DC)
+{
+       MYSQLND_PACKET_GREET * greet_packet;
+       MYSQLND_NET * net = conn->net;
+
+       DBG_ENTER("mysqlnd_conn_data::connect_handshake");
+
+       greet_packet = conn->protocol->m.get_greet_packet(conn->protocol, FALSE TSRMLS_CC);
+       if (!greet_packet) {
+               SET_OOM_ERROR(*conn->error_info);
+               DBG_RETURN(FAIL); /* OOM */
+       }
+
+       if (FAIL == net->data->m.connect_ex(conn->net, conn->scheme, conn->scheme_len, conn->persistent,
+                                                                               conn->stats, conn->error_info TSRMLS_CC))
+       {
+               goto err;
+       }
+
+       DBG_INF_FMT("stream=%p", net->data->m.get_stream(net TSRMLS_CC));
+
+       if (FAIL == PACKET_READ(greet_packet, conn)) {
+               DBG_ERR("Error while reading greeting packet");
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error while reading greeting packet. PID=%d", getpid());
+               goto err;
+       } else if (greet_packet->error_no) {
+               DBG_ERR_FMT("errorno=%u error=%s", greet_packet->error_no, greet_packet->error);
+               SET_CLIENT_ERROR(*conn->error_info, greet_packet->error_no, greet_packet->sqlstate, greet_packet->error);
+               goto err;
+       } else if (greet_packet->pre41) {
+               DBG_ERR_FMT("Connecting to 3.22, 3.23 & 4.0 is not supported. Server is %-.32s", greet_packet->server_version);
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Connecting to 3.22, 3.23 & 4.0 "
+                                               " is not supported. Server is %-.32s", greet_packet->server_version);
+               SET_CLIENT_ERROR(*conn->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE,
+                                                "Connecting to 3.22, 3.23 & 4.0 servers is not supported");
+               goto err;
+       }
+
+       conn->thread_id                 = greet_packet->thread_id;
+       conn->protocol_version  = greet_packet->protocol_version;
+       conn->server_version    = mnd_pestrdup(greet_packet->server_version, conn->persistent);
+
+       conn->greet_charset = mysqlnd_find_charset_nr(greet_packet->charset_no);
+
+       if (FAIL == mysqlnd_connect_run_authentication(conn, user, passwd, db, db_len, (size_t) passwd_len,
+                                                                                                  greet_packet, conn->options, mysql_flags TSRMLS_CC))
+       {
+               goto err;
+       }
+       conn->client_flag                       = mysql_flags;
+       conn->server_capabilities       = greet_packet->server_capabilities;
+       conn->upsert_status->warning_count = 0;
+       conn->upsert_status->server_status = greet_packet->server_status;
+       conn->upsert_status->affected_rows = 0;
+
+       PACKET_FREE(greet_packet);
+       DBG_RETURN(PASS);
+err:
+       PACKET_FREE(greet_packet);
+       DBG_RETURN(FAIL);
+}
+/* }}} */
+
+
 /* {{{ mysqlnd_conn_data::connect */
 static enum_func_status
 MYSQLND_METHOD(mysqlnd_conn_data, connect)(MYSQLND_CONN_DATA * conn,
@@ -625,8 +767,6 @@ MYSQLND_METHOD(mysqlnd_conn_data, connect)(MYSQLND_CONN_DATA * conn,
        zend_bool local_tx_started = FALSE;
        MYSQLND_NET * net = conn->net;
 
-       MYSQLND_PACKET_GREET * greet_packet = NULL;
-
        DBG_ENTER("mysqlnd_conn_data::connect");
 
        if (PASS != conn->m->local_tx_start(conn, this_func TSRMLS_CC)) {
@@ -686,6 +826,8 @@ MYSQLND_METHOD(mysqlnd_conn_data, connect)(MYSQLND_CONN_DATA * conn,
                DBG_INF_FMT("no db given, using empty string");
                db = "";
                db_len = 0;
+       } else {
+               mysql_flags |= CLIENT_CONNECT_WITH_DB;  
        }
 
        host_len = strlen(host);
@@ -729,77 +871,9 @@ MYSQLND_METHOD(mysqlnd_conn_data, connect)(MYSQLND_CONN_DATA * conn,
                }
        }
 
-       greet_packet = conn->protocol->m.get_greet_packet(conn->protocol, FALSE TSRMLS_CC);
-       if (!greet_packet) {
-               SET_OOM_ERROR(*conn->error_info);
-               goto err; /* OOM */
-       }
+       mysql_flags = conn->m->get_updated_connect_flags(conn, mysql_flags TSRMLS_CC);
 
-       if (FAIL == net->data->m.connect_ex(conn->net, conn->scheme, conn->scheme_len, conn->persistent,
-                                                                               conn->stats, conn->error_info TSRMLS_CC))
-       {
-               goto err;
-       }
-
-       DBG_INF_FMT("stream=%p", net->data->m.get_stream(net TSRMLS_CC));
-
-       if (FAIL == PACKET_READ(greet_packet, conn)) {
-               DBG_ERR("Error while reading greeting packet");
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error while reading greeting packet. PID=%d", getpid());
-               goto err;
-       } else if (greet_packet->error_no) {
-               DBG_ERR_FMT("errorno=%u error=%s", greet_packet->error_no, greet_packet->error);
-               SET_CLIENT_ERROR(*conn->error_info, greet_packet->error_no, greet_packet->sqlstate, greet_packet->error);
-               goto err;
-       } else if (greet_packet->pre41) {
-               DBG_ERR_FMT("Connecting to 3.22, 3.23 & 4.0 is not supported. Server is %-.32s", greet_packet->server_version);
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Connecting to 3.22, 3.23 & 4.0 "
-                                               " is not supported. Server is %-.32s", greet_packet->server_version);
-               SET_CLIENT_ERROR(*conn->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE,
-                                                "Connecting to 3.22, 3.23 & 4.0 servers is not supported");
-               goto err;
-       }
-
-       conn->thread_id                 = greet_packet->thread_id;
-       conn->protocol_version  = greet_packet->protocol_version;
-       conn->server_version    = mnd_pestrdup(greet_packet->server_version, conn->persistent);
-
-       conn->greet_charset = mysqlnd_find_charset_nr(greet_packet->charset_no);
-       /* we allow load data local infile by default */
-       mysql_flags |= MYSQLND_CAPABILITIES;
-
-       if (db) {
-               mysql_flags |= CLIENT_CONNECT_WITH_DB;
-       }
-
-       if (PG(open_basedir) && strlen(PG(open_basedir))) {
-               mysql_flags ^= CLIENT_LOCAL_FILES;
-       }
-
-#ifndef MYSQLND_COMPRESSION_ENABLED
-       if (mysql_flags & CLIENT_COMPRESS) {
-               mysql_flags &= ~CLIENT_COMPRESS;
-       }
-#else
-       if (net->data->options.flags & MYSQLND_NET_FLAG_USE_COMPRESSION) {
-               mysql_flags |= CLIENT_COMPRESS;
-       }
-#endif
-#ifndef MYSQLND_SSL_SUPPORTED
-       if (mysql_flags & CLIENT_SSL) {
-               mysql_flags &= ~CLIENT_SSL;
-       }
-#else
-       if (net->data->options.ssl_key || net->data->options.ssl_cert ||
-               net->data->options.ssl_ca || net->data->options.ssl_capath || net->data->options.ssl_cipher)
-       {
-               mysql_flags |= CLIENT_SSL;
-       }
-#endif
-
-       if (FAIL == mysqlnd_connect_run_authentication(conn, user, passwd, db, db_len, (size_t) passwd_len,
-                                                                                                  greet_packet, conn->options, mysql_flags TSRMLS_CC))
-       {
+       if (FAIL == conn->m->connect_handshake(conn, host, user, passwd, passwd_len, db, db_len, mysql_flags TSRMLS_CC)) {
                goto err;
        }
 
@@ -876,13 +950,8 @@ MYSQLND_METHOD(mysqlnd_conn_data, connect)(MYSQLND_CONN_DATA * conn,
                        }
                        conn->unix_socket_len = strlen(conn->unix_socket);
                }
-               conn->client_flag               = mysql_flags;
                conn->max_packet_size   = MYSQLND_ASSEMBLED_PACKET_MAX_SIZE;
                /* todo: check if charset is available */
-               conn->server_capabilities = greet_packet->server_capabilities;
-               conn->upsert_status->warning_count = 0;
-               conn->upsert_status->server_status = greet_packet->server_status;
-               conn->upsert_status->affected_rows = 0;
 
                SET_EMPTY_ERROR(*conn->error_info);
 
@@ -895,27 +964,10 @@ MYSQLND_METHOD(mysqlnd_conn_data, connect)(MYSQLND_CONN_DATA * conn,
                        DBG_INF("unicode set");
                }
 #endif
-               if (conn->options->init_commands) {
-                       unsigned int current_command = 0;
-                       for (; current_command < conn->options->num_commands; ++current_command) {
-                               const char * const command = conn->options->init_commands[current_command];
-                               if (command) {
-                                       MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_INIT_COMMAND_EXECUTED_COUNT);
-                                       if (PASS != conn->m->query(conn, command, strlen(command) TSRMLS_CC)) {
-                                               MYSQLND_INC_CONN_STATISTIC(conn->stats, STAT_INIT_COMMAND_FAILED_COUNT);
-                                               goto err;
-                                       }
-                                       if (conn->last_query_type == QUERY_SELECT) {
-                                               MYSQLND_RES * result = conn->m->use_result(conn TSRMLS_CC);
-                                               if (result) {
-                                                       result->m.free_result(result, TRUE TSRMLS_CC);
-                                               }
-                                       }
-                               }
-                       }
+               if (FAIL == conn->m->execute_init_commands(conn TSRMLS_CC)) {
+                       goto err;
                }
 
-
                MYSQLND_INC_CONN_STATISTIC_W_VALUE2(conn->stats, STAT_CONNECT_SUCCESS, 1, STAT_OPENED_CONNECTIONS, 1);
                if (reconnect) {
                        MYSQLND_INC_GLOBAL_STATISTIC(STAT_RECONNECT);
@@ -926,13 +978,10 @@ MYSQLND_METHOD(mysqlnd_conn_data, connect)(MYSQLND_CONN_DATA * conn,
 
                DBG_INF_FMT("connection_id=%llu", conn->thread_id);
 
-               PACKET_FREE(greet_packet);
-
                conn->m->local_tx_end(conn, this_func, PASS TSRMLS_CC);
                DBG_RETURN(PASS);
        }
 err:
-       PACKET_FREE(greet_packet);
 
        DBG_ERR_FMT("[%u] %.128s (trying to connect via %s)", conn->error_info->error_no, conn->error_info->error, conn->scheme);
        if (!conn->error_info->error_no) {
@@ -2133,7 +2182,7 @@ MYSQLND_METHOD(mysqlnd_conn_data, change_user)(MYSQLND_CONN_DATA * const conn,
                }
                memcpy(plugin_data, conn->auth_plugin_data, plugin_data_len);
 
-               requested_protocol = mnd_pestrdup(conn->options->auth_protocol? conn->options->auth_protocol:"mysql_native_password", FALSE);
+               requested_protocol = mnd_pestrdup(conn->options->auth_protocol? conn->options->auth_protocol:MYSQLND_DEFAULT_AUTH_PROTOCOL, FALSE);
                if (!requested_protocol) {
                        ret = FAIL;
                        goto end;
@@ -2646,7 +2695,10 @@ MYSQLND_CLASS_METHODS_START(mysqlnd_conn_data)
        MYSQLND_METHOD(mysqlnd_conn_data, tx_commit),
        MYSQLND_METHOD(mysqlnd_conn_data, tx_rollback),
        MYSQLND_METHOD(mysqlnd_conn_data, local_tx_start),
-       MYSQLND_METHOD(mysqlnd_conn_data, local_tx_end)
+       MYSQLND_METHOD(mysqlnd_conn_data, local_tx_end),
+       MYSQLND_METHOD(mysqlnd_conn_data, execute_init_commands),
+       MYSQLND_METHOD(mysqlnd_conn_data, get_updated_connect_flags),
+       MYSQLND_METHOD(mysqlnd_conn_data, connect_handshake)
 MYSQLND_CLASS_METHODS_END;
 
 
index 3372e739aa9ce3937b50fbfcc6bc62475f6f98aa..aa5a9e5162939ea6ef04162646ee7b1a383d7897 100644 (file)
@@ -117,6 +117,7 @@ PHPAPI char * mysqlnd_get_backtrace(uint max_levels, size_t * length TSRMLS_DC);
                                        if ((dbg_obj)) { \
                                                dbg_skip_trace = !(dbg_obj)->m->func_enter((dbg_obj), __LINE__, __FILE__, func_name, strlen(func_name)); \
                                        } \
+                                       if (dbg_skip_trace); /* shut compiler's mouth */\ 
                                        do { \
                                                if ((dbg_obj) && (dbg_obj)->flags & MYSQLND_DEBUG_PROFILE_CALLS) { \
                                                        DBG_PROFILE_START_TIME(); \
index b0fe88648ef41ba33842d5fc5f8cefbfa2a053d9..c8daa0c79bf58cc38a1c0da831affe52295e12e5 100644 (file)
@@ -37,6 +37,8 @@
 
 #define MYSQLND_ASSEMBLED_PACKET_MAX_SIZE 3UL*1024UL*1024UL*1024UL
 
+#define MYSQLND_DEFAULT_AUTH_PROTOCOL "mysql_native_password"
+
 #define MYSQLND_ERRMSG_SIZE                    512
 #define MYSQLND_SQLSTATE_LENGTH                5
 #define MYSQLND_SQLSTATE_NULL          "00000"
index b355a0d5f628676113a5165de7780ac30ea3db17..4b3db991a9a536daa9aefeb5da3b529f69927a69 100644 (file)
@@ -473,6 +473,9 @@ typedef enum_func_status    (*func_mysqlnd_conn_data__tx_rollback)(MYSQLND_CONN_DAT
 
 typedef enum_func_status       (*func_mysqlnd_conn_data__local_tx_start)(MYSQLND_CONN_DATA * conn, size_t this_func TSRMLS_DC);
 typedef enum_func_status       (*func_mysqlnd_conn_data__local_tx_end)(MYSQLND_CONN_DATA * conn, size_t this_func, enum_func_status status TSRMLS_DC);
+typedef enum_func_status       (*func_mysqlnd_conn_data__execute_init_commands)(MYSQLND_CONN_DATA * conn TSRMLS_DC);
+typedef unsigned int           (*func_mysqlnd_conn_data__get_updated_connect_flags)(MYSQLND_CONN_DATA * conn, unsigned int mysql_flags TSRMLS_DC);
+typedef enum_func_status       (*func_mysqlnd_conn_data__connect_handshake)(MYSQLND_CONN_DATA * conn, const char * const host, const char * const user, const char * const passwd, const unsigned int passwd_len, const char * const db, const unsigned int db_len, const unsigned int mysql_flags TSRMLS_DC);
 
 
 struct st_mysqlnd_conn_data_methods
@@ -553,6 +556,10 @@ struct st_mysqlnd_conn_data_methods
 
        func_mysqlnd_conn_data__local_tx_start local_tx_start;
        func_mysqlnd_conn_data__local_tx_end local_tx_end;
+
+       func_mysqlnd_conn_data__execute_init_commands execute_init_commands;
+       func_mysqlnd_conn_data__get_updated_connect_flags get_updated_connect_flags;
+       func_mysqlnd_conn_data__connect_handshake connect_handshake;
 };