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
}
/* file:// is handled above */
/* else if (strequal(proto, "FILE")) {
data->conf |= CONF_FILE;
result = file(data, path, &bytecount);
if(result)
return result;
return URG_OK;
}*/
else {
failf(data, "Unsupported protocol: %s", proto);
return URG_UNSUPPORTED_PROTOCOL;
}
if(data->conf & CONF_NETRC) {
if(ParseNetrc(data->hostname, data->user, data->passwd)) {
infof(data, "Couldn't find host %s in the .netrc file, using defaults",
data->hostname);
}
/* weather we failed or not, we don't know which fields that were filled
in anyway */
if(!data->user[0])
strcpy(data->user, CURL_DEFAULT_USER);
if(!data->passwd[0])
strcpy(data->passwd, CURL_DEFAULT_PASSWORD);
if(data->conf & CONF_HTTP) {
data->conf |= CONF_USERPWD;
}
}
else if(!(data->conf & CONF_USERPWD) &&
(data->conf & (CONF_FTP|CONF_HTTP)) ) {
/* This is a FTP or HTTP URL, and we haven't got the user+password in
the extra parameter, we will now try to extract the possible
user+password pair in a string like:
ftp://user:password@ftp.my.site:8021/README */
char *ptr=NULL; /* assign to remove possible warnings */
if(':' == *name) {
failf(data, "URL malformat: user can't be zero length");
return URG_URL_MALFORMAT_USER;
}
if((1 <= sscanf(name, "%127[^:]:%127[^@]",
data->user, data->passwd)) && (ptr=strchr(name, '@'))) {
name = ++ptr;
data->conf |= CONF_USERPWD;
}
else {
strcpy(data->user, CURL_DEFAULT_USER);
strcpy(data->passwd, CURL_DEFAULT_PASSWORD);
}
}
if(!(data->conf & CONF_PROXY)) {
/* If not connecting via a proxy, extract the port from the URL, if it is
* there, thus overriding any defaults that might have been set above. */
tmp = strchr(name, ':');
if (tmp) {
*tmp++ = '\0';
data->port = atoi(tmp);
}
/* Connect to target host right on */
if(!(hp = GetHost(data, name))) {
failf(data, "Couldn't resolv host '%s'", name);
return URG_COULDNT_RESOLVE_HOST;
}
}
else {
char *prox_portno;
char *endofprot;
Daniel Stenberg
committed
/* We need to make a duplicate of the proxy so that we can modify the
string safely. */
char *proxydup=strdup(data->proxy);
/* We use 'proxyptr' to point to the proxy name from now on... */
char *proxyptr=proxydup;
if(NULL == proxydup) {
failf(data, "memory shortage");
return URG_OUT_OF_MEMORY;
}
/* we use proxy all right, but we wanna know the remote port for SSL
reasons */
tmp = strchr(name, ':');
if (tmp) {
*tmp++ = '\0'; /* cut off the name there */
data->remote_port = atoi(tmp);
}
/* Daniel Dec 10, 1998:
We do the proxy host string parsing here. We want the host name and the
port name. Accept a protocol:// prefix, even though it should just be
ignored. */
/* 1. skip the protocol part if present */
Daniel Stenberg
committed
endofprot=strstr(proxyptr, "://");
Daniel Stenberg
committed
proxyptr = endofprot+3;
}
/* allow user to specify proxy.server.com:1080 if desired */
Daniel Stenberg
committed
prox_portno = strchr (proxyptr, ':');
if (prox_portno) {
*prox_portno = 0x0; /* cut off number from host name */
prox_portno ++;
/* now set the local port number */
data->port = atoi(prox_portno);
}
/* connect to proxy */
Daniel Stenberg
committed
if(!(hp = GetHost(data, proxyptr))) {
failf(data, "Couldn't resolv proxy '%s'", proxyptr);
Daniel Stenberg
committed
free(proxydup); /* free the duplicate pointer and not the modified */
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
data->firstsocket = socket(AF_INET, SOCK_STREAM, 0);
memset((char *) &serv_addr, '\0', sizeof(serv_addr));
memcpy((char *)&(serv_addr.sin_addr), hp->h_addr, hp->h_length);
serv_addr.sin_family = hp->h_addrtype;
serv_addr.sin_port = htons(data->port);
if (connect(data->firstsocket, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0) {
switch(errno) {
#ifdef ECONNREFUSED
/* this should be made nicer */
case ECONNREFUSED:
failf(data, "Connection refused");
break;
#endif
#ifdef EINTR
case EINTR:
failf(data, "Connection timeouted");
break;
#endif
default:
failf(data, "Can't connect to server: %d", errno);
break;
}
return URG_COULDNT_CONNECT;
}
if(data->conf & CONF_PROXYUSERPWD) {
char authorization[512];
sprintf(data->buffer, "%s:%s", data->proxyuser, data->proxypasswd);
base64Encode(data->buffer, authorization);
data->ptr_proxyuserpwd = maprintf("Proxy-authorization: Basic %s\015\012",
authorization);
}
if(data->conf & (CONF_HTTPS|CONF_HTTP|CONF_PROXY)) {
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
if(data->useragent) {
data->ptr_uagent = maprintf("User-Agent: %s\015\012", data->useragent);
}
}
/* If we are not using a proxy and we want a secure connection,
* perform SSL initialization & connection now.
* If using a proxy with https, then we must tell the proxy to CONNECT
* us to the host we want to talk to. Only after the connect
* has occured, can we start talking SSL
*/
if (data->conf & CONF_HTTPS) {
if (data->conf & CONF_PROXY) {
/* OK, now send the connect statment */
sendf(data->firstsocket, data,
"CONNECT %s:%d HTTP/1.0\015\012"
"%s"
"%s"
"\r\n",
data->hostname, data->remote_port,
(data->conf&CONF_PROXYUSERPWD)?data->ptr_proxyuserpwd:"",
(data->useragent?data->ptr_uagent:"")
);
/* wait for the proxy to send us a HTTP/1.0 200 OK header */
/* Daniel rewrote this part Nov 5 1998 to make it more obvious */
{
int httperror=0;
int subversion=0;
while(GetLine(data->firstsocket, data->buffer, data)) {
if('\r' == data->buffer[0])
break; /* end of headers */
if(2 == sscanf(data->buffer, "HTTP/1.%d %d",
&subversion,
&httperror)) {
;
}
}
if(200 != httperror) {
if(407 == httperror)
/* Added Nov 6 1998 */
failf(data, "Proxy requires authorization!");
else
failf(data, "Received error code %d from proxy", httperror);
return URG_READ_ERROR;
}
}
infof (data, "Proxy has replied to CONNECT request\n");
}
/* now, perform the SSL initialization for this socket */
if(UrgSSLConnect (data)) {
return URG_SSL_CONNECT_ERROR;
}
}
now = tvnow(); /* time this *after* the connect is done */
bytecount = 0;
/* Figure out the ip-number and the first host name it shows: */
{
struct in_addr in;
(void) memcpy(&in.s_addr, *hp->h_addr_list, sizeof (in.s_addr));
infof(data, "Connected to %s (%s)\n", hp->h_name, inet_ntoa(in));
}
Daniel Stenberg
committed
#if 0 /* Kerberos experiements! Beware! Take cover! */
kerberos_connect(data, name);
#endif
#ifdef __EMX__
/* 20000330 mgs
* the check is quite a hack...
* we're calling _fsetmode to fix the problem with fwrite converting newline
* characters (you get mangled text files, and corrupted binary files when
* you download to stdout and redirect it to a file). */
if ((data->out)->_handle == NULL) {
_fsetmode(stdout, "b");
}
#endif
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
if((data->conf&(CONF_FTP|CONF_PROXY)) == CONF_FTP) {
result = ftp(data, &bytecount, data->user, data->passwd, ppath);
if(result)
return result;
}
else if(data->conf & CONF_TELNET) {
result=telnet(data);
if(result)
return result;
}
else if (data->conf & CONF_LDAP) {
result = ldap(data, path, &bytecount);
if (result)
return result;
}
else if (data->conf & CONF_DICT) {
result = dict(data, path, &bytecount);
if(result)
return result;
}
else {
result = http(data, ppath, name, &bytecount);
if(result)
return result;
}
if(bytecount) {
double ittook = tvdiff (tvnow(), now);
infof(data, "%i bytes transfered in %.3lf seconds (%.0lf bytes/sec).\n",
bytecount, ittook, (double)bytecount/(ittook!=0.0?ittook:1));
}
return URG_OK;
}