Update.
This commit is contained in:
		
							parent
							
								
									e02315a0f6
								
							
						
					
					
						commit
						91598ce1ef
					
				
							
								
								
									
										4
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								Makefile
									
									
									
									
									
								
							| @ -1,11 +1,11 @@ | ||||
| all: build run | ||||
| 
 | ||||
| build:	 | ||||
| 	gcc main.c -lssl -lcrypto -ljson-c -Ofast -o r -Werror -Wall -lpython3.14 -I/usr/include/python3.14 -lreadline -lncurses | ||||
| 	gcc main.c -lcurl -lssl -lcrypto -ljson-c -Ofast -o r -Werror -Wall -lpython3.14 -I/usr/include/python3.14 -lreadline -lncurses | ||||
| 	publish r | ||||
| 
 | ||||
| build_free: | ||||
| 	gcc main.c -DFREE_VERSION -lssl -lcrypto -ljson-c -Ofast -o rf -Werror -Wall -lpython3.14 -I/usr/include/python3.14 -lreadline -lncurses | ||||
| 	gcc main.c -lcurl -DFREE_VERSION -lssl -lcrypto -ljson-c -Ofast -o rf -Werror -Wall -lpython3.14 -I/usr/include/python3.14 -lreadline -lncurses | ||||
| 	publish rf | ||||
| 
 | ||||
| run: | ||||
|  | ||||
							
								
								
									
										4
									
								
								auth.h
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								auth.h
									
									
									
									
									
								
							| @ -6,6 +6,8 @@ | ||||
| 
 | ||||
| // MIT License
 | ||||
| 
 | ||||
| #ifndef R_AUTH_H | ||||
| #define R_AUTH_H | ||||
| #include <stdlib.h> | ||||
| #include <stdio.h> | ||||
| 
 | ||||
| @ -28,3 +30,5 @@ const char * resolve_api_key(){ | ||||
|     api_key = "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"; | ||||
|     return api_key; | ||||
| } | ||||
| 
 | ||||
| #endif | ||||
|  | ||||
							
								
								
									
										33
									
								
								http.h
									
									
									
									
									
								
							
							
						
						
									
										33
									
								
								http.h
									
									
									
									
									
								
							| @ -166,8 +166,8 @@ char *https_post(char *url, char *data) | ||||
|     SSL_set_tlsext_host_name(ssl, hostname); | ||||
|     SSL_set_fd(ssl, sock); | ||||
| 
 | ||||
|     int buffer_size = 4096; | ||||
|     char *buffer = malloc(buffer_size); | ||||
|     int buffer_size = 1024*1024; | ||||
|     char *buffer = (char *)malloc(buffer_size); | ||||
|     size_t chunk_size_total = 0; | ||||
|     if (SSL_connect(ssl) <= 0) | ||||
|     { | ||||
| @ -176,7 +176,7 @@ char *https_post(char *url, char *data) | ||||
|     else | ||||
|     { | ||||
|         size_t len = strlen(data); | ||||
|         char *request = malloc(len + 4096); | ||||
|         char *request = (char *)malloc(len + 1024*1024); | ||||
|         sprintf(request, | ||||
|                 "POST %s HTTP/1.1\r\n" | ||||
|                 "Content-Length: %ld\r\n" | ||||
| @ -203,7 +203,7 @@ char *https_post(char *url, char *data) | ||||
|             while (remaining > 0) | ||||
|             { | ||||
|                 size_t to_read = (remaining < buffer_size) ? remaining : buffer_size; | ||||
|                 buffer = realloc(buffer, actual_buffer_size + to_read + 1); | ||||
|                 buffer = (char *)realloc(buffer, actual_buffer_size + to_read + 1); | ||||
|                 actual_buffer_size += to_read; | ||||
|                 size_t bytes_read = SSL_read(ssl, buffer + chunk_size_total, to_read); | ||||
|                 chunk_size_total += bytes_read; | ||||
| @ -275,7 +275,7 @@ char *https_get(char *url) | ||||
|             while (remaining > 0) | ||||
|             { | ||||
|                 size_t to_read = (remaining < buffer_size) ? remaining : buffer_size; | ||||
|                 buffer = realloc(buffer, actual_buffer_size + to_read + 1); | ||||
|                 buffer = (char *)realloc(buffer, actual_buffer_size + to_read + 1); | ||||
|                 actual_buffer_size += to_read; | ||||
|                 size_t bytes_read = SSL_read(ssl, buffer + chunk_size_total, to_read); | ||||
|                 chunk_size_total += bytes_read; | ||||
| @ -307,12 +307,12 @@ char *http_post(char *url, char *data) | ||||
|     int port = atoi(parsed_url.port); | ||||
|     int sock = create_socket(hostname, port); | ||||
|     | ||||
|     int buffer_size = 1024*1024; | ||||
|     char *buffer = malloc(buffer_size); | ||||
|     int buffer_size = 1024 * 1024; | ||||
|     char *buffer = (char *)malloc(buffer_size); | ||||
|     size_t chunk_size_total = 0; | ||||
|    | ||||
|         size_t len = strlen(data); | ||||
|         char *request = malloc(len + buffer_size); | ||||
|         size_t len = strlen(data) + strlen(path) + strlen(resolve_api_key()) + 10; | ||||
|         char *request = (char *)malloc(len + buffer_size); | ||||
|         sprintf(request, | ||||
|                 "POST %s HTTP/1.1\r\n" | ||||
|                 "Content-Length: %ld\r\n" | ||||
| @ -327,7 +327,6 @@ char *http_post(char *url, char *data) | ||||
|         free(request); | ||||
| 
 | ||||
|         char *headers = read_until(sock, "\r\n\r\n"); | ||||
|         printf("%s\n",headers); | ||||
|         (void)headers; | ||||
|      | ||||
|         size_t actual_buffer_size = buffer_size; | ||||
| @ -336,24 +335,30 @@ char *http_post(char *url, char *data) | ||||
|             char *header = read_until(sock, "\r\n"); | ||||
|             size_t chunk_size = hex_to_int(header); | ||||
|             if (chunk_size == 0) | ||||
|             {    printf("END\n"); | ||||
|                 break; | ||||
|             } | ||||
|             size_t remaining = chunk_size; | ||||
|             while (remaining > 0) | ||||
|             { | ||||
|                 size_t to_read = (remaining < buffer_size) ? remaining : buffer_size; | ||||
|                 buffer = realloc(buffer, actual_buffer_size + to_read + 1); | ||||
|                 buffer = (char *)realloc(buffer, actual_buffer_size + to_read + 10); | ||||
|                 actual_buffer_size += to_read; | ||||
|                 size_t bytes_read = recv(sock, buffer + chunk_size_total, to_read,0); | ||||
|                 chunk_size_total += bytes_read; | ||||
| 
 | ||||
|                 if (bytes_read <= 0) | ||||
|                 { | ||||
|                     fprintf(stderr, "Error reading chunk data\n"); | ||||
|                     return NULL; | ||||
|                 } | ||||
|                  | ||||
|                 chunk_size_total += bytes_read; | ||||
| 
 | ||||
|                 // fwrite(buffer, 1, bytes_read, stdout); // Output chunk data
 | ||||
|                 remaining -= bytes_read; | ||||
|             } | ||||
|         } | ||||
|         printf("HIERRR!\n"); | ||||
|         buffer[chunk_size_total] = 0; | ||||
|      | ||||
| 
 | ||||
| @ -373,7 +378,7 @@ char *http_get(char *url) | ||||
|     int sock = create_socket(hostname, port); | ||||
|     | ||||
| 
 | ||||
|     int buffer_size = 4096; | ||||
|     int buffer_size = 1024*1024; | ||||
|     char *buffer = (char *)malloc(buffer_size); | ||||
| 
 | ||||
|    | ||||
| @ -401,7 +406,7 @@ char *http_get(char *url) | ||||
|             while (remaining > 0) | ||||
|             { | ||||
|                 size_t to_read = (remaining < buffer_size) ? remaining : buffer_size; | ||||
|                 buffer = realloc(buffer, actual_buffer_size + to_read + 1); | ||||
|                 buffer = (char *)realloc(buffer, actual_buffer_size + to_read + 1); | ||||
|                 actual_buffer_size += to_read; | ||||
|                 size_t bytes_read = recv(sock, buffer + chunk_size_total, to_read,0); | ||||
|                 chunk_size_total += bytes_read; | ||||
|  | ||||
							
								
								
									
										82
									
								
								http_curl.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										82
									
								
								http_curl.h
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,82 @@ | ||||
| #ifndef HTTP_CURL | ||||
| #define HTTP_CURL | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| #include <string.h> | ||||
| #include <curl/curl.h> | ||||
| #include "auth.h" | ||||
| 
 | ||||
| // Buffer to store the response
 | ||||
| struct ResponseBuffer { | ||||
|     char *data;    // Pointer to the response data
 | ||||
|     size_t size;   // Size of the data
 | ||||
| }; | ||||
| 
 | ||||
| // Callback function to handle the response
 | ||||
| static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) { | ||||
|     size_t total_size = size * nmemb; | ||||
|     struct ResponseBuffer *response = (struct ResponseBuffer *)userp; | ||||
| 
 | ||||
|     // Reallocate memory to fit new data
 | ||||
|     char *ptr = realloc(response->data, response->size + total_size + 1); | ||||
|     if (ptr == NULL) { | ||||
|         fprintf(stderr, "Failed to allocate memory for response\n"); | ||||
|         return 0; // Returning 0 will signal libcurl to abort the request
 | ||||
|     } | ||||
| 
 | ||||
|     // Assign the newly allocated memory to response->data
 | ||||
|     response->data = ptr; | ||||
| 
 | ||||
|     // Copy the new data into the buffer
 | ||||
|     memcpy(&(response->data[response->size]), contents, total_size); | ||||
| 
 | ||||
|     // Update the size of the buffer
 | ||||
|     response->size += total_size; | ||||
| 
 | ||||
|     // Null-terminate the string
 | ||||
|     response->data[response->size] = '\0'; | ||||
| 
 | ||||
|     return total_size; | ||||
| } | ||||
| 
 | ||||
| char * curl_post(const char * url, const char * data) { | ||||
|     CURL *curl; | ||||
|     CURLcode res; | ||||
| 
 | ||||
|     struct ResponseBuffer response; | ||||
|     response.data = (char *)malloc(1);  | ||||
|     response.size = 0;          | ||||
| 
 | ||||
|     curl = curl_easy_init(); | ||||
|     if (curl) { | ||||
|         struct curl_slist *headers = NULL; | ||||
| 
 | ||||
|         curl_easy_setopt(curl, CURLOPT_URL, url); | ||||
| 
 | ||||
|         headers = curl_slist_append(headers, "Content-Type: application/json"); | ||||
|          | ||||
|         char * bearer_header = (char *)malloc(1337); | ||||
|         sprintf(bearer_header, "Authorization: Bearer %s", resolve_api_key()); | ||||
|         headers = curl_slist_append(headers, bearer_header); | ||||
| 
 | ||||
|         free(bearer_header); | ||||
| 
 | ||||
|         curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); | ||||
|         curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); | ||||
|         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); | ||||
|         curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response); | ||||
| 
 | ||||
|         res = curl_easy_perform(curl); | ||||
| 
 | ||||
|         if (res != CURLE_OK) { | ||||
|             fprintf(stderr, "Error occured: %s\n", curl_easy_strerror(res)); | ||||
|         }  | ||||
| 
 | ||||
|         curl_slist_free_all(headers); | ||||
|         curl_easy_cleanup(curl); | ||||
|         return response.data; | ||||
|     } | ||||
| 
 | ||||
|     return 0; | ||||
| } | ||||
| #endif  | ||||
							
								
								
									
										6
									
								
								openai.h
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								openai.h
									
									
									
									
									
								
							| @ -13,6 +13,7 @@ | ||||
| #define R_OPENAI_H | ||||
| #include "http.h" | ||||
| #include "chat.h" | ||||
| #include "http_curl.h" | ||||
| #include <string.h> | ||||
| #include <stdbool.h> | ||||
| 
 | ||||
| @ -24,7 +25,7 @@ char *openai_get_models() { | ||||
| bool openai_system(char *content) { | ||||
|     char *url = "https://api.openai.com/v1/chat/completions"; | ||||
|     char *data = chat_json("system", content); | ||||
|     char *result = https_post(url, data); | ||||
|     char *result = curl_post(url, data); | ||||
|     bool is_done = result != NULL; | ||||
|      | ||||
|     free(result); | ||||
| @ -34,8 +35,7 @@ bool openai_system(char *content) { | ||||
| char *openai_chat(char *role, char *content) { | ||||
|     char *url = "https://api.openai.com/v1/chat/completions"; | ||||
|     char *data = chat_json(role, content); | ||||
|     char *result = https_post(url, data); | ||||
|      | ||||
|     char *result = curl_post(url, data); | ||||
|     struct json_object *parsed_json = json_tokener_parse(result); | ||||
|     if (!parsed_json) { | ||||
|         fprintf(stderr, "Failed to parse JSON.\n"); | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user