[PATCH 3/5] Change header files to support gweb.

Marcel Holtmann marcel at holtmann.org
Thu Oct 28 12:21:40 PDT 2010


Hi Mohamed,

> > > Add new API to support http download.
> > > ---
> > >  gweb/gweb.h |   12 ++++++++++++
> > >  1 files changed, 12 insertions(+), 0 deletions(-)
> > > 
> > > diff --git a/gweb/gweb.h b/gweb/gweb.h
> > > index 1ab2a9f..61e488c 100644
> > > --- a/gweb/gweb.h
> > > +++ b/gweb/gweb.h
> > > @@ -38,10 +38,19 @@ typedef enum {
> > >  	G_WEB_METHOD_GET,
> > >  } GWebMethod;
> > >  
> > > +typedef enum {
> > > +	G_WEB_DATA_HEADER,
> > > +	G_WEB_DATA_BODY,
> > > +} GWebDataType;
> > > +
> > >  typedef void (*GWebResultFunc)(uint16_t status, gpointer user_data);
> > >  
> > >  typedef void (*GWebDebugFunc)(const char *str, gpointer user_data);
> > >  
> > > +typedef gboolean (*GWebReceivedFunc)(const guint8 *str, int len,
> > > +						GWebDataType data_type,
> > > +						gpointer user_data);
> > > +
> > 
> > so I am thinking this becomes a little bit too complicated and we are
> > overloading this. Here is my idea on how to make this simpler.
> > 
> > struct _GWebResult;
> > 
> > typedef struct _GWebResult GWebResult;
> > 
> > 
> > typedef gboolean (*GWebResultFunc) (guint16 status, GWebResult *result,
> > 						gpointer user_data);
> > 
> > guint g_web_request(GWeb, GWebMethod method, const char *url,
> > 				GWebResultFunc, gpointer user_data);
> > 
> > So essentially we are creating a new GWebResult object that represents
> > our transaction/request.
> > 
> > Then we can have these functions to be used inside the result callback.
> > 
> > gboolean g_web_result_get_header(GWebResult *result,
> > 				const char *header, const char **value);
> > 
> > gboolean g_web_result_get_fragment(GWebResult *result,
> > 				const guint8 **fragment, gsize *length);
> > 
> > The get_header function will be always valid since we keep all headers
> > in the GWebResult object for the time of the transaction. And the
> > get_fragment returns the current buffer value.
> > 
> > And of course the result function has a return value. If returning FALSE
> > then we just stop the transaction right away. So after we received the
> > headers we can call this once with empty fragment buffer and when we
> > have all details we just stop it. If we return TRUE then the transaction
> > continues until we have received the full body.
> > 
> > Also the status will indicate if we are done or not. A status of 100
> > indicates that there is more to come. And every other status is either
> > 200 for success or an error code. Of course the return value only
> > matters if the status is 100 since otherwise the transaction is over
> > anyway.
>
> So now we have one notification function which will be used by GWeb to
> notify the user. So I guess GWeb will call GWebResultFunc evey time we
> receive a payload right?
> This new API will be more flixible to user since they can get the
> payload on chunk or wait and get the whole payload at once but it will
> be more work in GWeb since we have to buffer until user ask for data.

no buffering inside the GWebResult. If you don't get the fragment buffer
in that callback, then that buffer is gone.

If you don't want the website, then don't bother requesting it in the
first place ;)

> One more question in gboolean g_web_result_get_fragment(GWebResult
> *result, const guint8 **fragment, gsize *length);
> I assume the user will responsible to release the memory here right?

It is const. So the buffer is owned by GWebResult. The user can not free
it. That is what const means. You get a pointer and can use that
pointer, but it is not yours.

So basically GWebResult needs to have an internal buffer where it stores
data in from the HTTP transaction. And when requested it just returns a
pointer to that data.

> I will start on adding this change and please let me know if any of my
> assumption are wrong so I dont have to redo.

Essentially you have all of this already. Just the header handling is a
bit different. So you have parse all headers and store them. Using a
GHastTable internally inside GWebResult looks like a good idea. And once
you have all headers, then you call into the callback for the first
time.

Regards

Marcel




More information about the connman mailing list