d

 avatar
unknown
c_cpp
2 years ago
3.6 kB
5
Indexable
include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/types.h>
#include <netdb.h>
#define PORT 80

int main() {
        int sockfd, new_socket, valread;
        char link[1000] = {};
        char hostName[1000] = {};
        char path[1000] = {};

        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd <= 0) {
                perror("socket failed");
                exit(EXIT_FAILURE);
        }

        struct sockaddr_in info;
        bzero(&info, sizeof(info));
        info.sin_family = AF_INET;
        info.sin_port = htons(PORT);

        printf("Enter the hostname: ");
        scanf("%s", link);

        int length = strlen(link);
        int i = 0;
        for (i = 0; i < length; i++) {
                if (link[i] == '/') {
                        break;
                }
                hostName[i] = link[i];
        }

        while (i < length) {
                path[i] = link[i];
                i++;
        }

        struct hostent *h = gethostbyname(hostName);

        if (h == NULL) return 1;

        memcpy(&info.sin_addr, h->h_addr_list[0], h->h_length);

        int err = connect(sockfd, (struct sockaddr *) &info, sizeof(info));
        if (err == -1) printf("ConnectionERROR");

        char buffer[1000];
        char request[1000];
        char reqFmt = "GET /%s HTTP/1.1\r\n";
        char hostFmt = "Host : %s\r\n";

        sprintf(request, reqFmt, path);
        sprintf(buffer, hostFmt, hostName);
        strcat(request, buffer);
        strcat(request, "\r\n");

        if (send(sockfd, request, sizeof(request), 0) < 0) {
                printf("socket: Failed to send HTTP request");
                return 1;
        }
        printf("socket: Start send HTTP request\n");

        char response[100000] = {};
        if (recv(sockfd, response, 100000, MSG_WAITALL) < 0) {
                printf("socket: Failed to receive response");
                return 1;
        }
        printf("socket: Start read the response\n");
        printf("socket: Finish read to buffer\n");
        printf("======== Hyperlinks ========\n");

        int count = 0;

        int replen = strlen(response);

        for (int j = 0; j < replen; j++) {
                if (response[j] == 'a' && response[j+1] == ' ' && response[j+2] == 'h' && response[j+3] == 'r' && response[j+4] == 'e' && response[j+5] == 'f') {
                        for (int k = j; ; k++) {
                                if (response[k] == '"') {
                                        count++;
                                        printf("\n");
                                        break;
                                }
                                printf("%c", response[k]);
                        }
                }
                else if (response[j] == 'A' && response[j+1] == ' ' && response[j+2] == 'H' && response[j+3] == 'R' && response[j+4] == 'E' && response[j+5] == 'F') {
                        for (int k = j; ; k++) {
                                if (response[k] == '"') {
                                        count++;
                                        printf("\n");
                                        break;
                                }
                                printf("%c", response[k]);
                        }
                }
        }
        printf("There are %d hyperlinks in http://%s/.\n", count, hostName);

        close(sockfd);
        return 0;
}



Editor is loading...