redirect_handler (SoupMessage *msg, gpointer user_data)
{
    SoupSession *session = user_data;
    const char  *new_loc;
    SoupURI     *new_uri;
    SoupURI     *old_uri;
    guint        status;
    gboolean     redirect;

    status = msg->status_code;
    new_loc = soup_message_headers_get_one (msg->response_headers, "Location");

    /* If we don't have a location to redirect to, just fail */
    if (new_loc == NULL)
      return;

    if (!SOUP_STATUS_IS_REDIRECTION(status))
      return;

   new_uri = soup_uri_new_with_base (soup_message_get_uri (msg), new_loc);
   if (new_uri == NULL)
     {
       soup_message_set_status_full (msg,
                                     SOUP_STATUS_MALFORMED,
                                     "Invalid Redirect URL");
       return;
     }

   old_uri = soup_message_get_uri (msg);

   /* copy over username and password to new_uri */
   soup_uri_set_user(new_uri, old_uri->user);
   soup_uri_set_password(new_uri, old_uri->password);

   /* Check if this is a trailing slash redirect (i.e. /a/b to /a/b/),
    * redirect it right away
    */
   redirect = dav_uri_match (new_uri, old_uri, TRUE);

   if (redirect == TRUE)
     {
       const char *dest;

       dest = soup_message_headers_get_one (msg->request_headers,
                                            "Destination");

       if (dest && g_str_has_suffix (dest, "/") == FALSE)
         {
           char *new_dest = g_strconcat (dest, "/", NULL);
           soup_message_headers_replace (msg->request_headers,
                                         "Destination",
                                         new_dest);
           g_free (new_dest);
         }
     }
   else if (message_should_apply_redir_ref (msg))
     {


       if (status == SOUP_STATUS_MOVED_PERMANENTLY ||
           status == SOUP_STATUS_TEMPORARY_REDIRECT)
         {

           /* Only corss-site redirect safe methods */
           if (msg->method == SOUP_METHOD_GET &&
               msg->method == SOUP_METHOD_HEAD &&
               msg->method == SOUP_METHOD_OPTIONS &&
               msg->method == SOUP_METHOD_PROPFIND)
             redirect = TRUE;
         }

#if 0
       else if (msg->status_code == SOUP_STATUS_SEE_OTHER ||
                msg->status_code == SOUP_STATUS_FOUND)
         {
           /* Redirect using a GET */
           g_object_set (msg,
                         SOUP_MESSAGE_METHOD, SOUP_METHOD_GET,
                         NULL);
           soup_message_set_request (msg, NULL,
                                     SOUP_MEMORY_STATIC, NULL, 0);
           soup_message_headers_set_encoding (msg->request_headers,
                                              SOUP_ENCODING_NONE);
         }
#endif
         /* ELSE:
          *
          * Two possibilities:
          *
          *   1) It's a non-redirecty 3xx response (300, 304,
          *      305, 306)
          *   2) It's some newly-defined 3xx response (308+)
          *
          * We ignore both of these cases. In the first,
          * redirecting would be explicitly wrong, and in the
          * last case, we have no clue if the 3xx response is
          * supposed to be redirecty or non-redirecty. Plus,
          * 2616 says unrecognized status codes should be
          * treated as the equivalent to the x00 code, and we
          * don't redirect on 300, so therefore we shouldn't
          * redirect on 308+ either.
          */
     }

    if (redirect)
      {
        soup_message_set_uri (msg, new_uri);
        soup_session_requeue_message (session, msg);
      }

    soup_uri_free (new_uri);
}
redirect_handler (SoupMessage *msg, gpointer user_data)
{
    SoupSession *session = user_data;
    const char  *new_loc;
    SoupURI     *new_uri;
    SoupURI     *old_uri;
    guint        status;
    gboolean     redirect;

    status = msg->status_code;
    new_loc = soup_message_headers_get_one (msg->response_headers, "Location");

     
    if (new_loc == NULL)
      return;

    if (!SOUP_STATUS_IS_REDIRECTION(status))
      return;

   new_uri = soup_uri_new_with_base (soup_message_get_uri (msg), new_loc);
   if (new_uri == NULL)
     {
       soup_message_set_status_full (msg,
                                     SOUP_STATUS_MALFORMED,
                                     "Invalid Redirect URL");
       return;
     }

   old_uri = soup_message_get_uri (msg);

    
   soup_uri_set_user(new_uri, old_uri->user);
   soup_uri_set_password(new_uri, old_uri->password);

    
   redirect = dav_uri_match (new_uri, old_uri, TRUE);

   if (redirect == TRUE)
     {
       const char *dest;

       dest = soup_message_headers_get_one (msg->request_headers,
                                            "Destination");

       if (dest && g_str_has_suffix (dest, "/") == FALSE)
         {
           char *new_dest = g_strconcat (dest, "/", NULL);
           soup_message_headers_replace (msg->request_headers,
                                         "Destination",
                                         new_dest);
           g_free (new_dest);
         }
     }
   else if (message_should_apply_redir_ref (msg))
     {


       if (status == SOUP_STATUS_MOVED_PERMANENTLY ||
           status == SOUP_STATUS_TEMPORARY_REDIRECT)
         {

            
           if (msg->method == SOUP_METHOD_GET &&
               msg->method == SOUP_METHOD_HEAD &&
               msg->method == SOUP_METHOD_OPTIONS &&
               msg->method == SOUP_METHOD_PROPFIND)
             redirect = TRUE;
         }

#if 0
       else if (msg->status_code == SOUP_STATUS_SEE_OTHER ||
                msg->status_code == SOUP_STATUS_FOUND)
         {
            
           g_object_set (msg,
                         SOUP_MESSAGE_METHOD, SOUP_METHOD_GET,
                         NULL);
           soup_message_set_request (msg, NULL,
                                     SOUP_MEMORY_STATIC, NULL, 0);
           soup_message_headers_set_encoding (msg->request_headers,
                                              SOUP_ENCODING_NONE);
         }
#endif
          
     }

    if (redirect)
      {
        soup_message_set_uri (msg, new_uri);
        soup_session_requeue_message (session, msg);
      }

    soup_uri_free (new_uri);
}