void manual_query() { char query[1024]; unsigned int qlen; GWBUF** tmpbuf; free_buffers(); printf("Enter query: "); fgets(query,1024,stdin); qlen = strnlen(query, 1024); if((tmpbuf = malloc(sizeof(GWBUF*)))== NULL){ printf("Error: cannot allocate enough memory.\n"); skygw_log_write(LOGFILE_ERROR,"Error: cannot allocate enough memory.\n"); return; } instance.buffer = tmpbuf; instance.buffer_count = 1; instance.buffer[0] = gwbuf_alloc(qlen + 5); gwbuf_set_type(instance.buffer[0],GWBUF_TYPE_MYSQL); memcpy(instance.buffer[0]->sbuf->data + 5,query,qlen); instance.buffer[0]->sbuf->data[0] = (qlen); instance.buffer[0]->sbuf->data[1] = (qlen << 8); instance.buffer[0]->sbuf->data[2] = (qlen << 16); instance.buffer[0]->sbuf->data[3] = 0x00; instance.buffer[0]->sbuf->data[4] = 0x03; }
/** * Returns pointer to GWBUF of a requested type. * As of 10.3.14 only MySQL to plain text conversion is supported. * Return NULL if conversion between types is not supported or due lacking * type information. */ GWBUF *gwbuf_clone_transform( GWBUF * head, gwbuf_type_t targettype) { gwbuf_type_t src_type; GWBUF* clonebuf; CHK_GWBUF(head); src_type = head->gwbuf_type; if (targettype == GWBUF_TYPE_UNDEFINED || src_type == GWBUF_TYPE_UNDEFINED || src_type == GWBUF_TYPE_PLAINSQL || targettype == src_type) { clonebuf = NULL; goto return_clonebuf; } if (GWBUF_IS_TYPE_MYSQL(head)) { if (GWBUF_TYPE_PLAINSQL == targettype) { /** Crete reference to string part of buffer */ clonebuf = gwbuf_clone_portion( head, 5, GWBUF_LENGTH(head)-5); ss_dassert(clonebuf != NULL); /** Overwrite the type with new format */ gwbuf_set_type(clonebuf, targettype); } else { clonebuf = NULL; } } else { clonebuf = NULL; } return_clonebuf: return clonebuf; }
/** * Read event for EPOLLIN on the httpd protocol module. * * @param dcb The descriptor control block * @return */ static int httpd_read_event(DCB* dcb) { SESSION *session = dcb->session; ROUTER_OBJECT *router = session->service->router; ROUTER *router_instance = session->service->router_instance; void *rsession = session->router_session; int numchars = 1; char buf[HTTPD_REQUESTLINE_MAXLEN-1] = ""; char *query_string = NULL; char method[HTTPD_METHOD_MAXLEN-1] = ""; char url[HTTPD_SMALL_BUFFER] = ""; size_t i, j; int headers_read = 0; HTTPD_session *client_data = NULL; GWBUF *uri; client_data = dcb->data; /** * get the request line * METHOD URL HTTP_VER\r\n */ numchars = httpd_get_line(dcb->fd, buf, sizeof(buf)); i = 0; j = 0; while (!ISspace(buf[j]) && (i < sizeof(method) - 1)) { method[i] = buf[j]; i++; j++; } method[i] = '\0'; strcpy(client_data->method, method); /* check allowed http methods */ if (strcasecmp(method, "GET") && strcasecmp(method, "POST")) { //httpd_unimplemented(dcb->fd); return 0; } i = 0; while ( (j < sizeof(buf)) && ISspace(buf[j])) { j++; } while ((j < sizeof(buf) - 1) && !ISspace(buf[j]) && (i < sizeof(url) - 1)) { url[i] = buf[j]; i++; j++; } url[i] = '\0'; /** * Get the query string if availble */ if (strcasecmp(method, "GET") == 0) { query_string = url; while ((*query_string != '?') && (*query_string != '\0')) { query_string++; } if (*query_string == '?') { *query_string = '\0'; query_string++; } } /** * Get the request headers */ while ((numchars > 0) && strcmp("\n", buf)) { char *value = NULL; char *end = NULL; numchars = httpd_get_line(dcb->fd, buf, sizeof(buf)); if ((value = strchr(buf, ':'))) { *value = '\0'; value++; end = &value[strlen(value) -1]; *end = '\0'; if (strncasecmp(buf, "Hostname", 6) == 0) { strcpy(client_data->hostname, value); } if (strncasecmp(buf, "useragent", 9) == 0) { strcpy(client_data->useragent, value); } } } if (numchars) { headers_read = 1; memcpy(&client_data->headers_received, &headers_read, sizeof(int)); } /** * Now begins the server reply */ /* send all the basic headers and close with \r\n */ httpd_send_headers(dcb, 1); #if 0 /** * ToDO: launch proper content handling based on the requested URI, later REST interface * */ if (strcmp(url, "/show") == 0) { if (query_string && strlen(query_string)) { if (strcmp(query_string, "dcb") == 0) { dprintAllDCBs(dcb); } if (strcmp(query_string, "session") == 0) { dprintAllSessions(dcb); } } } if (strcmp(url, "/services") == 0) { RESULTSET *set, *seviceGetList(); if ((set = serviceGetList()) != NULL) { resultset_stream_json(set, dcb); resultset_free(set); } } #endif if ((uri = gwbuf_alloc(strlen(url) + 1)) != NULL) { strcpy((char *)GWBUF_DATA(uri), url); gwbuf_set_type(uri, GWBUF_TYPE_HTTP); SESSION_ROUTE_QUERY(session, uri); } /* force the client connecton close */ dcb_close(dcb); return 0; }
/** * Loads a query from a file *@return 0 if successful, 1 if an error occurred */ int load_query() { char** query_list = NULL; char* buffer = NULL; int i, qcount = 0, qbuff_sz = 10, rval = 0; int offset = 0; unsigned int qlen = 0; buffer = (char*)calloc(4092,sizeof(char)); if(buffer == NULL){ printf("Error: cannot allocate enough memory.\n"); MXS_ERROR("cannot allocate enough memory.\n"); return 1; } query_list = calloc(qbuff_sz,sizeof(char*)); if(query_list == NULL){ printf("Error: cannot allocate enough memory.\n"); MXS_ERROR("cannot allocate enough memory.\n"); free(buffer); return 1; } while((offset = fdgets(instance.infile,buffer,4092))){ if(qbuff_sz <= qcount){ char** tmpbuff = realloc(query_list,sizeof(char*)*qbuff_sz*2); if(tmpbuff == NULL){ printf("Error: cannot allocate enough memory.\n"); MXS_ERROR("cannot allocate enough memory.\n"); rval = 1; goto retblock; } query_list = tmpbuff; qbuff_sz *= 2; } query_list[qcount] = calloc((offset + 1),sizeof(char)); strcpy(query_list[qcount],buffer); offset = 0; qcount++; } /**TODO check what messes up the first querystring*/ GWBUF** tmpbff = malloc(sizeof(GWBUF*)*(qcount + 1)); if(tmpbff){ for(i = 0;i<qcount;i++){ tmpbff[i] = gwbuf_alloc(strlen(query_list[i]) + 6); if(tmpbff[i] == NULL) { printf("Error: cannot allocate a new buffer.\n"); MXS_ERROR("cannot allocate a new buffer.\n"); int x; for(x = 0;x<i;x++) { gwbuf_free(tmpbff[x]); } free(tmpbff); rval = 1; goto retblock; } gwbuf_set_type(tmpbff[i],GWBUF_TYPE_MYSQL); strcpy((char*)(tmpbff[i]->start + 5),query_list[i]); qlen = strlen(query_list[i]) + 1; tmpbff[i]->sbuf->data[0] = qlen; tmpbff[i]->sbuf->data[1] = (qlen << 8); tmpbff[i]->sbuf->data[2] = (qlen << 16); tmpbff[i]->sbuf->data[3] = 0x00; tmpbff[i]->sbuf->data[4] = 0x03; } tmpbff[qcount] = NULL; instance.buffer = tmpbff; }else{ printf("Error: cannot allocate enough memory for buffers.\n"); MXS_ERROR("cannot allocate enough memory for buffers.\n"); free_buffers(); rval = 1; goto retblock; } if(qcount < 1){ rval = 1; goto retblock; } instance.buffer_count = qcount; retblock: for(i = 0;i<qcount;i++) { free(query_list[i]); } free(query_list); free(buffer); return rval; }