postHandler(struct mg_connection *conn, void *cbdata)
{
	int rc = 1;
	instanceConf_t* inst = (instanceConf_t*) cbdata;
	const struct mg_request_info *ri = mg_get_request_info(conn);
	struct conn_wrkr_s *connWrkr = mg_get_thread_pointer(conn);
	connWrkr->multiSub.nElem = 0;
	memset(&connWrkr->parseState, 0, sizeof(connWrkr->parseState));
	connWrkr->pri = ri;

	if (inst->bAddMetadata && connWrkr->scratchBufSize == 0) {
		connWrkr->pScratchBuf = calloc(1, INIT_SCRATCH_BUF_SIZE);
		if (!connWrkr->pScratchBuf) {
			mg_cry(conn, "%s() - could not alloc scratch buffer!\n", __FUNCTION__);
			rc = 500;
			FINALIZE;
		}
		connWrkr->scratchBufSize = INIT_SCRATCH_BUF_SIZE;
	}

	if (0 != strcmp(ri->request_method, "POST")) {
		/* Not a POST request */
		int ret = mg_get_request_link(conn, connWrkr->pReadBuf, connWrkr->readBufSize);
		mg_printf(conn,
		          "HTTP/1.1 405 Method Not Allowed\r\nConnection: close\r\n");
		mg_printf(conn, "Content-Type: text/plain\r\n\r\n");
		mg_printf(conn,
		          "%s method not allowed in the POST handler\n",
		          ri->request_method);
		if (ret >= 0) {
			mg_printf(conn,
			          "use a web tool to send a POST request to %s\n",
			          connWrkr->pReadBuf);
		}
		STATSCOUNTER_INC(statsCounter.ctrFailed, statsCounter.mutCtrFailed);
		rc = 405;
		FINALIZE;
	}

	if (ri->remote_addr[0] != '\0') {
		size_t len = strnlen(ri->remote_addr, sizeof(ri->remote_addr));
		prop.CreateOrReuseStringProp(&connWrkr->propRemoteAddr, (const uchar*)ri->remote_addr, len);
	}

	if (ri->content_length >= 0) {
		/* We know the content length in advance */
		if (ri->content_length > (long long) connWrkr->readBufSize) {
			connWrkr->pReadBuf = realloc(connWrkr->pReadBuf, ri->content_length+1);
			if (!connWrkr->pReadBuf) {
				mg_cry(conn, "%s() - realloc failed!\n", __FUNCTION__);
				FINALIZE;
			}
			connWrkr->readBufSize = ri->content_length+1;
		}
	} else {
		/* We must read until we find the end (chunked encoding
		 * or connection close), indicated my mg_read returning 0 */
	}

	if (ri->num_headers > 0) {
		int i;
		for (i = 0; i < ri->num_headers; i++) {
			if (!strcasecmp(ri->http_headers[i].name, "content-encoding") &&
					!strcasecmp(ri->http_headers[i].value, "gzip")) {
				connWrkr->parseState.content_compressed = 1;
			}
		}
	}

	while (1) {
		int count = mg_read(conn, connWrkr->pReadBuf, connWrkr->readBufSize);
		if (count > 0) {
			processData(inst, connWrkr, (const char*)connWrkr->pReadBuf, count);
		} else {
			break;
		}
	}

	/* submit remainder */
	doSubmitMsg(inst, connWrkr, connWrkr->pMsg, connWrkr->iMsg);
	multiSubmitFlush(&connWrkr->multiSub);

	mg_send_http_ok(conn, "text/plain", 0);
	rc = 200;

finalize_it:
	if (connWrkr->parseState.bzInitDone) {
		inflateEnd(&connWrkr->parseState.zstrm);
	}
	/* reset */
	connWrkr->iMsg = 0;

	return rc;
}
postHandler(struct mg_connection *conn, void *cbdata)
{
	int rc = 1;
	instanceConf_t* inst = (instanceConf_t*) cbdata;
	const struct mg_request_info *ri = mg_get_request_info(conn);
	struct conn_wrkr_s *connWrkr = mg_get_thread_pointer(conn);
	connWrkr->multiSub.nElem = 0;
	memset(&connWrkr->parseState, 0, sizeof(connWrkr->parseState));
	connWrkr->pri = ri;

	if (inst->bAddMetadata && connWrkr->scratchBufSize == 0) {
		connWrkr->pScratchBuf = calloc(1, INIT_SCRATCH_BUF_SIZE);
		if (!connWrkr->pScratchBuf) {
			mg_cry(conn, "%s() - could not alloc scratch buffer!\n", __FUNCTION__);
			rc = 500;
			FINALIZE;
		}
		connWrkr->scratchBufSize = INIT_SCRATCH_BUF_SIZE;
	}

	if (0 != strcmp(ri->request_method, "POST")) {
		 
		int ret = mg_get_request_link(conn, connWrkr->pReadBuf, connWrkr->readBufSize);
		mg_printf(conn,
		          "HTTP/1.1 405 Method Not Allowed\r\nConnection: close\r\n");
		mg_printf(conn, "Content-Type: text/plain\r\n\r\n");
		mg_printf(conn,
		          "%s method not allowed in the POST handler\n",
		          ri->request_method);
		if (ret >= 0) {
			mg_printf(conn,
			          "use a web tool to send a POST request to %s\n",
			          connWrkr->pReadBuf);
		}
		STATSCOUNTER_INC(statsCounter.ctrFailed, statsCounter.mutCtrFailed);
		rc = 405;
		FINALIZE;
	}

	if (ri->remote_addr[0] != '\0') {
		size_t len = strnlen(ri->remote_addr, sizeof(ri->remote_addr));
		prop.CreateOrReuseStringProp(&connWrkr->propRemoteAddr, (const uchar*)ri->remote_addr, len);
	}

	if (ri->content_length >= 0) {
		 
		if (ri->content_length > (long long) connWrkr->readBufSize) {
			connWrkr->pReadBuf = realloc(connWrkr->pReadBuf, ri->content_length+1);
			if (!connWrkr->pReadBuf) {
				mg_cry(conn, "%s() - realloc failed!\n", __FUNCTION__);
				FINALIZE;
			}
			connWrkr->readBufSize = ri->content_length+1;
		}
	} else {
		 
	}

	if (ri->num_headers > 0) {
		int i;
		for (i = 0; i < ri->num_headers; i++) {
			if (!strcasecmp(ri->http_headers[i].name, "content-encoding") &&
					!strcasecmp(ri->http_headers[i].value, "gzip")) {
				connWrkr->parseState.content_compressed = 1;
			}
		}
	}

	while (1) {
		int count = mg_read(conn, connWrkr->pReadBuf, connWrkr->readBufSize);
		if (count > 0) {
			processData(inst, connWrkr, (const char*)connWrkr->pReadBuf, count);
		} else {
			break;
		}
	}

	 
	doSubmitMsg(inst, connWrkr, connWrkr->pMsg, connWrkr->iMsg);
	multiSubmitFlush(&connWrkr->multiSub);

	mg_send_http_ok(conn, "text/plain", 0);
	rc = 200;

finalize_it:
	if (connWrkr->parseState.bzInitDone) {
		inflateEnd(&connWrkr->parseState.zstrm);
	}
	 
	connWrkr->iMsg = 0;

	return rc;
}