Exemplo n.º 1
0
R_API int r_socket_proc_gets (RSocketProc *sp, char *buf, int size) {
	RSocket s;
	s.is_ssl = false;
	s.fd = sp->fd1[0];
	return r_socket_gets (&s, buf, size);
}
Exemplo n.º 2
0
R_API RSocketHTTPRequest *r_socket_http_accept (RSocket *s, int timeout) {
	int content_length = 0, xx, yy;
	int pxx = 1, first = 0;
	char buf[1500], *p, *q;
	RSocketHTTPRequest *hr = R_NEW0 (RSocketHTTPRequest);
	hr->s = r_socket_accept (s);
	if (!hr->s) {
		free (hr);
		return NULL;
	}
	if (timeout>0)
		r_socket_block_time (hr->s, 1, timeout);
	for (;;) {
#if __WINDOWS__
		if (breaked)
			break;
#endif
		memset (buf, 0, sizeof (buf));
		xx = r_socket_gets (hr->s, buf, sizeof (buf));
		yy = r_socket_ready (hr->s, 0, 20 * 1000); //this function uses usecs as argument
//		eprintf ("READ %d (%s) READY %d\n", xx, buf, yy);
		if (!yy || (!xx && !pxx)) {
			break;
		}
		pxx = xx;

		if (first==0) {
			first = 1;
			if (strlen (buf)<3) {
				r_socket_http_close (hr);
				return NULL;
			}
			p = strchr (buf, ' ');
			if (p) *p = 0;
			hr->method = strdup (buf);
			if (p) {
				q = strstr (p+1, " HTTP"); //strchr (p+1, ' ');
				if (q) *q = 0;
				hr->path = strdup (p+1);
			}
		} else {
			if (!hr->referer && !strncmp (buf, "Referer: ", 9)) {
				hr->referer = strdup (buf + 9);
			} else
			if (!hr->agent && !strncmp (buf, "User-Agent: ", 12)) {
				hr->agent = strdup (buf + 12);
			} else
			if (!hr->host && !strncmp (buf, "Host: ", 6)) {
				hr->host = strdup (buf + 6);
			} else
			if (!strncmp (buf, "Content-Length: ", 16)) {
				content_length = atoi (buf+16);
			}
		}
	}
	if (content_length>0) {
		r_socket_read_block (hr->s, (ut8*)buf, 1); // one missing byte wtf
		hr->data = malloc (content_length+1);
		hr->data_length = content_length;
		r_socket_read_block (hr->s, hr->data, hr->data_length);
		hr->data[content_length] = 0;
	}
	return hr;
}