// catch_url_init(&port,&return_host); HTSEXT_API T_SOC catch_url_init(int *port, /* 128 bytes */ char *adr) { T_SOC soc = INVALID_SOCKET; char h_loc[256]; if (gethostname(h_loc, sizeof(h_loc)) == 0) { // host name SOCaddr server; if (hts_dns_resolve_nocache(h_loc, &server) != NULL) { // notre host if ((soc = (T_SOC) socket(SOCaddr_sinfamily(server), SOCK_STREAM, 0)) != INVALID_SOCKET) { SOCaddr_initport(server, *port); if (bind(soc, &SOCaddr_sockaddr(server), SOCaddr_size(server)) == 0) { SOCaddr server2; SOClen len = SOCaddr_capacity(server2); if (getsockname(soc, &SOCaddr_sockaddr(server2), &len) == 0) { *port = ntohs(SOCaddr_sinport(server)); // récupérer port if (listen(soc, 1) >= 0) { SOCaddr_inetntoa(adr, 128, server2); } else { #ifdef _WIN32 closesocket(soc); #else close(soc); #endif soc = INVALID_SOCKET; } } else { #ifdef _WIN32 closesocket(soc); #else close(soc); #endif soc = INVALID_SOCKET; } } else { #ifdef _WIN32 closesocket(soc); #else close(soc); #endif soc = INVALID_SOCKET; } } } } return soc; }
// catch_url // returns 0 if error // url: buffer where URL must be stored - or ip:port in case of failure // data: 32Kb HTSEXT_API int catch_url(T_SOC soc, char *url, char *method, char *data) { int retour = 0; // connexion (accept) if (soc != INVALID_SOCKET) { T_SOC soc2; while((soc2 = (T_SOC) accept(soc, NULL, NULL)) == INVALID_SOCKET) ; /* #ifdef _WIN32 closesocket(soc); #else close(soc); #endif */ soc = soc2; /* INFOS */ { SOCaddr server2; SOClen len = SOCaddr_capacity(server2); if (getpeername(soc, &SOCaddr_sockaddr(server2), &len) == 0) { char dot[256 + 2]; SOCaddr_inetntoa(dot, sizeof(dot), server2); sprintf(url, "%s:%d", dot, ntohs(SOCaddr_sinport(server2))); } } /* INFOS */ // réception if (soc != INVALID_SOCKET) { char line[1000]; char protocol[256]; line[0] = protocol[0] = '\0'; // socinput(soc, line, 1000); if (strnotempty(line)) { if (sscanf(line, "%s %s %s", method, url, protocol) == 3) { lien_adrfil af; // méthode en majuscule size_t i; int r = 0; af.adr[0] = af.fil[0] = '\0'; // for(i = 0; method[i] != '\0'; i++) { if ((method[i] >= 'a') && (method[i] <= 'z')) method[i] -= ('a' - 'A'); } // adresse du lien if (ident_url_absolute(url, &af) >= 0) { // Traitement des en-têtes char BIGSTK loc[HTS_URLMAXSIZE * 2]; htsblk blkretour; hts_init_htsblk(&blkretour); //memset(&blkretour, 0, sizeof(htsblk)); // effacer blkretour.location = loc; // si non nul, contiendra l'adresse véritable en cas de moved xx // Lire en têtes restants sprintf(data, "%s %s %s\r\n", method, af.fil, protocol); while(strnotempty(line)) { socinput(soc, line, 1000); treathead(NULL, NULL, NULL, &blkretour, line); // traiter strcatbuff(data, line); strcatbuff(data, "\r\n"); } // CR/LF final de l'en tête inutile car déja placé via la ligne vide juste au dessus //strcatbuff(data,"\r\n"); if (blkretour.totalsize > 0) { int len = (int) min(blkretour.totalsize, 32000); int pos = (int) strlen(data); // Copier le reste (post éventuel) while((len > 0) && ((r = recv(soc, (char *) data + pos, len, 0)) > 0)) { pos += r; len -= r; data[pos] = '\0'; // terminer par NULL } } // Envoyer page sprintf(line, CATCH_RESPONSE); send(soc, line, (int) strlen(line), 0); // OK! retour = 1; } } } // sinon erreur } } if (soc != INVALID_SOCKET) { #ifdef _WIN32 closesocket(soc); /* WSACleanup(); */ #else close(soc); #endif } return retour; }
// catch_url_init(&port,&return_host); HTSEXT_API T_SOC catch_url_init(int* port,char* adr) { T_SOC soc = INVALID_SOCKET; char h_loc[256+2]; if (gethostname(h_loc,256)==0) { // host name SOCaddr server; int server_size=sizeof(server); t_hostent* hp_loc; t_fullhostent buffer; // effacer structure memset(&server, 0, sizeof(server)); if ( (hp_loc=vxgethostbyname(h_loc, &buffer)) ) { // notre host // copie adresse SOCaddr_copyaddr(server, server_size, hp_loc->h_addr_list[0], hp_loc->h_length); if ( (soc = (T_SOC) socket(SOCaddr_sinfamily(server), SOCK_STREAM, 0)) != INVALID_SOCKET) { SOCaddr_initport(server, *port); if ( bind(soc,(struct sockaddr*) &server,server_size) == 0 ) { SOCaddr server2; int len; len=sizeof(server2); // effacer structure memset(&server2, 0, sizeof(server2)); if (getsockname(soc,(struct sockaddr*) &server2,&len) == 0) { *port=ntohs(SOCaddr_sinport(server)); // récupérer port if (listen(soc,10)>=0) { // au pif le 10 SOCaddr_inetntoa(adr, 128, server2, len); } else { #ifdef _WIN32 closesocket(soc); #else close(soc); #endif soc=INVALID_SOCKET; } } else { #ifdef _WIN32 closesocket(soc); #else close(soc); #endif soc=INVALID_SOCKET; } } else { #ifdef _WIN32 closesocket(soc); #else close(soc); #endif soc=INVALID_SOCKET; } } } } return soc; }