Example #1
0
static int uwsgi_routing_func_cache(struct wsgi_request *wsgi_req, struct uwsgi_route *ur){

	struct uwsgi_router_cache_conf *urcc = (struct uwsgi_router_cache_conf *) ur->data2;

	if (!uwsgi.cache_max_items) return UWSGI_ROUTE_NEXT;

	char *c_k = NULL;
	uint16_t c_k_len = 0;
	struct uwsgi_buffer *ub = NULL;
	int k_need_free = 0;

	if (urcc->key) {
		char **subject = (char **) (((char *)(wsgi_req))+ur->subject);
        	uint16_t *subject_len = (uint16_t *)  (((char *)(wsgi_req))+ur->subject_len);

        	c_k = uwsgi_regexp_apply_ovec(*subject, *subject_len, urcc->key, urcc->key_len, ur->ovector, ur->ovn);
		c_k_len = strlen(c_k);
		k_need_free = 1;
	}
	else {
		char **key = (char **) (((char *) wsgi_req) + urcc->var_offset);
        	uint16_t *keylen = (uint16_t *) (((char *) wsgi_req) + urcc->var_offset_len);
		c_k = *key;
		c_k_len = *keylen;
	}

	uint64_t valsize = 0;
	char *value = uwsgi_cache_get(c_k, c_k_len, &valsize);
	if (value) {
		ub = uwsgi_buffer_new(uwsgi.page_size);
		if (urcc->type_num == 1) {
			wsgi_req->status = 200;
			if (uwsgi_buffer_append(ub, "HTTP/1.0 200 OK\r\nConnection: close\r\nContent-Type: ", 50)) goto error;
			if (uwsgi_buffer_append(ub, urcc->content_type, urcc->content_type_len)) goto error;
			if (uwsgi_buffer_append(ub, "\r\nContent-Length: ", 18 )) goto error;
			if (uwsgi_buffer_num64(ub, valsize)) goto error;
			if (uwsgi_buffer_append(ub, "\r\n\r\n", 4 )) goto error;
			wsgi_req->headers_size += wsgi_req->socket->proto_write_header(wsgi_req, ub->buf, ub->pos);
			uwsgi_buffer_destroy(ub);
			wsgi_req->header_cnt = 3;		
		}
		// body only
		wsgi_req->response_size += wsgi_req->socket->proto_write(wsgi_req, value, valsize);
		if (k_need_free) free(c_k);
		if (ur->custom)
			return UWSGI_ROUTE_NEXT;
		return UWSGI_ROUTE_BREAK;
	}
	if (k_need_free) free(c_k);
	
	return UWSGI_ROUTE_NEXT;
error:
	uwsgi_buffer_destroy(ub);
	if (k_need_free) free(c_k);
	return UWSGI_ROUTE_BREAK;
}
Example #2
0
static int socket_send_metric(struct uwsgi_buffer *ub, struct uwsgi_stats_pusher_instance *uspi, struct uwsgi_metric *um) {
	struct socket_node *sn = (struct socket_node *) uspi->data;
	// reset the buffer
        ub->pos = 0;
	if (uwsgi_buffer_append(ub, sn->prefix, sn->prefix_len)) return -1;	
	if (uwsgi_buffer_append(ub, ".", 1)) return -1;
	if (uwsgi_buffer_append(ub, um->name, um->name_len)) return -1;
	if (uwsgi_buffer_append(ub, " ", 1)) return -1;
        if (uwsgi_buffer_num64(ub, (int64_t) um->type)) return -1;
	if (uwsgi_buffer_append(ub, " ", 1)) return -1;
        if (uwsgi_buffer_num64(ub, *um->value)) return -1;

        if (sendto(sn->fd, ub->buf, ub->pos, 0, &sn->addr.sa, sn->addr_len) < 0) {
			if (errno != EAGAIN)	// drop if we were to block
                uwsgi_error("socket_send_metric()/sendto()");
        }

        return 0;

}
Example #3
0
static int dogstatsd_send_metric(struct uwsgi_buffer *ub, struct uwsgi_stats_pusher_instance *uspi, char *metric, size_t metric_len, int64_t value, char type[2]) {
  struct dogstatsd_node *sn = (struct dogstatsd_node *) uspi->data;

  char datatog_metric_name[MAX_BUFFER_SIZE];
  char datadog_tags[MAX_BUFFER_SIZE];
  char raw_metric_name[MAX_BUFFER_SIZE];

  int extracted_tags = 0;

  // check if we can handle such a metric length
  if (metric_len >= MAX_BUFFER_SIZE)
    return -1;

  // reset the buffer
  ub->pos = 0;

  // sanitize buffers
  memset(datadog_tags, 0, MAX_BUFFER_SIZE);
  memset(datatog_metric_name, 0, MAX_BUFFER_SIZE);

  // let's copy original metric name before we start
  strncpy(raw_metric_name, metric, metric_len + 1);

  // try to extract tags
  extracted_tags = dogstatsd_generate_tags(raw_metric_name, metric_len, datatog_metric_name, datadog_tags);

  if (extracted_tags < 0)
    return -1;

  if (uwsgi_buffer_append(ub, sn->prefix, sn->prefix_len)) return -1;
  if (uwsgi_buffer_append(ub, ".", 1)) return -1;

  // put the datatog_metric_name if we found some tags
  if (extracted_tags) {
    if (uwsgi_buffer_append(ub, datatog_metric_name, strlen(datatog_metric_name))) return -1;
  } else {
    if (uwsgi_buffer_append(ub, metric, strlen(metric))) return -1;
  }

  if (uwsgi_buffer_append(ub, ":", 1)) return -1;
  if (uwsgi_buffer_num64(ub, value)) return -1;
  if (uwsgi_buffer_append(ub, type, 2)) return -1;

  // add tags metadata if there are any
  if (extracted_tags) {
    if (uwsgi_buffer_append(ub, datadog_tags, strlen(datadog_tags))) return -1;
  }

  if (sendto(sn->fd, ub->buf, ub->pos, 0, (struct sockaddr *) &sn->addr.sa_in, sn->addr_len) < 0) {
    uwsgi_error("dogstatsd_send_metric()/sendto()");
  }

  return 0;
}
Example #4
0
static int statsd_send_metric(struct uwsgi_buffer *ub, struct uwsgi_stats_pusher_instance *uspi, char *metric, size_t metric_len, int64_t value, char type[2]) {
	struct statsd_node *sn = (struct statsd_node *) uspi->data;
	// reset the buffer
        ub->pos = 0;
	if (uwsgi_buffer_append(ub, sn->prefix, sn->prefix_len)) return -1;	
	if (uwsgi_buffer_append(ub, ".", 1)) return -1;
	if (uwsgi_buffer_append(ub, metric, metric_len)) return -1;
	if (uwsgi_buffer_append(ub, ":", 1)) return -1;
        if (uwsgi_buffer_num64(ub, value)) return -1;
	if (uwsgi_buffer_append(ub, type, 2)) return -1;

        if (sendto(sn->fd, ub->buf, ub->pos, 0, (struct sockaddr *) &sn->addr.sa_in, sn->addr_len) < 0) {
                uwsgi_error("statsd_send_metric()/sendto()");
        }

        return 0;

}
Example #5
0
static ssize_t uwsgi_crypto_logger(struct uwsgi_logger *ul, char *message, size_t len) {

    struct uwsgi_crypto_logger_conf *uclc = (struct uwsgi_crypto_logger_conf *) ul->data;

    if (!ul->configured) {

        uclc = uwsgi_calloc(sizeof(struct uwsgi_crypto_logger_conf));

        if (uwsgi_kvlist_parse(ul->arg, strlen(ul->arg), ',', '=',
                               "addr", &uclc->addr,
                               "algo", &uclc->algo,
                               "secret", &uclc->secret,
                               "iv", &uclc->iv,
                               "prefix", &uclc->prefix,
                               NULL)) {
            uwsgi_log_safe("[uwsgi-logcrypto] unable to parse options\n");
            exit(1);
        }

        if (!uclc->addr || !uclc->algo || !uclc->secret) {
            uwsgi_log_safe("[uwsgi-logcrypto] you have to specify at least addr,algo and secret options\n");
            exit(1);
        }

        if (uclc->prefix) {
            uclc->prefix_len = strlen(uclc->prefix);
        }

        char *colon = strchr(uclc->addr, ':');
        if (!colon) {
            uwsgi_log_safe("[uwsgi-logcrypto] invalid UDP address\n");
            exit(1);
        }
        ul->addr_len = socket_to_in_addr(uclc->addr, colon, 0, &ul->addr.sa_in);

        ul->fd = socket(AF_INET, SOCK_DGRAM, 0);
        if (ul->fd < 0) {
            uwsgi_error_safe("uwsgi_crypto_logger()/socket()");
            exit(1);
        }

        uwsgi_crypto_logger_setup_encryption(uclc);

        ul->data = uclc;
        ul->configured = 1;
    }


    struct uwsgi_buffer *ub = uwsgi_buffer_new(uwsgi.page_size);
    if (uwsgi_buffer_num64(ub, uwsgi_micros())) goto error;
    if (uwsgi_buffer_append(ub, " ", 1)) goto error;
    if (uclc->prefix) {
        if (uwsgi_buffer_append(ub, uclc->prefix, uclc->prefix_len)) goto error;
        if (uwsgi_buffer_append(ub, " ", 1)) goto error;
    }
    if (uwsgi_buffer_append(ub, message, len)) goto error;

    // let's encrypt the message
    unsigned char *encrypted = uwsgi_malloc(ub->pos + EVP_MAX_BLOCK_LENGTH);
    if (EVP_EncryptInit_ex(uclc->encrypt_ctx, NULL, NULL, NULL, NULL) <= 0) {
        uwsgi_error_safe("[uwsgi-logcrypto] EVP_EncryptInit_ex()");
        free(encrypted);
        goto error;
    }

    int e_len = 0;
    if (EVP_EncryptUpdate(uclc->encrypt_ctx, encrypted, &e_len, (unsigned char *) ub->buf, ub->pos) <= 0) {
        uwsgi_error("[uwsgi-logcrypto] EVP_EncryptUpdate()");
        free(encrypted);
        goto error;
    }

    int tmplen = 0;
    if (EVP_EncryptFinal_ex(uclc->encrypt_ctx, encrypted + e_len, &tmplen) <= 0) {
        uwsgi_error("[uwsgi-logcrypto] EVP_EncryptFinal_ex()");
        free(encrypted);
        goto error;
    }

    uwsgi_buffer_destroy(ub);

    ssize_t rlen = sendto(ul->fd, encrypted, e_len + tmplen, 0, (struct sockaddr *) &ul->addr.sa_in, ul->addr_len);
    free(encrypted);
    return rlen;

error:
    uwsgi_buffer_destroy(ub);
    return -1;

}
Example #6
0
int uwsgi_exceptions_catch(struct wsgi_request *wsgi_req) {

	if (uwsgi_response_prepare_headers(wsgi_req, "500 Internal Server Error", 25)) {
		return -1;
	}

	if (uwsgi_response_add_content_type(wsgi_req, "text/plain", 10)) {
		return -1;
	}

	struct uwsgi_buffer *ub = uwsgi_buffer_new(4096);
	if (uwsgi_buffer_append(ub, "uWSGI exceptions catcher for \"", 30)) goto error;
	if (uwsgi_buffer_append(ub, wsgi_req->method, wsgi_req->method_len)) goto error;
	if (uwsgi_buffer_append(ub, " ", 1)) goto error;
	if (uwsgi_buffer_append(ub, wsgi_req->uri, wsgi_req->uri_len)) goto error;
	if (uwsgi_buffer_append(ub, "\" (request plugin: \"", 20)) goto error;
	if (uwsgi_buffer_append(ub, (char *) uwsgi.p[wsgi_req->uh->modifier1]->name, strlen(uwsgi.p[wsgi_req->uh->modifier1]->name))) goto error;
	if (uwsgi_buffer_append(ub, "\", modifier1: ", 14 )) goto error;
	if (uwsgi_buffer_num64(ub, wsgi_req->uh->modifier1)) goto error;
	if (uwsgi_buffer_append(ub, ")\n\n", 3)) goto error;

	if (uwsgi_buffer_append(ub, "Exception: ", 11)) goto error;
                
        if (uwsgi.p[wsgi_req->uh->modifier1]->exception_repr) {
                struct uwsgi_buffer *ub_exc_repr = uwsgi.p[wsgi_req->uh->modifier1]->exception_repr(wsgi_req);
                if (ub_exc_repr) {
                        if (uwsgi_buffer_append(ub, ub_exc_repr->buf, ub_exc_repr->pos)) {
                                uwsgi_buffer_destroy(ub_exc_repr);
                                goto error;
                        }
                        uwsgi_buffer_destroy(ub_exc_repr);
                }
                else {
                        goto notavail3;
                }
        }
        else {
notavail3:
                if (uwsgi_buffer_append(ub, "-Not available-", 15)) goto error;
        }

        if (uwsgi_buffer_append(ub, "\n\n", 2)) goto error;

	if (uwsgi_buffer_append(ub, "Exception class: ", 17)) goto error;

	if (uwsgi.p[wsgi_req->uh->modifier1]->exception_class) {
		struct uwsgi_buffer *ub_exc_class = uwsgi.p[wsgi_req->uh->modifier1]->exception_class(wsgi_req);
		if (ub_exc_class) {
			if (uwsgi_buffer_append(ub, ub_exc_class->buf, ub_exc_class->pos)) {
				uwsgi_buffer_destroy(ub_exc_class);
				goto error;
			}
			uwsgi_buffer_destroy(ub_exc_class);
		}
		else {
			goto notavail;
		}
	}
	else {
notavail:
		if (uwsgi_buffer_append(ub, "-Not available-", 15)) goto error;
	}

	if (uwsgi_buffer_append(ub, "\n\n", 2)) goto error;

	if (uwsgi_buffer_append(ub, "Exception message: ", 19)) goto error;

        if (uwsgi.p[wsgi_req->uh->modifier1]->exception_msg) {
                struct uwsgi_buffer *ub_exc_msg = uwsgi.p[wsgi_req->uh->modifier1]->exception_msg(wsgi_req);
                if (ub_exc_msg) {
                        if (uwsgi_buffer_append(ub, ub_exc_msg->buf, ub_exc_msg->pos)) {
                                uwsgi_buffer_destroy(ub_exc_msg);
                                goto error;
                        }
                        uwsgi_buffer_destroy(ub_exc_msg);
                }
                else {
                        goto notavail2;
                }
        }
        else {
notavail2:
                if (uwsgi_buffer_append(ub, "-Not available-", 15)) goto error;
        }

	if (uwsgi_buffer_append(ub, "\n\n", 2)) goto error;

	if (uwsgi_buffer_append(ub, "Backtrace:\n", 11)) goto error;

        if (uwsgi.p[wsgi_req->uh->modifier1]->backtrace) {
                struct uwsgi_buffer *ub_exc_bt = uwsgi.p[wsgi_req->uh->modifier1]->backtrace(wsgi_req);
                if (ub_exc_bt) {
			struct uwsgi_buffer *parsed_bt = uwsgi_buffer_new(4096);
			if (uwsgi_hooked_parse_array(ub_exc_bt->buf, ub_exc_bt->pos, append_backtrace_to_ubuf, parsed_bt)) {
				uwsgi_buffer_destroy(ub_exc_bt);
				uwsgi_buffer_destroy(parsed_bt);
                                goto error;
			}
			uwsgi_buffer_destroy(ub_exc_bt);
                        if (uwsgi_buffer_append(ub, parsed_bt->buf, parsed_bt->pos)) {
                                uwsgi_buffer_destroy(parsed_bt);
                                goto error;
                        }
                        uwsgi_buffer_destroy(parsed_bt);
                }
                else {
                        goto notavail4;
                }
        }
        else {
notavail4:
                if (uwsgi_buffer_append(ub, "-Not available-", 15)) goto error;
        }

        if (uwsgi_buffer_append(ub, "\n\n", 2)) goto error;

	if (uwsgi_hooked_parse(wsgi_req->buffer, wsgi_req->uh->pktsize, append_vars_to_ubuf, ub)) {
		goto error;
	}

	if (uwsgi_response_write_body_do(wsgi_req, ub->buf, ub->pos)) {
		goto error;
	}

	uwsgi_buffer_destroy(ub);
	return 0;

error:
	uwsgi_buffer_destroy(ub);
	return -1;

}