diff --git a/README.md b/README.md index 30910a2..ada1506 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # clii -command line flashii chatii (pronounced klee) \ No newline at end of file +command line flashii chatii (pronounced klee) diff --git a/src/wsock.c b/src/wsock.c index 69feb9d..43e7363 100644 --- a/src/wsock.c +++ b/src/wsock.c @@ -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); - if(plen != NULL) - *plen = length; + *out = malloc(length); - char* out = malloc(length); - _buffer_read(buf, out); - return out; + _buffer_read(buf, *out); + return length; } -char* buffer_read_str(buffer_t* buf, int* plen) { +int buffer_read_str(buffer_t* buf, char** out) { int length = buffer_length(buf); - if(plen != NULL) - *plen = length; - - char* out = malloc(length + 1); + *out = malloc(length + 1); out[length] = '\0'; - _buffer_read(buf, out); - return out; + + _buffer_read(buf, *out); + return length; } -char* buffer_flush(buffer_t* buf, int* plen) { - char* out = buffer_read(buf, plen); +int buffer_flush(buffer_t* buf, char** out) { + int length = buffer_read(buf, out); buffer_clear(buf); return out; } -char* buffer_flush_str(buffer_t* buf, int* plen) { - char* out = buffer_read_str(buf, plen); +int buffer_flush_str(buffer_t* buf, char** out) { + int length = buffer_read_str(buf, out); buffer_clear(buf); - return out; + return length; } void _buffer_clear(buffer_t* buf, int root) { @@ -172,7 +168,42 @@ wsock_t* wsock_open s_shake[total] = '\0'; } - shutdown(sock, SHUT_RDWR); - return NULL; + const char resp_check[] = "HTTP/1.1 101 Switching Protocols"; + 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); } diff --git a/src/wsock.h b/src/wsock.h index 10612fa..2b982c2 100644 --- a/src/wsock.h +++ b/src/wsock.h @@ -73,14 +73,15 @@ typedef enum { typedef struct { int sock, mode; - buffer_t buffer; + buffer_t* buffer; } wsock_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_recv(wsock_t*, int wsock_is_open(wsock_t*); void wsock_close(wsock_t*); +