moar wsocking

does it do the thing if i add a new line in the commot
This commit is contained in:
reemo 2023-12-18 08:37:43 -06:00
parent 2766e50f47
commit 1cb5c464bb
3 changed files with 56 additions and 24 deletions

View file

@ -1,3 +1,3 @@
# clii # clii
command line flashii chatii (pronounced klee) command line flashii chatii (pronounced klee)

View file

@ -48,37 +48,33 @@ void _buffer_read(buffer_t* buf, char* out) {
} }
} }
char* buffer_read(buffer_t* buf, int* plen) { int buffer_read(buffer_t* buf, char** out) {
int length = buffer_length(buf); int length = buffer_length(buf);
if(plen != NULL) *out = malloc(length);
*plen = length;
char* out = malloc(length); _buffer_read(buf, *out);
_buffer_read(buf, out); return length;
return out;
} }
char* buffer_read_str(buffer_t* buf, int* plen) { int buffer_read_str(buffer_t* buf, char** out) {
int length = buffer_length(buf); int length = buffer_length(buf);
if(plen != NULL) *out = malloc(length + 1);
*plen = length;
char* out = malloc(length + 1);
out[length] = '\0'; out[length] = '\0';
_buffer_read(buf, out);
return out; _buffer_read(buf, *out);
return length;
} }
char* buffer_flush(buffer_t* buf, int* plen) { int buffer_flush(buffer_t* buf, char** out) {
char* out = buffer_read(buf, plen); int length = buffer_read(buf, out);
buffer_clear(buf); buffer_clear(buf);
return out; return out;
} }
char* buffer_flush_str(buffer_t* buf, int* plen) { int buffer_flush_str(buffer_t* buf, char** out) {
char* out = buffer_read_str(buf, plen); int length = buffer_read_str(buf, out);
buffer_clear(buf); buffer_clear(buf);
return out; return length;
} }
void _buffer_clear(buffer_t* buf, int root) { void _buffer_clear(buffer_t* buf, int root) {
@ -172,7 +168,42 @@ wsock_t* wsock_open
s_shake[total] = '\0'; s_shake[total] = '\0';
} }
shutdown(sock, SHUT_RDWR); const char resp_check[] = "HTTP/1.1 101 Switching Protocols";
return NULL; if(strncmp(s_shake, resp_check, sizeof(resp_check) - 1) != 0)
{
shutdown(sock, SHUT_RDWR);
return NULL;
}
wsock_t* wsock = malloc(sizeof(wsock_t));
wsock->sock = sock;
wsock->mode = WS_BLOCK;
wsock->buffer = buffer_create();
if(total - 1 != (end - s_shake) + 3)
buffer_append(wsock->buffer, end + 4,
total - 1 - ((end - s_shake) + 3));
return wsock;
}
void wsock_mode_set(wsock_t* ws, int mode) {
ws->mode = mode | WS_FULL_SEND;
}
int wsock_mode_get(wsock_t* ws) {
return ws->mode;
}
int wsock_is_open(wsock_t* ws) {
return ws->sock >= 0;
}
void wsock_close(wsock_t* ws) {
// todo: send close frame
shutdown(ws->sock, SHUT_RDWR);
buffer_free(ws->buffer);
free(ws);
} }

View file

@ -73,14 +73,15 @@ typedef enum {
typedef struct { typedef struct {
int sock, mode; int sock, mode;
buffer_t buffer; buffer_t* buffer;
} wsock_t; } wsock_t;
wsock_t* wsock_open(const char*, const char*, uint16_t); wsock_t* wsock_open(const char*, const char*, uint16_t);
int wsock_mode_set(wsock_t*, int); void wsock_mode_set(wsock_t*, int);
int wsock_mode_get(wsock_t*); int wsock_mode_get(wsock_t*);
int wsock_recv(wsock_t*,
int wsock_is_open(wsock_t*); int wsock_is_open(wsock_t*);
void wsock_close(wsock_t*); void wsock_close(wsock_t*);