File: ne_redirect.c

package info (click to toggle)
neon27 0.30.2-2
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 4,940 kB
  • ctags: 3,303
  • sloc: ansic: 24,449; xml: 3,673; sh: 3,557; makefile: 595
file content (141 lines) | stat: -rw-r--r-- 3,677 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* 
   HTTP-redirect support
   Copyright (C) 1999-2007, Joe Orton <joe@manyfish.co.uk>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.
   
   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
   MA 02111-1307, USA

*/

#include "config.h"

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif

#include "ne_session.h"
#include "ne_request.h"
#include "ne_alloc.h"
#include "ne_uri.h"
#include "ne_redirect.h"
#include "ne_internal.h"
#include "ne_string.h"

#define REDIRECT_ID "http://www.webdav.org/neon/hooks/http-redirect"

struct redirect {
    char *requri;
    int valid; /* non-zero if .uri contains a redirect */
    ne_uri uri;
    ne_session *sess;
};

static void
create(ne_request *req, void *session, const char *method, const char *uri)
{
    struct redirect *red = session;
    if (red->requri) ne_free(red->requri);
    red->requri = ne_strdup(uri);
}

#define REDIR(n) ((n) == 301 || (n) == 302 || (n) == 303 || \
		  (n) == 307)

static int post_send(ne_request *req, void *private, const ne_status *status)
{
    struct redirect *red = private;
    const char *location = ne_get_response_header(req, "Location");
    ne_buffer *path = NULL;
    int ret;

    /* Don't do anything for non-redirect status or no Location header. */
    if (!REDIR(status->code) || location == NULL)
	return NE_OK;

    if (strstr(location, "://") == NULL && location[0] != '/') {
	char *pnt;

	path = ne_buffer_create();
	ne_buffer_zappend(path, red->requri);
	pnt = strrchr(path->data, '/');

	if (pnt && pnt[1] != '\0') {
	    /* Chop off last path segment. */
	    pnt[1] = '\0';
	    ne_buffer_altered(path);
	}
	ne_buffer_zappend(path, location);
	location = path->data;
    }

    /* free last uri. */
    ne_uri_free(&red->uri);
    
    /* Parse the Location header */
    if (ne_uri_parse(location, &red->uri) || red->uri.path == NULL) {
        red->valid = 0;
	ne_set_error(red->sess, _("Could not parse redirect destination URL"));
        ret = NE_ERROR;
    } else {
        /* got a valid redirect. */
        red->valid = 1;
        ret = NE_REDIRECT;

        if (!red->uri.host) {
            /* Not an absoluteURI: breaks 2616 but everybody does it. */
            ne_fill_server_uri(red->sess, &red->uri);
        }
    }

    if (path) ne_buffer_destroy(path);

    return ret;
}

static void free_redirect(void *cookie)
{
    struct redirect *red = cookie;
    ne_uri_free(&red->uri);
    if (red->requri)
        ne_free(red->requri);
    ne_free(red);
}

void ne_redirect_register(ne_session *sess)
{
    struct redirect *red = ne_calloc(sizeof *red);
    
    red->sess = sess;

    ne_hook_create_request(sess, create, red);
    ne_hook_post_send(sess, post_send, red);
    ne_hook_destroy_session(sess, free_redirect, red);

    ne_set_session_private(sess, REDIRECT_ID, red);
}

const ne_uri *ne_redirect_location(ne_session *sess)
{
    struct redirect *red = ne_get_session_private(sess, REDIRECT_ID);

    if (red && red->valid)
        return &red->uri;
    else
        return NULL;
}