/** * Route the main query downstream along the main filter chain and possibly route * a clone of the buffer to the branch session. If the clone buffer is NULL, nothing * is routed to the branch session. * @param my_instance Tee instance * @param my_session Tee session * @param buffer Main buffer * @param clone Cloned buffer * @return 1 on success, 0 on failure. */ int route_single_query(TEE_INSTANCE* my_instance, TEE_SESSION* my_session, GWBUF* buffer, GWBUF* clone) { int rval = 0; if(!my_session->active || my_session->branch_session == NULL || my_session->branch_session->state != SESSION_STATE_ROUTER_READY) { rval = 0; my_session->active = 0; return rval; } rval = my_session->down.routeQuery(my_session->down.instance, my_session->down.session, buffer); if (clone) { my_session->n_duped++; if (my_session->branch_session->state == SESSION_STATE_ROUTER_READY) { SESSION_ROUTE_QUERY(my_session->branch_session, clone); } else { /** Close tee session */ my_session->active = 0; rval = 0; LOGIF(LT, (skygw_log_write( LOGFILE_TRACE, "Closed tee filter session: Child session in invalid state."))); gwbuf_free(clone); } } else { if (my_session->active) { LOGIF(LT, (skygw_log_write( LOGFILE_TRACE, "Closed tee filter session: Child session is NULL."))); my_session->active = 0; rval = 0; } my_session->n_rejected++; } return rval; }
/** * Route the main query downstream along the main filter chain and possibly route * a clone of the buffer to the branch session. If the clone buffer is NULL, nothing * is routed to the branch session. * @param my_instance Tee instance * @param my_session Tee session * @param buffer Main buffer * @param clone Cloned buffer * @return 1 on success, 0 on failure. */ int route_single_query(TEE_INSTANCE* my_instance, TEE_SESSION* my_session, GWBUF* buffer, GWBUF* clone) { int rval = 0; if (!my_session->active || my_session->branch_session == NULL || my_session->branch_session->state != SESSION_STATE_ROUTER_READY) { rval = 0; my_session->active = 0; return rval; } if (clone == NULL) { /** We won't be expecting any response from the child branch */ my_session->waiting[CHILD] = false; my_session->multipacket[CHILD] = false; my_session->eof[CHILD] = 2; my_session->n_rejected++; } rval = my_session->down.routeQuery(my_session->down.instance, my_session->down.session, buffer); if (clone) { my_session->n_duped++; if (my_session->branch_session->state == SESSION_STATE_ROUTER_READY) { SESSION_ROUTE_QUERY(my_session->branch_session, clone); } else { /** Close tee session */ my_session->active = 0; rval = 0; MXS_INFO("Closed tee filter session: Child session in invalid state."); gwbuf_free(clone); } } return rval; }
/** * 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; }
/** * 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; GWBUF *buf = NULL; char *ptr, *sol; HTTPD_session *client_data = NULL; int n; // Read all the available data if ((n = dcb_read(dcb, &buf)) != -1) { client_data = dcb->data; if (client_data->saved) { buf = gwbuf_append(client_data->saved, buf); client_data->saved = NULL; } buf = gwbuf_make_contiguous(buf); ptr = GWBUF_DATA(buf); if (strncasecmp(ptr, "POST", 4)) { client_data->method = METHOD_POST; gwbuf_add_property(buf, "Method", "POST"); ptr = ptr + 4; } else if (strncasecmp(ptr, "PUT", 3)) { client_data->method = METHOD_PUT; gwbuf_add_property(buf, "Method", "PUT"); ptr = ptr + 3; } else if (strncasecmp(ptr, "GET", 3)) { client_data->method = METHOD_GET; gwbuf_add_property(buf, "Method", "GET"); ptr = ptr + 3; } else if (strncasecmp(ptr, "HEAD", 4)) { client_data->method = METHOD_HEAD; gwbuf_add_property(buf, "Method", "HEAD"); ptr = ptr + 4; } while (ptr < (char *)(buf->end) && isspace(*ptr)) ptr++; sol = ptr; while (ptr < (char *)(buf->end) && isspace(*ptr) == 0) ptr++; client_data->url = strndup(sol, ptr - sol); gwbuf_add_property(buf, "URL", client_data->url); while ((sol = httpd_nextline(buf, ptr)) != NULL && *sol != '\n' && *sol != '\r') { httpd_process_header(buf, sol, client_data); ptr = sol; } /* * We have read all the headers, or run out of data to * examine. */ if (sol == NULL) { client_data->saved = buf; return 0; } else { if (((char *)(buf->end) - sol) < client_data->request_len) { client_data->saved = buf; } else { LOGIF(LT, (skygw_log_write( LOGFILE_TRACE, "HTTPD: request %s.\n", client_data->url))); SESSION_ROUTE_QUERY(session, buf); if (client_data->url) { free(client_data->url); client_data->url = NULL; } } } } return 0; }
/** * Read event for EPOLLIN on the telnetd protocol module. * * @param dcb The descriptor control block * @return */ static int telnetd_read_event(DCB* dcb) { int n; GWBUF *head = NULL; SESSION *session = dcb->session; TELNETD *telnetd = (TELNETD *)dcb->protocol; char *password, *t; if ((n = dcb_read(dcb, &head)) != -1) { if (head) { unsigned char *ptr = GWBUF_DATA(head); ptr = GWBUF_DATA(head); while (GWBUF_LENGTH(head) && *ptr == TELNET_IAC) { telnetd_command(dcb, ptr + 1); GWBUF_CONSUME(head, 3); ptr = GWBUF_DATA(head); } if (GWBUF_LENGTH(head)) { switch (telnetd->state) { case TELNETD_STATE_LOGIN: telnetd->username = strndup(GWBUF_DATA(head), GWBUF_LENGTH(head)); /* Strip the cr/lf from the username */ t = strstr(telnetd->username, "\r\n"); if (t) *t = 0; telnetd->state = TELNETD_STATE_PASSWD; dcb_printf(dcb, "Password: "******"\r\n"); if (t) *t = 0; if (admin_verify(telnetd->username, password)) { telnetd_echo(dcb, 1); telnetd->state = TELNETD_STATE_DATA; dcb_printf(dcb, "\n\nMaxScale> "); } else { dcb_printf(dcb, "\n\rLogin incorrect\n\rLogin: "); telnetd_echo(dcb, 1); telnetd->state = TELNETD_STATE_LOGIN; free(telnetd->username); } gwbuf_consume(head, GWBUF_LENGTH(head)); free(password); break; case TELNETD_STATE_DATA: SESSION_ROUTE_QUERY(session, head); break; } } else { // Force the free of the buffer header gwbuf_consume(head, 0); } } } return n; }