[Libwebsockets] How to handle GET and POST request in the same callback

Shmuel Weiss sweiss at kramerel.com
Thu Jun 23 23:14:50 CEST 2016


Means I have to use one PSS structure for all my scenarios handled in this protocol ?

If I want to cast the PSS ptr depending of the command received, where I can handle the Type?
How can I merge this  in the same PSS?

struct per_session_data__post_demo {
	struct lws_spa *spa;
	char result[LWS_PRE + 500];
	int result_len;

	char filename[256];
	long file_length;
	lws_filefd_type fd;
};

And

//this is a structure of per socket, user data.
struct per_session_data__wsproxy {
    int socket;                         //holds the udp socket id.
    unsigned int time_prev_keep_alive;    //holds time in seconds (time is counted since system boot), at which last keep alive made.
    lws_filefd_type fd;
};

I need something like

struct per_session_data_MYSTRUCT {
	MyTypes type;
	void *my_pss;
}

And cast it depending of the type?

case FILE_UPLOAD:
	struct per_session_data__post_demo *pss =
			(struct per_session_data__post_demo *)data;
	blabla....
	break;

case FILE_DOWNLOAD:
	struct per_session_data__post_demo *pss =
			(struct per_session_data__post_demo *)user;
	blabla....
	break;


thanks
-----Original Message-----
From: Andy Green [mailto:andy at warmcat.com] 
Sent: Friday, June 24, 2016 00:04
To: Shmuel Weiss; libwebsockets at ml.libwebsockets.org
Subject: Re: [Libwebsockets] How to handle GET and POST request in the same callback



On June 24, 2016 4:38:28 AM GMT+08:00, Shmuel Weiss <sweiss at kramerel.com> wrote:
>Hi All,
>
>I looked at the newest example about the POST DEMO uploading a file via 
>HTTP.
>I have already an HTTP callback which has an implementation for HTTP 
>GET download files.
>
>I would like to understand How I can merge the different behavior 
>inside the same case:
>case LWS_CALLBACK_HTTP_WRITEABLE:
>
>Sometimes I have to send response for the GET request and sometimes for 
>the POST.
>
>What is the right approach to handle these scenarios ?

Basically the pss struct is there to hold state that's interesting for that particular wsi.

https://github.com/warmcat/libwebsockets/blob/master/plugins/protocol_post_demo.c#L34

So generally, if you have to defer doing something until another event, you would have a member in the pss struct that you set, then later you can check what it had been set to, and do the right thing.

That might be a state enum, or just a bit in the pss depending on how many possibilities you need to cover.

The pss is very convenient for this since 

 - it's typed

 - it's something private to your protocol you can change how you like

 - it's auto-allocated / freed for each wsi

 - the allocation adapts to changes to the struct size without you having to do anything.

-Andy

>thanks
>
>Shmulito
>
>
>
>-----------------------------------------------------------------------
>-
>
>_______________________________________________
>Libwebsockets mailing list
>Libwebsockets at ml.libwebsockets.org
>http://libwebsockets.org/mailman/listinfo/libwebsockets

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://libwebsockets.org/pipermail/libwebsockets/attachments/20160623/5d294640/attachment-0001.html>


More information about the Libwebsockets mailing list