Beispiel #1
0
static void mupnp_http_server_clientthread(mUpnpThread *thread)
{
	mUpnpHttpServerClientData *clientData;
	mUpnpHttpServer *httpServer;
	mUpnpSocket *clientSock;
	void *httpServerUserData;
	mUpnpHttpRequest *httpReq;
	char *version = NULL;

	mupnp_log_debug_l4("Entering...\n");

	clientData = (mUpnpHttpServerClientData *)mupnp_thread_getuserdata(thread);
	httpServer = clientData->httpServer;
	clientSock = clientData->clientSock;
	httpServerUserData = mupnp_http_server_getuserdata(httpServer);

	httpReq = mupnp_http_request_new();
	mupnp_http_request_setsocket(httpReq, clientSock);

	/**** Thanks for Makela Aapo (10/31/05) ****/
	while (mupnp_http_request_read(httpReq, clientSock) == true && mupnp_thread_isrunnable(thread) == true) {
		/* Check some validity of the request */
		version = mupnp_http_request_getversion(httpReq);
		if (mupnp_strcmp(version, MUPNP_HTTP_VER11) == 0)
		{
			/* According to HTTP/1.1 spec, we must not tolerate
			   HTTP/1.1 request without HOST-header */
			if (mupnp_http_request_gethost(httpReq) == NULL)
			{
				mupnp_http_request_postbadrequest(httpReq);
				continue;
			}
		}

		if (httpServer->listener != NULL) {
            mupnp_http_request_setuserdata(httpReq, httpServerUserData);
			httpServer->listener(httpReq);
		}

		/* Close connection according to HTTP version and headers */
		if (mupnp_strcmp(version, MUPNP_HTTP_VER10) == 0)
		{
			/* Terminate connection after HTTP/1.0 request */
			break;
		}

		/* We are having HTTP/1.1 or better => terminate, if requested */
		if (mupnp_http_request_iskeepaliveconnection(httpReq) == false)
		{
			break;
		}
	}

	mupnp_log_debug_s("Dropping HTTP client\n");
	mupnp_http_request_delete(httpReq);

	mupnp_socket_close(clientSock);
	mupnp_socket_delete(clientSock);

	mupnp_http_server_clientdata_delete(clientData);
	mupnp_thread_setuserdata(thread, NULL);

    // This code frequently crashes. mutex lock referencing free'd memory.
	mupnp_http_server_lock(httpServer);
	mupnp_thread_remove(thread);
	mupnp_http_server_unlock(httpServer);

	mupnp_log_debug_l4("Leaving...\n");

	mupnp_thread_delete(thread);
}
/**
 * The function that calls all HTTP listener callback functions. Do not call
 * this from applications.
 *
 * @param httpReq The received HTTP request
 */
void mupnp_controlpoint_httprequestreceived(mUpnpHttpRequest *httpReq)
{
	mUpnpControlPoint *ctrlPoint = NULL;
	mUpnpNotifyRequest *notifyReq = NULL;
	mUpnpPropertyList *propList = NULL;
	mUpnpProperty *prop = NULL;
	mUpnpEventListenerList *eventListeners = NULL;
	const char *sid = NULL;
	long seq = 0;
	long timeout = 0;
	mUpnpDevice *dev = NULL;
	mUpnpService *service = NULL;
	int notifyListeners = 0;
	
	mupnp_log_debug_l4("Entering...\n");

	ctrlPoint = (mUpnpControlPoint *)mupnp_http_request_getuserdata(httpReq);

	mupnp_controlpoint_lock(ctrlPoint);

#if !defined(MUPNP_NOUSE_SUBSCRIPTION)
	if (mupnp_http_request_isnotifyrequest(httpReq) == true) {	
		notifyReq = mupnp_event_notify_request_new();
		mupnp_event_notify_request_sethttprequest(notifyReq, httpReq);

		/* Get service according to SID */
		sid = mupnp_event_notify_request_getsid(notifyReq);
		
		for (dev = mupnp_controlpoint_getdevices(ctrlPoint); 
		     dev != NULL;
		     dev = mupnp_device_next(dev))
		{
			service = mupnp_device_getservicebysid(dev, sid);
			if (service != NULL) break;
		}

		if (service != NULL) {
			/* We found a service */
			seq = mupnp_event_notify_request_getseq(notifyReq);
			
			/* Check that event key = previous + 1 */
			if (seq != 0 && seq != mupnp_service_geteventkey(service) + 1)
			{
				/* The sequence does not match, unsubscribe and subscribe */
				timeout = mupnp_service_getsubscriptiontimeout(service);
				mupnp_controlpoint_unsubscribe(ctrlPoint, service);
				mupnp_controlpoint_subscribe(ctrlPoint, service, timeout);
			} else {
				/* Wrap seq, so that assertion is true next time */
				if (seq == MUPNP_EVENT_MAX_SEQ) seq = 0;
				
				/* Set event key */
				mupnp_service_seteventkey(service, seq);
				
				notifyListeners = 1;
				propList = mupnp_event_notify_request_getpropertylist(notifyReq); 
				for (prop=mupnp_propertylist_gets(propList); 
				     prop != NULL; 
				     prop = mupnp_property_next(prop)) 
				{
					/* Update the service's state table from the event */
					mupnp_controlpoint_updatestatetablefromproperty(service, prop);
				}
			}
		}
		eventListeners = mupnp_controlpoint_geteventlisteners(ctrlPoint);
		mupnp_controlpoint_unlock(ctrlPoint);

		if (notifyListeners && propList != NULL) 
		{
			/* Notify listeners out of control point lock */
			for (prop=mupnp_propertylist_gets(propList); 
			     prop != NULL; 
			     prop = mupnp_property_next(prop)) 
			{
//                printf("\n%s\n%s\n%s\n", prop->name->value, prop->sid->value, prop->value->value);
				mupnp_eventlistenerlist_notify(ctrlPoint, eventListeners, prop);
			}
		}

		mupnp_event_notify_request_delete(notifyReq);
		mupnp_http_request_postokrequest(httpReq);
		
		return;
	}
#endif
	
	mupnp_controlpoint_unlock(ctrlPoint);
	mupnp_http_request_postbadrequest(httpReq);

	mupnp_log_debug_l4("Leaving...\n");
}