+
+/*****************************************************************************
+ * Cookies (FIXME: we may want to rewrite that using a nice structure to hold
+ * them) (FIXME: only support the "domain=" param)
+ *****************************************************************************/
+
+/* Get the NAME=VALUE part of the Cookie */
+static char * cookie_get_content( const char * cookie )
+{
+ char * ret = strdup( cookie );
+ if( !ret ) return NULL;
+ char * str = ret;
+ /* Look for a ';' */
+ while( *str && *str != ';' ) str++;
+ /* Replace it by a end-char */
+ if( *str == ';' ) *str = 0;
+ return ret;
+}
+
+/* Get the domain where the cookie is stored */
+static char * cookie_get_domain( const char * cookie )
+{
+ const char * str = cookie;
+ static const char domain[] = "domain=";
+ if( !str )
+ return NULL;
+ /* Look for a ';' */
+ while( *str )
+ {
+ if( !strncmp( str, domain, sizeof(domain) - 1 /* minus \0 */ ) )
+ {
+ str += sizeof(domain) - 1 /* minus \0 */;
+ char * ret = strdup( str );
+ /* Now remove the next ';' if present */
+ char * ret_iter = ret;
+ while( *ret_iter && *ret_iter != ';' ) ret_iter++;
+ if( *ret_iter == ';' )
+ *ret_iter = 0;
+ return ret;
+ }
+ /* Go to next ';' field */
+ while( *str && *str != ';' ) str++;
+ if( *str == ';' ) str++;
+ /* skip blank */
+ while( *str && *str == ' ' ) str++;
+ }
+ return NULL;
+}
+
+/* Get NAME in the NAME=VALUE field */
+static char * cookie_get_name( const char * cookie )
+{
+ char * ret = cookie_get_content( cookie ); /* NAME=VALUE */
+ if( !ret ) return NULL;
+ char * str = ret;
+ while( *str && *str != '=' ) str++;
+ *str = 0;
+ return ret;
+}
+
+/* Add a cookie in cookies, checking to see how it should be added */
+static void cookie_append( vlc_array_t * cookies, char * cookie )
+{
+ int i;
+ char * cookie_name = cookie_get_name( cookie );
+ char * cookie_domain = cookie_get_domain( cookie );
+ for( i = 0; i < vlc_array_count( cookies ); i++ )
+ {
+ char * current_cookie = vlc_array_item_at_index( cookies, i );
+ char * current_cookie_name = cookie_get_name( current_cookie );
+ char * current_cookie_domain = cookie_get_domain( current_cookie );
+ if(!strcmp( cookie_name, current_cookie_name ) &&
+ !strcmp( cookie_domain, current_cookie_domain ))
+ {
+ /* Remove previous value for this cookie */
+ free( current_cookie );
+ vlc_array_remove( cookies, i );
+
+ /* Clean */
+ free( cookie_name );
+ free( cookie_domain );
+ free( current_cookie_name );
+ free( current_cookie_domain );
+ break;
+ }
+ free( current_cookie_name );
+ free( current_cookie_domain );
+ }
+ free( cookie_name );
+ free( cookie_domain );
+ vlc_array_append( cookies, cookie );
+}
+