added mio_svc_htts_setservernamewithbcstr()
This commit is contained in:
		@ -902,7 +902,6 @@ int main (int argc, char* argv[])
 | 
			
		||||
	tcp_bind.options |= MIO_DEV_SCK_BIND_SSL; 
 | 
			
		||||
	tcp_bind.ssl_certfile = "localhost.crt";
 | 
			
		||||
	tcp_bind.ssl_keyfile = "localhost.key";
 | 
			
		||||
	MIO_INIT_NTIME (&tcp_bind.ssl_accept_tmout, 5, 1);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
	if (mio_dev_sck_bind(tcp[2], &tcp_bind) <= -1)
 | 
			
		||||
@ -912,6 +911,7 @@ int main (int argc, char* argv[])
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	tcp_lstn.backlogs = 100;
 | 
			
		||||
	MIO_INIT_NTIME (&tcp_lstn.accept_tmout, 5, 1);
 | 
			
		||||
	if (mio_dev_sck_listen(tcp[2], &tcp_lstn) <= -1)
 | 
			
		||||
	{
 | 
			
		||||
		MIO_INFO1 (mio, "tcp[2] mio_dev_sck_listen() failed - %js\n", mio_geterrmsg(mio));
 | 
			
		||||
@ -977,7 +977,7 @@ for (i = 0; i < 5; i++)
 | 
			
		||||
 | 
			
		||||
	dnc = mio_svc_dnc_start(mio, &servaddr, MIO_NULL, &send_tmout, &reply_tmout, 2); /* option - send to all, send one by one */
 | 
			
		||||
	htts = mio_svc_htts_start(mio, &htts_bind_addr);
 | 
			
		||||
 | 
			
		||||
	mio_svc_htts_setservernamewithbcstr (htts, "MIO-HTTP");
 | 
			
		||||
#if 1
 | 
			
		||||
	{
 | 
			
		||||
		mio_dns_bqr_t qrs[] = 
 | 
			
		||||
 | 
			
		||||
@ -7,6 +7,8 @@ struct mio_svc_htts_t
 | 
			
		||||
	MIO_SVC_HEADER;
 | 
			
		||||
 | 
			
		||||
	mio_dev_sck_t* lsck;
 | 
			
		||||
	mio_bch_t* server_name;
 | 
			
		||||
	mio_bch_t server_name_buf[64];
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct mio_svc_httc_t
 | 
			
		||||
@ -14,7 +16,6 @@ struct mio_svc_httc_t
 | 
			
		||||
	MIO_SVC_HEADER;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
struct mio_htts_client_t
 | 
			
		||||
{
 | 
			
		||||
	mio_skad_t remote_addr;
 | 
			
		||||
@ -396,35 +397,39 @@ static void listener_on_disconnect (mio_dev_sck_t* sck)
 | 
			
		||||
	switch (MIO_DEV_SCK_GET_PROGRESS(sck))
 | 
			
		||||
	{
 | 
			
		||||
		case MIO_DEV_SCK_CONNECTING:
 | 
			
		||||
			/* only for connecting sockets */
 | 
			
		||||
			MIO_INFO1 (sck->mio, "OUTGOING SESSION DISCONNECTED - FAILED TO CONNECT (%d) TO REMOTE SERVER\n", (int)sck->sck);
 | 
			
		||||
			break;
 | 
			
		||||
 | 
			
		||||
		case MIO_DEV_SCK_CONNECTING_SSL:
 | 
			
		||||
			/* only for connecting sockets */
 | 
			
		||||
			MIO_INFO1 (sck->mio, "OUTGOING SESSION DISCONNECTED - FAILED TO SSL-CONNECT (%d) TO REMOTE SERVER\n", (int)sck->sck);
 | 
			
		||||
			break;
 | 
			
		||||
 | 
			
		||||
		case MIO_DEV_SCK_LISTENING:
 | 
			
		||||
			MIO_INFO1 (sck->mio, "SHUTTING DOWN THE SERVER SOCKET(%d)...\n", (int)sck->sck);
 | 
			
		||||
		case MIO_DEV_SCK_CONNECTED:
 | 
			
		||||
			/* only for connecting sockets */
 | 
			
		||||
			MIO_INFO1 (sck->mio, "OUTGOING CLIENT CONNECTION GOT TORN DOWN %p(%d).......\n", (int)sck->sck);
 | 
			
		||||
			break;
 | 
			
		||||
 | 
			
		||||
		case MIO_DEV_SCK_CONNECTED:
 | 
			
		||||
			MIO_INFO1 (sck->mio, "OUTGOING CLIENT CONNECTION GOT TORN DOWN(%d).......\n", (int)sck->sck);
 | 
			
		||||
		case MIO_DEV_SCK_LISTENING:
 | 
			
		||||
			MIO_INFO2 (sck->mio, "LISTNER SOCKET %p(%d) - SHUTTUING DOWN\n", sck, (int)sck->sck);
 | 
			
		||||
			break;
 | 
			
		||||
 | 
			
		||||
		case MIO_DEV_SCK_ACCEPTING_SSL:
 | 
			
		||||
			MIO_INFO1 (sck->mio, "LISTENER INCOMING SSL-ACCEPT GOT DISCONNECTED(%d) ....\n", (int)sck->sck);
 | 
			
		||||
			MIO_INFO2 (sck->mio, "LISTENER INCOMING SSL-ACCEPT GOT DISCONNECTED %p(%d) ....\n", sck, (int)sck->sck);
 | 
			
		||||
			break;
 | 
			
		||||
 | 
			
		||||
		case MIO_DEV_SCK_ACCEPTED:
 | 
			
		||||
			MIO_INFO1 (sck->mio, "LISTENER INCOMING CLIENT BEING SERVED GOT DISCONNECTED(%d).......\n", (int)sck->sck);
 | 
			
		||||
			/* only for sockets accepted by the listeners. will never come here because
 | 
			
		||||
			 * the disconnect call for such sockets have been changed in listener_on_connect() */
 | 
			
		||||
			MIO_INFO2 (sck->mio, "ACCEPTED SOCKET %p(%d) GOT DISCONNECTED.......\n", sck, (int)sck->sck);
 | 
			
		||||
			break;
 | 
			
		||||
 | 
			
		||||
		default:
 | 
			
		||||
			MIO_INFO1 (sck->mio, "LISTENER DISCONNECTED AFTER ALL(%d).......\n", (int)sck->sck);
 | 
			
		||||
			MIO_INFO2 (sck->mio, "SOCKET %p(%d) DISCONNECTED AFTER ALL.......\n", sck, (int)sck->sck);
 | 
			
		||||
			break;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	/* the client sockets are finalized in clinet_on_disconnect().
 | 
			
		||||
	 * for a listener socket, these fields must be NULL */
 | 
			
		||||
	MIO_ASSERT (sck->mio, sckxtn->c.htrd == MIO_NULL);
 | 
			
		||||
@ -482,15 +487,22 @@ mio_svc_htts_t* mio_svc_htts_start (mio_t* mio, const mio_skad_t* bind_addr)
 | 
			
		||||
 | 
			
		||||
	MIO_MEMSET (&info, 0, MIO_SIZEOF(info));
 | 
			
		||||
	info.b.localaddr = *bind_addr;
 | 
			
		||||
	info.b.options = MIO_DEV_SCK_BIND_REUSEADDR | MIO_DEV_SCK_BIND_REUSEPORT | MIO_DEV_SCK_BIND_LENIENT;
 | 
			
		||||
	/*MIO_INIT_NTIME (&info.b.ssl_accept_tmout, 5, 1);*/
 | 
			
		||||
	info.b.options = MIO_DEV_SCK_BIND_REUSEADDR | MIO_DEV_SCK_BIND_REUSEPORT;
 | 
			
		||||
	if (mio_dev_sck_bind(htts->lsck, &info.b) <= -1) goto oops;
 | 
			
		||||
 | 
			
		||||
	MIO_MEMSET (&info, 0, MIO_SIZEOF(info));
 | 
			
		||||
	info.l.options = MIO_DEV_SCK_LISTEN_LENIENT;
 | 
			
		||||
	info.l.backlogs = 255;
 | 
			
		||||
	MIO_INIT_NTIME (&info.l.accept_tmout, 5, 1);
 | 
			
		||||
	if (mio_dev_sck_listen(htts->lsck, &info.l) <= -1) goto oops;
 | 
			
		||||
 | 
			
		||||
printf ("** HTTS LISTENER SOCKET %p\n", htts->lsck);
 | 
			
		||||
	mio_fmttobcstr (htts->mio, htts->server_name_buf, MIO_COUNTOF(htts->server_name_buf), "%s-%d.%d.%d", 
 | 
			
		||||
		MIO_PACKAGE_NAME, (int)MIO_PACKAGE_VERSION_MAJOR, (int)MIO_PACKAGE_VERSION_MINOR, (int)MIO_PACKAGE_VERSION_PATCH);
 | 
			
		||||
	htts->server_name = htts->server_name_buf;
 | 
			
		||||
 | 
			
		||||
	MIO_SVCL_APPEND_SVC (&mio->actsvc, (mio_svc_t*)htts);
 | 
			
		||||
 | 
			
		||||
	MIO_DEBUG3 (mio, "STARTED SVC(HTTS) LISTENER %p LISTENER SOCKET %p(%d)\n", htts, htts->lsck, (int)htts->lsck->sck);
 | 
			
		||||
	return htts;
 | 
			
		||||
 | 
			
		||||
oops:
 | 
			
		||||
@ -506,14 +518,37 @@ void mio_svc_htts_stop (mio_svc_htts_t* htts)
 | 
			
		||||
{
 | 
			
		||||
	mio_t* mio = htts->mio;
 | 
			
		||||
 | 
			
		||||
	MIO_DEBUG3 (mio, "STOPPING SVC(HTTS) %p LISTENER SOCKET %p(%d)\n", htts, htts->lsck, (int)(htts->lsck? htts->lsck->sck: -1));
 | 
			
		||||
 | 
			
		||||
	/* htts->lsck may be null if the socket has been destroyed for operational error and 
 | 
			
		||||
	 * forgotten in the disconnect callback thereafter */
 | 
			
		||||
	if (htts->lsck) mio_dev_sck_kill (htts->lsck);
 | 
			
		||||
	/*if (dnc->lsck) mio_dev_sck_kill (dnc->lsck);
 | 
			
		||||
	while (dnc->pending_req) release_dns_msg (dnc, dnc->pending_req);*/
 | 
			
		||||
 | 
			
		||||
	if (htts->server_name && htts->server_name != htts->server_name_buf) mio_freemem (mio, htts->server_name);
 | 
			
		||||
 | 
			
		||||
	MIO_SVCL_UNLINK_SVC (htts);
 | 
			
		||||
	mio_freemem (mio, htts);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int mio_svc_htts_setservernamewithbcstr (mio_svc_htts_t* htts, const mio_bch_t* name)
 | 
			
		||||
{
 | 
			
		||||
	mio_t* mio = htts->mio;
 | 
			
		||||
	mio_bch_t* tmp;
 | 
			
		||||
 | 
			
		||||
	if (mio_copy_bcstr(htts->server_name_buf, MIO_COUNTOF(htts->server_name_buf), name) == mio_count_bcstr(name))
 | 
			
		||||
	{
 | 
			
		||||
		tmp = htts->server_name_buf;
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		tmp = mio_dupbcstr(mio, name, MIO_NULL);
 | 
			
		||||
		if (!tmp) return -1;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (htts->server_name && htts->server_name != htts->server_name_buf) mio_freemem (mio, htts->server_name);
 | 
			
		||||
	htts->server_name = tmp;
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void mio_svc_htts_sendstatus (mio_svc_htts_t* htts, mio_dev_sck_t* csck, int status_code, mio_http_method_t method, const mio_http_version_t* version, int keepalive, void* extra)
 | 
			
		||||
{
 | 
			
		||||
@ -579,7 +614,7 @@ void mio_svc_htts_sendstatus (mio_svc_htts_t* htts, mio_dev_sck_t* csck, int sta
 | 
			
		||||
 | 
			
		||||
	mio_becs_fmt (csckxtn->c.sbuf, "HTTP/%d.%d %d %s\r\nServer: %s\r\nDate: %s\r\nConnection: %s\r\nContent-Type: text/html\r\nContent-Length: %u\r\n%s%s%s\r\n%s",
 | 
			
		||||
		version->major, version->minor, status_code, mio_http_status_to_bcstr(status_code), 
 | 
			
		||||
		"SERVER-NAME", /* TODO: use actual name mio_svc_htts_getservername(). */
 | 
			
		||||
		htts->server_name,
 | 
			
		||||
		dtbuf, /* DATE */
 | 
			
		||||
		(keepalive? "keep-alive": "close"), /* connection */
 | 
			
		||||
		mio_count_bcstr(text), /* content length */
 | 
			
		||||
@ -597,7 +632,6 @@ void mio_svc_htts_sendstatus (mio_svc_htts_t* htts, mio_dev_sck_t* csck, int sta
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void mio_svc_htts_fmtgmtime (mio_svc_htts_t* htts, const mio_ntime_t* nt, mio_bch_t* buf, mio_oow_t len)
 | 
			
		||||
{
 | 
			
		||||
	mio_ntime_t now;
 | 
			
		||||
 | 
			
		||||
@ -258,6 +258,10 @@ MIO_EXPORT void mio_svc_htts_stop (
 | 
			
		||||
	mio_svc_htts_t* htts
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
MIO_EXPORT int mio_svc_htts_setservernamewithbcstr (
 | 
			
		||||
	mio_svc_htts_t*  htts,
 | 
			
		||||
	const mio_bch_t* server_name
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
MIO_EXPORT void mio_svc_htts_sendstatus (
 | 
			
		||||
	mio_svc_htts_t*           htts,
 | 
			
		||||
 | 
			
		||||
@ -315,7 +315,6 @@ enum mio_dev_sck_bind_option_t
 | 
			
		||||
/* TODO: more options --- SO_RCVBUF, SO_SNDBUF, SO_RCVTIMEO, SO_SNDTIMEO, SO_KEEPALIVE */
 | 
			
		||||
/*   BINDTODEVICE??? */
 | 
			
		||||
 | 
			
		||||
	MIO_DEV_SCK_BIND_LENIENT     = (1 << 14), /* for now, accept failure doesn't affect the listing socket if this is set */
 | 
			
		||||
	MIO_DEV_SCK_BIND_SSL         = (1 << 15)
 | 
			
		||||
};
 | 
			
		||||
typedef enum mio_dev_sck_bind_option_t mio_dev_sck_bind_option_t;
 | 
			
		||||
@ -329,7 +328,6 @@ struct mio_dev_sck_bind_t
 | 
			
		||||
 | 
			
		||||
	const mio_bch_t* ssl_certfile;
 | 
			
		||||
	const mio_bch_t* ssl_keyfile;
 | 
			
		||||
	mio_ntime_t ssl_accept_tmout;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
enum mio_dev_sck_connect_option_t
 | 
			
		||||
@ -346,10 +344,18 @@ struct mio_dev_sck_connect_t
 | 
			
		||||
	mio_ntime_t connect_tmout;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
enum mio_dev_sck_listen_option_t
 | 
			
		||||
{
 | 
			
		||||
	MIO_DEV_SCK_LISTEN_LENIENT     = (1 << 0) /* for now, accept failure doesn't affect the listing socket if this is set */
 | 
			
		||||
};
 | 
			
		||||
typedef enum mio_dev_sck_listen_option_t mio_dev_sck_listen_option_t;
 | 
			
		||||
 | 
			
		||||
typedef struct mio_dev_sck_listen_t mio_dev_sck_listen_t;
 | 
			
		||||
struct mio_dev_sck_listen_t
 | 
			
		||||
{
 | 
			
		||||
	int options;
 | 
			
		||||
	int backlogs;
 | 
			
		||||
	mio_ntime_t accept_tmout;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct mio_dev_sck_t
 | 
			
		||||
 | 
			
		||||
@ -901,8 +901,6 @@ static int dev_sck_ioctl (mio_dev_t* dev, int cmd, void* arg)
 | 
			
		||||
				                           SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
 | 
			
		||||
 | 
			
		||||
				SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_SSLv2); /* no outdated SSLv2 by default */
 | 
			
		||||
 | 
			
		||||
				rdev->tmout = bnd->ssl_accept_tmout;
 | 
			
		||||
			#else
 | 
			
		||||
				mio_seterrnum (mio, MIO_ENOIMPL);
 | 
			
		||||
				return -1;
 | 
			
		||||
@ -925,7 +923,6 @@ static int dev_sck_ioctl (mio_dev_t* dev, int cmd, void* arg)
 | 
			
		||||
			rdev->ssl_ctx = ssl_ctx;
 | 
			
		||||
		#endif
 | 
			
		||||
 | 
			
		||||
			if (bnd->options & MIO_DEV_SCK_BIND_LENIENT) rdev->state |= MIO_DEV_SCK_LENIENT;
 | 
			
		||||
			return 0;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
@ -1092,13 +1089,16 @@ fcntl (rdev->sck, F_SETFL, flags | O_NONBLOCK);
 | 
			
		||||
				return -1;
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			x = listen (rdev->sck, lstn->backlogs);
 | 
			
		||||
			x = listen(rdev->sck, lstn->backlogs);
 | 
			
		||||
			if (x == -1) 
 | 
			
		||||
			{
 | 
			
		||||
				mio_seterrwithsyserr (mio, 0, errno);
 | 
			
		||||
				return -1;
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			rdev->tmout = lstn->accept_tmout;
 | 
			
		||||
			if (lstn->options & MIO_DEV_SCK_LISTEN_LENIENT) rdev->state |= MIO_DEV_SCK_LENIENT;
 | 
			
		||||
 | 
			
		||||
			MIO_DEV_SCK_SET_PROGRESS (rdev, MIO_DEV_SCK_LISTENING);
 | 
			
		||||
			return 0;
 | 
			
		||||
		}
 | 
			
		||||
@ -1150,7 +1150,6 @@ static int harvest_outgoing_connection (mio_dev_sck_t* rdev)
 | 
			
		||||
	mio_t* mio = rdev->mio;
 | 
			
		||||
	int errcode;
 | 
			
		||||
	mio_scklen_t len;
 | 
			
		||||
	
 | 
			
		||||
 | 
			
		||||
	MIO_ASSERT (mio, !(rdev->state & MIO_DEV_SCK_CONNECTED));
 | 
			
		||||
 | 
			
		||||
@ -1284,7 +1283,6 @@ static int accept_incoming_connection (mio_dev_sck_t* rdev)
 | 
			
		||||
		return -1;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
accept_done:
 | 
			
		||||
	/* use rdev->dev_size when instantiating a client sck device
 | 
			
		||||
	 * instead of MIO_SIZEOF(mio_dev_sck_t). therefore, the 
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user