Exemplo n.º 1
0
static void WINAPI internet_status_callback(HINTERNET internet, DWORD_PTR context,
        DWORD internet_status, LPVOID status_info, DWORD status_info_len)
{
    Protocol *protocol = (Protocol*)context;

    switch(internet_status) {
    case INTERNET_STATUS_RESOLVING_NAME:
        TRACE("%p INTERNET_STATUS_RESOLVING_NAME\n", protocol);
        report_progress(protocol, BINDSTATUS_FINDINGRESOURCE, (LPWSTR)status_info);
        break;

    case INTERNET_STATUS_CONNECTING_TO_SERVER:
        TRACE("%p INTERNET_STATUS_CONNECTING_TO_SERVER\n", protocol);
        report_progress(protocol, BINDSTATUS_CONNECTING, (LPWSTR)status_info);
        break;

    case INTERNET_STATUS_SENDING_REQUEST:
        TRACE("%p INTERNET_STATUS_SENDING_REQUEST\n", protocol);
        report_progress(protocol, BINDSTATUS_SENDINGREQUEST, (LPWSTR)status_info);
        break;

    case INTERNET_STATUS_REDIRECT:
        TRACE("%p INTERNET_STATUS_REDIRECT\n", protocol);
        report_progress(protocol, BINDSTATUS_REDIRECTING, (LPWSTR)status_info);
        break;

    case INTERNET_STATUS_REQUEST_COMPLETE:
        request_complete(protocol, status_info);
        break;

    case INTERNET_STATUS_HANDLE_CREATED:
        TRACE("%p INTERNET_STATUS_HANDLE_CREATED\n", protocol);
        IInternetProtocol_AddRef(protocol->protocol);
        break;

    case INTERNET_STATUS_HANDLE_CLOSING:
        TRACE("%p INTERNET_STATUS_HANDLE_CLOSING\n", protocol);

        if(*(HINTERNET *)status_info == protocol->request) {
            protocol->request = NULL;
            if(protocol->protocol_sink) {
                IInternetProtocolSink_Release(protocol->protocol_sink);
                protocol->protocol_sink = NULL;
            }

            if(protocol->bind_info.cbSize) {
                ReleaseBindInfo(&protocol->bind_info);
                memset(&protocol->bind_info, 0, sizeof(protocol->bind_info));
            }
        }else if(*(HINTERNET *)status_info == protocol->connection) {
            protocol->connection = NULL;
        }

        IInternetProtocol_Release(protocol->protocol);
        break;

    default:
        WARN("Unhandled Internet status callback %d\n", internet_status);
    }
}
Exemplo n.º 2
0
static void
request_complete_list(struct usba_ep *ep, struct list_head *list, int status)
{
    struct usba_request *req, *tmp_req;

    list_for_each_entry_safe(req, tmp_req, list, queue) {
        list_del_init(&req->queue);
        request_complete(ep, req, status);
    }
Exemplo n.º 3
0
static void
on_read(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
  if (nread < 0) {
    if (buf->base)
      free(buf->base);
    /* FIXME
    uv_shutdown_t* shutdown_req = (uv_shutdown_t*) malloc(sizeof(uv_shutdown_t));
    if (shutdown_req == NULL) {
      fprintf(stderr, "Allocate error\n");
      uv_close((uv_handle_t*) stream, on_close);
      return;
    }
    uv_shutdown(shutdown_req, stream, on_shutdown);
    */
    uv_close((uv_handle_t*) stream, on_close);
    return;
  }

  if (nread == 0) {
    free(buf->base);
    return;
  }

  http_request* request = calloc(1, sizeof(http_request));
  if (request == NULL) {
    free(buf->base);
    fprintf(stderr, "Allocate error: %s\n", strerror(errno));
    uv_close((uv_handle_t*) stream, on_close);
    return;
  }
  stream->data = request;

  request->handle = (uv_handle_t*) stream;

  request->num_headers = sizeof(request->headers) / sizeof(request->headers[0]);
  int nparsed = phr_parse_request(
          buf->base,
          buf->len,
          &request->method,
          &request->method_len,
          &request->path,
          &request->path_len,
          &request->minor_version,
          request->headers,
          &request->num_headers,
          0);
  if (nparsed < 0) {
    free(request);
    free(buf->base);
    fprintf(stderr, "Invalid request\n");
    uv_close((uv_handle_t*) stream, on_close);
    return;
  }
  /*
  int cl = content_length(request);
  if (cl >= 0 && cl < buf->len - nparsed) {
    free(request);
    free(buf->base);
    return;
  }
  */
  /* TODO: handle reading whole payload */
  request->payload = buf->base + nparsed;
  request->payload_len = buf->len - nparsed;
  request_complete(request);
  free(buf->base);
}
Exemplo n.º 4
0
static void data_in_do_read(nxe_ostream* os, nxe_istream* is) {
  nxd_http_client_proto* hcp=(nxd_http_client_proto*)((char*)os-offsetof(nxd_http_client_proto, data_in));
  nxe_loop* loop=os->super.loop;
  if (hcp->state==HCP_WAITING_FOR_RESPONSE) {
    nxe_unset_timer(loop, NXWEB_TIMER_READ, &hcp->timer_read);
    nxe_unset_timer(loop, NXWEB_TIMER_100CONTINUE, &hcp->timer_100_continue);
    nxe_set_timer(loop, NXWEB_TIMER_READ, &hcp->timer_read);
    nxb_make_room(hcp->nxb, NXWEB_MAX_REQUEST_HEADERS_SIZE);
    hcp->state=HCP_RECEIVING_HEADERS;
  }

  if (hcp->state==HCP_RECEIVING_HEADERS) {
    int size;
    nxe_flags_t flags=0;
    void* ptr=nxb_get_room(hcp->nxb, &size);
    int bytes_received=ISTREAM_CLASS(is)->read(is, os, ptr, size, &flags);
    if (bytes_received) {
      nxb_blank_fast(hcp->nxb, bytes_received);
      int read_buf_size;
      char* read_buf=nxb_get_unfinished(hcp->nxb, &read_buf_size);
      char* end_of_headers;
      char* start_of_body;
      if ((end_of_headers=_nxweb_find_end_of_http_headers(read_buf, read_buf_size, &start_of_body))) {
        nxb_finish_stream(hcp->nxb, 0);
        memset(&hcp->resp, 0, sizeof(hcp->resp));
        hcp->resp.nxb=hcp->nxb;
        hcp->resp.cdstate.monitor_only=hcp->chunked_do_not_decode;
        if (_nxweb_parse_http_response(&hcp->resp, read_buf, end_of_headers)) {
          // bad response
          nxe_unset_timer(loop, NXWEB_TIMER_READ, &hcp->timer_read);
          nxe_ostream_unset_ready(os);
          nxe_publish(&hcp->events_pub, (nxe_data)NXD_HCP_BAD_RESPONSE);
          return;
        }
        if (hcp->receiving_100_continue) {
          hcp->receiving_100_continue=0;
          if (hcp->resp.status_code==100) {
            // back to sending request body
            nxe_ostream_unset_ready(&hcp->data_in);
            nxe_unset_timer(loop, NXWEB_TIMER_READ, &hcp->timer_read);
            nxe_set_timer(loop, NXWEB_TIMER_WRITE, &hcp->timer_write);
            nxe_istream_set_ready(loop, &hcp->data_out);
            hcp->state=HCP_SENDING_BODY;
            return;
          }
          else {
            nxe_publish(&hcp->events_pub, (nxe_data)NXD_HCP_NO_100CONTINUE);
          }
        }
        char* read_buf_end=read_buf+read_buf_size;
        if (start_of_body<read_buf_end) {
          hcp->first_body_chunk=start_of_body;
          hcp->first_body_chunk_end=read_buf_end;

          nxe_size_t first_body_chunk_size=hcp->first_body_chunk_end-hcp->first_body_chunk;
          if (hcp->resp.chunked_encoding) {
            int r=_nxweb_decode_chunked_stream(&hcp->resp.cdstate, hcp->first_body_chunk, &first_body_chunk_size);
            hcp->first_body_chunk_end=hcp->first_body_chunk+first_body_chunk_size;
            if (r<0) nxe_publish(&hcp->events_pub, (nxe_data)NXD_HCP_RESPONSE_CHUNKED_ENCODING_ERROR);
            else if (r>0) hcp->response_body_complete=1;
          }
          else if (first_body_chunk_size >= hcp->resp.content_length && hcp->resp.content_length>=0) {
            hcp->response_body_complete=1;
          }
        }
        else {
          hcp->first_body_chunk=0;
          hcp->first_body_chunk_end=0;
        }
        nxe_ostream_unset_ready(os);
        nxe_publish(&hcp->events_pub, (nxe_data)NXD_HCP_RESPONSE_RECEIVED);
        if (hcp->resp.content_length && !hcp->req->head_method) { // is body expected?
          hcp->state=HCP_RECEIVING_BODY;
          nxe_istream_set_ready(loop, &hcp->resp_body_out);
          if (hcp->resp_body_out.pair) {
            nxe_ostream_set_ready(loop, os);
          }
        }
        else {
          // rearm connection
          request_complete(hcp, loop);
        }
      }
      else {
        if (read_buf_size>=NXWEB_MAX_REQUEST_HEADERS_SIZE) {
          // bad response (headers too large)
          nxe_unset_timer(loop, NXWEB_TIMER_READ, &hcp->timer_read);
          nxe_ostream_unset_ready(os);
          nxe_publish(&hcp->events_pub, (nxe_data)NXD_HCP_BAD_RESPONSE);
          return;
        }
      }
    }
  }
  else if (hcp->state==HCP_RECEIVING_BODY) {
    nxe_ostream* next_os=hcp->resp_body_out.pair;
    if (next_os) {
      if (next_os->ready) OSTREAM_CLASS(next_os)->do_read(next_os, &hcp->resp_body_out);
      if (!next_os->ready) {
        nxe_ostream_unset_ready(os);
        nxe_istream_set_ready(loop, &hcp->resp_body_out); // get notified when next_os becomes ready again
      }
    }
    else {
      nxweb_log_error("no connected device for hcp->resp_body_out");
      nxe_ostream_unset_ready(os);
    }
  }
}
Exemplo n.º 5
0
static nxe_size_t resp_body_out_read(nxe_istream* is, nxe_ostream* os, void* ptr, nxe_size_t size, nxe_flags_t* flags) {
  nxd_http_client_proto* hcp=(nxd_http_client_proto*)((char*)is-offsetof(nxd_http_client_proto, resp_body_out));
  nxe_loop* loop=is->super.loop;

  if (hcp->request_complete) {
    nxweb_log_error("hcp->request_complete - resp_body_out_read() should not be called");
    *flags|=NXEF_EOF;
    nxe_istream_unset_ready(is);
    return 0;
  }

  if (hcp->state!=HCP_RECEIVING_BODY) {
    nxe_istream_unset_ready(is);
    nxe_ostream_set_ready(loop, &hcp->data_in); // get notified when prev_is ready
    return 0;
  }

  nxe_size_t bytes_received=0;

  if (hcp->first_body_chunk) {
    nxe_size_t first_body_chunk_size=hcp->first_body_chunk_end-hcp->first_body_chunk;
    if (first_body_chunk_size<=size) {
      bytes_received=first_body_chunk_size;
      memcpy(ptr, hcp->first_body_chunk, bytes_received);
      hcp->first_body_chunk=0;
      hcp->first_body_chunk_end=0;
    }
    else {
      bytes_received=size;
      memcpy(ptr, hcp->first_body_chunk, size);
      hcp->first_body_chunk+=size;
    }
    hcp->resp.content_received+=bytes_received;
    if (hcp->response_body_complete) {
      // rearm connection
      request_complete(hcp, loop);
      nxe_istream_unset_ready(is);
      *flags|=NXEF_EOF;
      return bytes_received;
    }
    ptr+=bytes_received;
    size-=bytes_received;
  }

  if (size>0) {
    nxe_istream* prev_is=hcp->data_in.pair;
    if (prev_is) {
      nxe_size_t bytes_received2=0;
      nxe_flags_t rflags=0;
      if (prev_is->ready) bytes_received2=ISTREAM_CLASS(prev_is)->read(prev_is, &hcp->data_in, ptr, size, &rflags);
      if (!prev_is->ready) {
        nxe_istream_unset_ready(is);
        nxe_ostream_set_ready(loop, &hcp->data_in); // get notified when prev_is becomes ready again
      }

      if (hcp->resp.chunked_encoding) {
        int r=_nxweb_decode_chunked_stream(&hcp->resp.cdstate, ptr, &bytes_received2);
        hcp->resp.content_received+=bytes_received2;
        bytes_received+=bytes_received2;
        if (r<0) nxe_publish(&hcp->events_pub, (nxe_data)NXD_HCP_RESPONSE_CHUNKED_ENCODING_ERROR);
        else if (r>0) hcp->response_body_complete=1;
      }
      else {
        hcp->resp.content_received+=bytes_received2;
        bytes_received+=bytes_received2;
        if (hcp->resp.content_received >= hcp->resp.content_length && hcp->resp.content_length>=0) {
          hcp->response_body_complete=1;
        }
      }
      if (hcp->response_body_complete) {
        // rearm connection
        request_complete(hcp, loop);
        nxe_istream_unset_ready(is);
        *flags|=NXEF_EOF;
        return bytes_received;
      }
    }
    else {
      if (!prev_is) nxweb_log_error("no connected device for hcp->data_in");
      nxe_istream_unset_ready(is);
    }
  }

  return bytes_received;
}
Exemplo n.º 6
0
static bool can_read_data(struct io *io, void *user_data)
{
	struct mgmt *mgmt = user_data;
	struct mgmt_hdr *hdr;
	struct mgmt_ev_cmd_complete *cc;
	struct mgmt_ev_cmd_status *cs;
	ssize_t bytes_read;
	uint16_t opcode, event, index, length;

	bytes_read = read(mgmt->fd, mgmt->buf, mgmt->len);
	if (bytes_read < 0)
		return false;

	util_hexdump('>', mgmt->buf, bytes_read,
				mgmt->debug_callback, mgmt->debug_data);

	if (bytes_read < MGMT_HDR_SIZE)
		return true;

	hdr = mgmt->buf;
	event = btohs(hdr->opcode);
	index = btohs(hdr->index);
	length = btohs(hdr->len);

	if (bytes_read < length + MGMT_HDR_SIZE)
		return true;

	switch (event) {
	case MGMT_EV_CMD_COMPLETE:
		cc = mgmt->buf + MGMT_HDR_SIZE;
		opcode = btohs(cc->opcode);

		util_debug(mgmt->debug_callback, mgmt->debug_data,
				"[0x%04x] command 0x%04x complete: 0x%02x",
						index, opcode, cc->status);

		request_complete(mgmt, cc->status, opcode, index, length - 3,
						mgmt->buf + MGMT_HDR_SIZE + 3);
		break;
	case MGMT_EV_CMD_STATUS:
		cs = mgmt->buf + MGMT_HDR_SIZE;
		opcode = btohs(cs->opcode);

		util_debug(mgmt->debug_callback, mgmt->debug_data,
				"[0x%04x] command 0x%02x status: 0x%02x",
						index, opcode, cs->status);

		request_complete(mgmt, cs->status, opcode, index, 0, NULL);
		break;
	default:
		util_debug(mgmt->debug_callback, mgmt->debug_data,
				"[0x%04x] event 0x%04x", index, event);

		process_notify(mgmt, event, index, length,
						mgmt->buf + MGMT_HDR_SIZE);
		break;
	}

	if (mgmt->destroyed)
		return false;

	return true;
}
Exemplo n.º 7
0
static gboolean received_data(GIOChannel *channel, GIOCondition cond,
							gpointer user_data)
{
	struct mgmt *mgmt = user_data;
	struct mgmt_hdr *hdr;
	struct mgmt_ev_cmd_complete *cc;
	struct mgmt_ev_cmd_status *cs;
	ssize_t bytes_read;
	uint16_t opcode, event, index, length;

	if (cond & (G_IO_HUP | G_IO_ERR | G_IO_NVAL))
		return FALSE;

	bytes_read = read(mgmt->fd, mgmt->buf, mgmt->len);
	if (bytes_read < 0)
		return TRUE;

	util_hexdump('>', mgmt->buf, bytes_read,
				mgmt->debug_callback, mgmt->debug_data);

	if (bytes_read < MGMT_HDR_SIZE)
		return TRUE;

	hdr = mgmt->buf;
	event = btohs(hdr->opcode);
	index = btohs(hdr->index);
	length = btohs(hdr->len);

	if (bytes_read < length + MGMT_HDR_SIZE)
		return TRUE;

	switch (event) {
	case MGMT_EV_CMD_COMPLETE:
		cc = mgmt->buf + MGMT_HDR_SIZE;
		opcode = btohs(cc->opcode);

		util_debug(mgmt->debug_callback, mgmt->debug_data,
				"[0x%04x] command 0x%04x complete: 0x%02x",
						index, opcode, cc->status);

		request_complete(mgmt, cc->status, opcode, index, length - 3,
						mgmt->buf + MGMT_HDR_SIZE + 3);
		break;
	case MGMT_EV_CMD_STATUS:
		cs = mgmt->buf + MGMT_HDR_SIZE;
		opcode = btohs(cs->opcode);

		util_debug(mgmt->debug_callback, mgmt->debug_data,
				"[0x%04x] command 0x%02x status: 0x%02x",
						index, opcode, cs->status);

		request_complete(mgmt, cs->status, opcode, index, 0, NULL);
		break;
	default:
		util_debug(mgmt->debug_callback, mgmt->debug_data,
				"[0x%04x] event 0x%04x", index, event);

		process_notify(mgmt, event, index, length,
						mgmt->buf + MGMT_HDR_SIZE);
		break;
	}

	if (mgmt->destroyed)
		return FALSE;

	return TRUE;
}
Exemplo n.º 8
0
static void do_profiler_stats(const struct b2r2_blt_request * const req,
	const struct b2r2_blt_profiling_info * const info)
{
	s32 blt_px;
	s32 blt_time;
	s32 blt_mpix_per_second;

	if (time_before(jiffies, profiler_stats.sampling_start_time_jiffies)) {
		/* Flush cached entries */
		clear_profiling_info(cache, BLIT_CACHE_SIZE, req);
		return;
	}

	/* Save fastest and slowest blit */
	blt_px = info->pixels;
	blt_time = nsec_2_usec((s32)(info->nsec_active_in_cpu +
			info->nsec_active_in_b2r2));
	blt_mpix_per_second = get_mpix_per_second(blt_px, blt_time);

	if (blt_mpix_per_second <= profiler_stats.min_mpix_per_second) {
		profiler_stats.min_mpix_per_second = blt_mpix_per_second;
		memcpy(&profiler_stats.min_blt_request,
			&req->user_req,
			sizeof(profiler_stats.min_blt_request));
		memcpy(&profiler_stats.min_blt_profiling_info,
			info,
			sizeof(struct b2r2_blt_profiling_info));
	}

	if (blt_mpix_per_second >= profiler_stats.max_mpix_per_second) {
		profiler_stats.max_mpix_per_second = blt_mpix_per_second;
		memcpy(&profiler_stats.max_blt_request,
			&req->user_req,
			sizeof(profiler_stats.max_blt_request));
		memcpy(&profiler_stats.max_blt_profiling_info,
			info,
			sizeof(struct b2r2_blt_profiling_info));
	}

	profiler_stats.num_pixels[info->core_id] += blt_px;
	profiler_stats.num_usecs[info->core_id] += blt_time;

	/* Save stats to cache */
	if (!request_complete(cache, BLIT_CACHE_SIZE, req, info)) {
		save_profiling_info(cache, BLIT_CACHE_SIZE, info);
		return;
	}

	/* Calculate the stats for the entire blit job */
	blt_px = get_accumulated_pixels(cache, BLIT_CACHE_SIZE,
			req, info);
	blt_time = get_accumulated_usecs(cache, BLIT_CACHE_SIZE,
			req, info);

	/* Flush cached entries */
	clear_profiling_info(cache, BLIT_CACHE_SIZE, req);

	/* Accumulate stats */
	profiler_stats.accumulated_num_pixels += blt_px;
	profiler_stats.accumulated_num_usecs += blt_time;
	profiler_stats.num_blts_done++;

	/* Print stats when we reach the configured number of blits to use */
	if (profiler_stats.num_blts_done >= profiler_stats_blts_used) {
		print_profiler_stats();
		reset_profiler_stats();

		/*
		 * The printouts initiated above can disturb the next
		 * measurement so we delay it two seconds to give the
		 * printouts a chance to finish.
		 */
		profiler_stats.sampling_start_time_jiffies =
				jiffies + (2 * HZ);
	}
}