/* Include CUPS header files */ #include /* * 'main()' - Get and display a web page. */ int /* O - Exit status */ main(int argc, /* I - Number of command-line arguments */ char *argv[]) /* I - Command-line arguments */ { http_t *http; http_status_t status; char scheme[HTTP_MAX_URI]; char userpass[HTTP_MAX_URI]; char server[HTTP_MAX_URI]; int port; char resource[HTTP_MAX_URI]; http_encryption_t encryption; char username[33]; char *password; char encoded[255]; int bytes; char buffer[1024]; /* Check the command-line. */ if (argc != 2) { puts("Usage: browse "); return (1); } /* Separate the URL into its components. */ httpSeparate(argv[1], scheme, userpass, server, &port, resource); /* Determine if we need to use SSL. */ if (strcmp(scheme, "https") == 0) encryption = HTTP_ENCRYPT_ALWAYS; else encryption = HTTP_ENCRYPT_IF_REQUESTED; /* Connect to the server. */ if ((http = httpConnectEncrypt(server, port, encryption)) == NULL) { perror(server); return (1); } /* Encode the userpass string. */ if (userpass[0]) { strcpy(encoded, "Basic "); httpEncode64(encoded + 6, userpass); } else encoded[0] = '\0'; /* Send a GET request, retrying as necessary... */ do { /* Set HTTP fields and send the request. */ httpClearFields(http); httpSetField(http, HTTP_FIELD_AUTHORIZATION, encoded); httpGet(http, resource); /* Wait for a response from the server. */ while ((status = httpUpdate(http)) == HTTP_CONTINUE); /* Do authentication or encryption as needed. */ if (status == HTTP_UNAUTHORIZED) { /* Get the username and password. */ printf("Username: "); if (gets(username) == NULL) break; if ((password = getpass("Password: ")) == NULL) break; /* Encode the username and password */ sprintf(userpass, "%s:%s", username, password); strcpy(encoded, "Basic "); httpEncode64(encoded + 6, userpass); /* Flush the error message. */ httpFlush(http); } else if (status == HTTP_UPGRADE_REQUIRED) { /* Flush the error message. */ httpFlush(http); /* Upgrade to encryption */ httpEncryption(http, HTTP_ENCRYPT_REQUIRED); } } while (status == HTTP_UPGRADE_REQUIRED || status == HTTP_UNAUTHORIZED); /* Now display the system error or data from the server. */ if (status == HTTP_ERROR) { printf("A system error occurred:\n\t%s\n", strerror(httpError(http))); } else { while ((bytes = httpRead(http, buffer, sizeof(buffer) - 1)) > 0) { buffer[bytes] = '\0'; fputs(buffer, stdout); } } /* Close the server connection and delete the HTTP object. */ httpClose(http); /* Return with no error */ return (0); }