示例#1
0
static VALUE nogvl_perform(void *handle) {
  CURLcode res;
  VALUE status = Qnil;

  res = curl_easy_perform(handle);
  if (CURLE_OK != res) {
    status = select_error(res);
  }

  return status;
}
示例#2
0
/* Perform the actual HTTP request by calling libcurl. */
static VALUE perform_request(VALUE self) {
  struct curl_state *state = get_curl_state(self);
  CURL* curl = state->handle;
  membuffer* header_buffer = NULL;
  membuffer* body_buffer = NULL;
  CURLcode ret = 0;

  state->interrupt = 0;  /* clear any interrupt flags */

  header_buffer = &state->header_buffer;
  body_buffer = &state->body_buffer;

  membuffer_clear(header_buffer);
  membuffer_clear(body_buffer);

  /* headers */
  curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, &session_write_handler);
  curl_easy_setopt(curl, CURLOPT_HEADERDATA, header_buffer);

  /* body */
  if (!state->download_file) {
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &session_write_handler);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, body_buffer);
  }

#if (defined(HAVE_TBR) || defined(HAVE_TCWOGVL)) && defined(USE_TBR)
#if defined(HAVE_TCWOGVL)
  ret = (CURLcode) rb_thread_call_without_gvl(
          (void *(*)(void *)) curl_easy_perform, curl,
          RUBY_UBF_IO, 0
        );
#else
  ret = (CURLcode) rb_thread_blocking_region(
          (rb_blocking_function_t*) curl_easy_perform, curl,
          RUBY_UBF_IO, 0
        );
#endif
#else
  ret = curl_easy_perform(curl);
#endif

  if (CURLE_OK == ret) {
    VALUE header_str = membuffer_to_rb_str(header_buffer);
    VALUE body_str = Qnil;
    if (!state->download_file) { body_str = membuffer_to_rb_str(body_buffer); }
    
    curl_easy_setopt(curl, CURLOPT_COOKIELIST, "FLUSH"); // Flush cookies to the cookie jar
    
    return create_response(self, curl, header_str, body_str);
  } else {
    rb_raise(select_error(ret), "%s", state->error_buf);
  }
}
示例#3
0
文件: session_ext.c 项目: tmm1/patron
// Perform the actual HTTP request by calling libcurl.
static VALUE perform_request(VALUE self) {
  struct curl_state *state;
  Data_Get_Struct(self, struct curl_state, state);

  CURL* curl = state->handle;

  // headers
  VALUE header_buffer = rb_str_buf_new(32768);
  curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, &session_write_handler);
  curl_easy_setopt(curl, CURLOPT_HEADERDATA, header_buffer);

  // body
  VALUE body_buffer = Qnil;
  if (!state->download_file) {
    body_buffer = rb_str_buf_new(32768);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &session_write_handler);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, body_buffer);
  }

  #if defined(HAVE_TBR) && defined(USE_TBR)
  CURLcode ret = rb_thread_blocking_region(curl_easy_perform, curl, RUBY_UBF_IO, 0);
  #else
  CURLcode ret = curl_easy_perform(curl);
  #endif

  if (CURLE_OK == ret) {
    VALUE response = create_response(curl);
    if (!NIL_P(body_buffer)) {
      rb_iv_set(response, "@body", body_buffer);
    }
    rb_funcall(response, rb_intern("parse_headers"), 1, header_buffer);
    return response;
  } else {
    rb_raise(select_error(ret), "%s", state->error_buf);
  }
}
示例#4
0
文件: stmt.c 项目: fanyang01/sql
static int select_and_print(DB * db, const char *tname,
			    col_t * cols, int ncol, cond_t * conds, int ncond)
{
	table_t *t;
	cursor_t *cur;
	record_t *r;
	int count;
	int ret = 0;
	int error, xerror;

	if ((t = db_find_table(db, tname)) == NULL) {
		xerrno = ERR_NOTABLE;
		return -1;
	}
	for (int i = 0; i < ncol; i++) {
		int k;
		if ((k = table_find_col(t, cols[i].name)) < 0) {
			xerrno = ERR_NOCOL;
			return -1;
		}
		cols[i].type = t->cols[k].type;
		cols[i].size = t->cols[k].size;
	}

	if (ncol == 0) {
		ncol = t->ncols;
		cols = t->cols;
	}

	int *width;

	if ((width = calloc(ncol, sizeof(int))) == NULL) {
		perror("out of memory");
		return -1;
	}

	for (int i = 0; i < ncol; i++) {
		int k = table_find_col(t, cols[i].name);
		switch (t->cols[k].type) {
		case TYPE_INT:
			width[i] =
			    _max(INT_P_WIDTH, strlen(t->cols[k].name) + 1);
			break;
		case TYPE_FLOAT:
			width[i] =
			    _max(FLOAT_P_WIDTH, strlen(t->cols[k].name) + 1);
			break;
		case TYPE_STRING:
			width[i] =
			    _max(t->cols[k].size, strlen(t->cols[k].name) + 1);
			break;
		}
	}

	if ((cur = select_from(db, tname, conds, ncond)) == NULL) {
		preserve_errno(free(width));
		return -1;
	}

	print_thead(cols, ncol, width);

	count = 0;
	while ((r = select_next(db, cur)) != NULL) {
		print_record(t, cols, ncol, r, width);
		free_record(r);
		count++;
	}
	if (select_error(cur)) {
		error = errno;
		xerror = xerrno;
		ret = -1;
	}
	printf("Total %d %s\n\n", count, count > 1 ? "records" : "record");
	free_cursor(cur);
	free(width);
	errno = error;
	xerrno = xerror;
	return ret;
}