Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
port = r->connection->local_addr->port;
/* Recall that the name_chain is a list of server_addr_recs, some of
* whose ports may not match. Also each server may appear more than
* once in the chain -- specifically, it will appear once for each
* address from its VirtualHost line which matched. We only want to
* do the full ServerName/ServerAlias comparisons once for each
* server, fortunately we know that all the VirtualHost addresses for
* a single server are adjacent to each other.
*/
for (src = r->connection->vhost_lookup_data; src; src = src->next) {
server_addr_rec *sar;
/* We only consider addresses on the name_chain which have a matching
* port
*/
sar = src->sar;
if (sar->host_port != 0 && port != sar->host_port) {
continue;
}
s = src->server;
/* If we still need to do ServerName and ServerAlias checks for this
* server, do them now.
*/
if (s != last_s) {
/* does it match any ServerName or ServerAlias directive? */
if (matches_aliases(s, host)) {
goto found;
}
}
last_s = s;
/* Fallback: does it match the virthost from the sar? */
if (!strcasecmp(host, sar->virthost)) {
/* only the first match is used */
if (virthost_s == NULL) {
virthost_s = s;
}
}
}
/* If ServerName and ServerAlias check failed, we end up here. If it
* matches a VirtualHost, virthost_s is set. Use that as fallback
*/
if (virthost_s) {
s = virthost_s;
goto found;
}
return;
found:
/* s is the first matching server, we're done */
r->server = s;
}
static void check_serverpath(request_rec *r)
{
server_rec *s;
server_rec *last_s;
name_chain *src;
apr_port_t port;
port = r->connection->local_addr->port;
/*
* This is in conjunction with the ServerPath code in http_core, so we
* get the right host attached to a non- Host-sending request.
*
* See the comment in check_hostalias about how each vhost can be
* listed multiple times.
*/
last_s = NULL;
for (src = r->connection->vhost_lookup_data; src; src = src->next) {
/* We only consider addresses on the name_chain which have a matching
* port
*/
if (src->sar->host_port != 0 && port != src->sar->host_port) {
continue;
}
s = src->server;
if (s == last_s) {
continue;
}
last_s = s;
if (s->path && !strncmp(r->uri, s->path, s->pathlen) &&
(s->path[s->pathlen - 1] == '/' ||
r->uri[s->pathlen] == '/' ||
r->uri[s->pathlen] == '\0')) {
r->server = s;
return;
}
}
}
static APR_INLINE const char *construct_host_header(request_rec *r,
int is_v6literal)
{
struct iovec iov[5];
apr_size_t nvec = 0;
/*
* We cannot use ap_get_server_name/port here, because we must
* ignore UseCanonicalName/Port.
*/
if (is_v6literal) {
iov[nvec].iov_base = "[";
iov[nvec].iov_len = 1;
nvec++;
}
iov[nvec].iov_base = (void *)r->hostname;
iov[nvec].iov_len = strlen(r->hostname);
nvec++;
if (is_v6literal) {
iov[nvec].iov_base = "]";
iov[nvec].iov_len = 1;
nvec++;
}
if (r->parsed_uri.port_str) {
iov[nvec].iov_base = ":";
iov[nvec].iov_len = 1;
nvec++;
iov[nvec].iov_base = r->parsed_uri.port_str;
iov[nvec].iov_len = strlen(r->parsed_uri.port_str);
nvec++;
}
return apr_pstrcatv(r->pool, iov, nvec, NULL);
}
AP_DECLARE(void) ap_update_vhost_from_headers(request_rec *r)
{
core_server_config *conf = ap_get_core_module_config(r->server->module_config);
const char *host_header = apr_table_get(r->headers_in, "Host");
int is_v6literal = 0;
int have_hostname_from_url = 0;
if (r->hostname) {
/*
* If there was a host part in the Request-URI, ignore the 'Host'
* header.
*/
have_hostname_from_url = 1;
is_v6literal = fix_hostname(r, NULL, conf->http_conformance);
}
else if (host_header != NULL) {
is_v6literal = fix_hostname(r, host_header, conf->http_conformance);
}
if (r->status != HTTP_OK)
return;
if (conf->http_conformance != AP_HTTP_CONFORMANCE_UNSAFE) {
/*
* If we have both hostname from an absoluteURI and a Host header,
* we must ignore the Host header (RFC 2616 5.2).
* To enforce this, we reset the Host header to the value from the
* request line.
*/
if (have_hostname_from_url && host_header != NULL) {
const char *repl = construct_host_header(r, is_v6literal);
apr_table_setn(r->headers_in, "Host", repl);
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02417)
"Replacing host header '%s' with host '%s' given "
"in the request uri", host_header, repl);
}
}
/* check if we tucked away a name_chain */
if (r->connection->vhost_lookup_data) {
if (r->hostname)
check_hostalias(r);
else
check_serverpath(r);
}
}
/**
* For every virtual host on this connection, call func_cb.
*/
AP_DECLARE(int) ap_vhost_iterate_given_conn(conn_rec *conn,
ap_vhost_iterate_conn_cb func_cb,
void* baton)
{
server_rec *s;
server_rec *last_s;
name_chain *src;
apr_port_t port;
int rv = 0;
if (conn->vhost_lookup_data) {
last_s = NULL;
port = conn->local_addr->port;
for (src = conn->vhost_lookup_data; src; src = src->next) {
server_addr_rec *sar;
/* We only consider addresses on the name_chain which have a
* matching port.
*/
sar = src->sar;
if (sar->host_port != 0 && port != sar->host_port) {
continue;
}
s = src->server;
if (s == last_s) {
/* we've already done a callback for this vhost. */
continue;
}
last_s = s;
rv = func_cb(baton, conn, s);
if (rv != 0) {
break;
}
}
}
else {
rv = func_cb(baton, conn, conn->base_server);
}
return rv;
}
/* Called for a new connection which has a known local_addr. Note that the
* new connection is assumed to have conn->server == main server.
*/
AP_DECLARE(void) ap_update_vhost_given_ip(conn_rec *conn)
{
ipaddr_chain *trav;
apr_port_t port;
/* scan the hash table for an exact match first */
trav = find_ipaddr(conn->local_addr);
if (trav) {
/* save the name_chain for later in case this is a name-vhost */
conn->vhost_lookup_data = trav->names;
conn->base_server = trav->server;
return;
}
/* maybe there's a default server or wildcard name-based vhost
* matching this port
*/
port = conn->local_addr->port;
trav = find_default_server(port);
if (trav) {
conn->vhost_lookup_data = trav->names;
conn->base_server = trav->server;
return;
}
/* otherwise we're stuck with just the main server
* and no name-based vhosts
*/
conn->vhost_lookup_data = NULL;
}