| verzia 1.14, 2001/11/16 19:30:01 |
verzia 1.15, 2001/11/19 20:15:07 |
|
|
| /* |
/* |
| * Author: Lubomir Host 'rajo' <host8@kepler.fmph.uniba.sk> |
* Author: Lubomir Host 'rajo' <host8@kepler.fmph.uniba.sk> |
| * $Log$ |
* $Log$ |
| |
* Revision 1.15 2001/11/19 20:15:07 host8 |
| |
* Http protocol probably works fine. |
| |
* |
| * Revision 1.14 2001/11/16 19:30:01 host8 |
* Revision 1.14 2001/11/16 19:30:01 host8 |
| * Patched bug in declaration/definition of global variable debug. |
* Patched bug in declaration/definition of global variable debug. |
| * |
* |
| Riadok 302 static void http_set_cookie(char *str) |
|
| Riadok 305 static void http_set_cookie(char *str) |
|
| // static void free_request(HTTP_REQUEST *r) {{{ |
// static void free_request(HTTP_REQUEST *r) {{{ |
| static void free_request(HTTP_REQUEST *r) |
static void free_request(HTTP_REQUEST *r) |
| { |
{ |
| free(r->ret_val); |
if (r->ret_val != NULL) |
| free(r->host); |
free(r->ret_val); |
| free(r->file); |
if (r->host != NULL) |
| |
free(r->host); |
| |
if (r->file != NULL) |
| |
free(r->file); |
| free(r); |
free(r); |
| } |
} |
| // }}} |
// }}} |
| Riadok 533 static FILE *http_connect(HTTP_REQUEST * |
|
| Riadok 539 static FILE *http_connect(HTTP_REQUEST * |
|
| servers[idx].ip, |
servers[idx].ip, |
| r->port, strerror(errno)); |
r->port, strerror(errno)); |
| return (NULL); |
return (NULL); |
| |
|
| |
|
| /* povodny bordel {{{ |
|
| res0 = GetAddrInfo(r->host, r->port); |
|
| if (res0 == NULL) { |
|
| fprintf(stderr, "Unable to locate remote host %s.", r->host); |
|
| return (NULL); |
|
| } |
|
| |
|
| for (i=0; servers[i].desc != NULL; i++) { |
|
| if (servers[i].id == id) { |
|
| idx = i; |
|
| break; |
|
| } |
|
| } |
|
| if (idx == -1) { |
|
| fprintf(stderr, "Id %d not found in servers[] !\n", id); |
|
| exit(1); |
|
| } |
|
| if (r->host == NULL) |
|
| serv_addr.sin_addr.s_addr = inet_addr(servers[idx].ip); |
|
| else |
|
| serv_addr.sin_addr.s_addr = inet_addr(r->host); |
|
| if (serv_addr.sin_addr.s_addr == INADDR_NONE) { |
|
| struct hostent *pent = gethostbyname(r->host); |
|
| if (pent != NULL) |
|
| serv_addr.sin_addr.s_addr = *((u_long *) (pent->h_addr)); |
|
| } |
|
| if (serv_addr.sin_addr.s_addr == INADDR_NONE) |
|
| return NULL; |
|
| }}} */ |
|
| /****i* lib5250/telnet_stream_connect {{{ |
|
| * NAME |
|
| * telnet_stream_connect |
|
| * SYNOPSIS |
|
| * ret = telnet_stream_connect (This, to); |
|
| * INPUTS |
|
| * Tn5250Stream * This - |
|
| * const char * to - |
|
| * DESCRIPTION |
|
| * Connects to server. The `to' parameter is in the form |
|
| * host[:port]. |
|
| ***** |
|
| static int telnet_stream_connect(Tn5250Stream * This, const char *to) |
|
| { |
|
| struct sockaddr_in serv_addr; |
|
| u_long ioctlarg = 1; |
|
| char *address; |
|
| int r; |
|
| |
|
| memset((char *) &serv_addr, 0, sizeof(serv_addr)); |
|
| serv_addr.sin_family = AF_INET; |
|
| |
|
| // Figure out the internet address. // |
|
| address = (char *)malloc (strlen (to)+1); |
|
| strcpy (address, to); |
|
| if (strchr (address, ':')) |
|
| *strchr (address, ':') = '\0'; |
|
| |
|
| serv_addr.sin_addr.s_addr = inet_addr(address); |
|
| if (serv_addr.sin_addr.s_addr == INADDR_NONE) { |
|
| struct hostent *pent = TN_GETHOSTBYNAME(address); |
|
| if (pent != NULL) |
|
| serv_addr.sin_addr.s_addr = *((u_long *) (pent->h_addr)); |
|
| } |
|
| g_free (address); |
|
| if (serv_addr.sin_addr.s_addr == INADDR_NONE) |
|
| return LAST_ERROR; |
|
| |
|
| // Figure out the port name. // |
|
| if (strchr (to, ':') != NULL) { |
|
| const char *port = strchr (to, ':') + 1; |
|
| serv_addr.sin_port = htons((u_short) atoi(port)); |
|
| if (serv_addr.sin_port == 0) { |
|
| struct servent *pent = getservbyname(port, "tcp"); |
|
| if (pent != NULL) |
|
| serv_addr.sin_port = pent->s_port; |
|
| } |
|
| if (serv_addr.sin_port == 0) |
|
| return LAST_ERROR; |
|
| } else { |
|
| // No port specified ... use telnet port. // |
|
| struct servent *pent = getservbyname ("telnet", "tcp"); |
|
| if (pent == NULL) |
|
| serv_addr.sin_port = htons(23); |
|
| else |
|
| serv_addr.sin_port = pent->s_port; |
|
| } |
|
| |
|
| This->sockfd = socket(PF_INET, SOCK_STREAM, 0); |
|
| if (WAS_INVAL_SOCK(This->sockfd)) { |
|
| return LAST_ERROR; |
|
| } |
|
| r = TN_CONNECT(This->sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)); |
|
| if (WAS_ERROR_RET(r)) { |
|
| return LAST_ERROR; |
|
| } |
|
| // Set socket to non-blocking mode. // |
|
| #ifdef FIONBIO |
|
| TN5250_LOG(("Non-Blocking\n")); |
|
| TN_IOCTL(This->sockfd, FIONBIO, &ioctlarg); |
|
| #endif |
|
| |
|
| This->state = TN5250_STREAM_STATE_DATA; |
|
| return 0; |
|
| } // }}} |
|
| */ |
|
| } // }}} |
} // }}} |
| // static void http_read_source(HTTP_REQUEST *r) {{{ |
// static void http_read_source(HTTP_REQUEST *r) {{{ |
| static void http_read_source(HTTP_REQUEST *r) |
static void http_read_source(HTTP_REQUEST *r) |
| Riadok 651 static void http_read_source(HTTP_REQUES |
|
| Riadok 550 static void http_read_source(HTTP_REQUES |
|
| int info = 1; |
int info = 1; |
| #endif |
#endif |
| int bad_request = 0; |
int bad_request = 0; |
| HTTP_REQUEST *new_r = NULL; |
char *buf; |
| |
char *new_host = NULL; |
| |
char *new_file = NULL; |
| |
|
| while (1) { |
#define BUF_SIZE 1024 |
| char buf[1024]; |
|
| |
|
| if (fgets(buf, 1023, r->frw) == NULL) |
if ((buf = (char *) malloc((BUF_SIZE)*sizeof(char))) == NULL) { |
| |
fprintf(stderr, "Memory aloocation failed!\n"); |
| |
exit(1); |
| |
} |
| |
|
| |
while (1) { |
| |
memset(buf, 0, BUF_SIZE); |
| |
if (fgets(buf, ((BUF_SIZE) -1), r->frw) == NULL) |
| break; |
break; |
| strrmcr(buf); |
buf[BUF_SIZE -1] = '\0'; |
| |
|
| |
strrmeol(buf); |
| |
|
| if (r->ret_val == NULL) |
if (r->ret_val == NULL) |
| r->ret_val = strdup(buf); |
r->ret_val = strdup(buf); |
| Riadok 667 static void http_read_source(HTTP_REQUES |
|
| Riadok 576 static void http_read_source(HTTP_REQUES |
|
| if (search_for_link == 1) { /* Handling " 302 Object Moved" {{{ */ |
if (search_for_link == 1) { /* Handling " 302 Object Moved" {{{ */ |
| if (buf == strstr(buf, "Location: ")) { |
if (buf == strstr(buf, "Location: ")) { |
| char *str; |
char *str; |
| |
char *tmp; |
| |
|
| if ((str = strdup(buf)) == NULL) { |
if ((str = strdup(buf)) == NULL) { |
| fprintf(stderr, "strdup(%s) failed!\n", buf); |
fprintf(stderr, "strdup(%s) failed!\n", buf); |
| exit(1); |
exit(1); |
| } |
} |
| while (strlen(str) > 0 && isspace(str[strlen(str) - 1])) |
strrmeol(str); |
| str[strlen(str) - 1] = '\0'; |
|
| |
|
| new_r = new_request(); |
if ((tmp = strstr((str + 17), "/")) != NULL) { |
| r->method = HTTP_GET; |
new_file = strdup(tmp); |
| if ((new_r->file = strstr((str + 17), "/")) != NULL) { |
new_host = (char *) |
| new_r->host = (char *) |
malloc ((tmp - str - 16)*sizeof(char)); |
| malloc ((new_r->file - str - 16)*sizeof(char)); |
strncpy(new_host, (str + 17), (tmp - str - 17)); |
| strncpy(new_r->host, (str + 17), |
new_host[tmp - str - 17] = '\0'; |
| (new_r->file - str - 17)); |
|
| new_r->host[new_r->file - str - 17] = '\0'; |
|
| #ifdef DEBUG |
#ifdef DEBUG |
| fprintf(stderr, "FILENAME = '%s', HOST = '%s'\n", |
fprintf(stderr, "FILENAME = '%s', HOST = '%s'\n", |
| new_r->file, |
new_file, |
| new_r->host); |
new_host); |
| #endif |
#endif |
| } |
} |
| #ifdef DEBUG |
#ifdef DEBUG |
| Riadok 701 static void http_read_source(HTTP_REQUES |
|
| Riadok 608 static void http_read_source(HTTP_REQUES |
|
| } |
} |
| } /* }}} End of Handling '302 Object Moved' */ |
} /* }}} End of Handling '302 Object Moved' */ |
| if (info) { |
if (info) { |
| fprintf(stderr, "\t%.*s\n", (int)(strlen(buf)-1), buf); |
fprintf(stderr, "\t%.*s\n", (int)(strlen(buf)), buf); |
| info=0; |
info=0; |
| if (strstr(buf, "HTTP/1.") != NULL) { |
if (strstr(buf, "HTTP/1.") != NULL) { |
| if ((buf+8) == strstr(buf, " 200 OK")) { |
if ((buf+8) == strstr(buf, " 200 OK")) { |
| Riadok 741 static void http_read_source(HTTP_REQUES |
|
| Riadok 648 static void http_read_source(HTTP_REQUES |
|
| fclose (r->frw); |
fclose (r->frw); |
| if (bad_request) |
if (bad_request) |
| exit(1); |
exit(1); |
| if (new_r != NULL) |
if (new_host != NULL && new_file != NULL) { |
| http_request_by_request(new_r); |
http_request(HTTP_GET, new_file, new_host, NULL); |
| |
free(new_host); |
| |
free(new_file); |
| |
} |
| |
|
| |
free(buf); |
| |
|
| } // }}} |
} // }}} |
| // int http_request_by_request(HTTP_REQUEST *r) {{{ |
// int http_request_by_request(HTTP_REQUEST *r) {{{ |