Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Remove asserts

  • Loading branch information...
commit 5f236e968de8dc2ce7a5fdbf50dd155a32ec0c3b 1 parent a56afa6
@nathanj authored
Showing with 36 additions and 17 deletions.
  1. +7 −5 server/key.c
  2. +1 −1  server/key.h
  3. +28 −11 server/server.c
View
12 server/key.c
@@ -2,7 +2,6 @@
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
-#include <assert.h>
#include <arpa/inet.h>
#include "sha1.h"
@@ -13,7 +12,7 @@
/* Computes the server's response based on the three keys. The response
* is stored in response which should be at least 28 bytes in length. */
-void compute_response(const char *key, char *response)
+int compute_response(const char *key, char *response)
{
char full_key[FULL_KEY_SIZE];
int len;
@@ -21,13 +20,14 @@ void compute_response(const char *key, char *response)
SHA1Context sha1_context;
len = snprintf(full_key, FULL_KEY_SIZE, "%s%s", key, GUID);
- assert(len < (int) FULL_KEY_SIZE);
+ if (len < 0 || len >= (int) FULL_KEY_SIZE)
+ return -1;
SHA1Reset(&sha1_context);
SHA1Input(&sha1_context, (unsigned char *) full_key, len);
rc = SHA1Result(&sha1_context);
-
- assert(rc == 1);
+ if (rc != 1)
+ return -1;
for (int i = 0; i < 5; i++) {
sha1_context.Message_Digest[i] =
@@ -38,5 +38,7 @@ void compute_response(const char *key, char *response)
base64encode((unsigned char *) sha1_context.Message_Digest,
(unsigned char *) response,
20);
+
+ return 0;
}
View
2  server/key.h
@@ -1,6 +1,6 @@
#ifndef __KEY_H__
#define __KEY_H__
-void compute_response(const char *key, char *response);
+int compute_response(const char *key, char *response);
#endif /* __KEY_H__ */
View
39 server/server.c
@@ -9,7 +9,6 @@
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
-#include <assert.h>
#include <event.h>
@@ -109,7 +108,11 @@ static int handle_handshake(struct client *client)
if (client->partial_line) {
rc = asprintf(&line, "%s%s",
client->partial_line, start);
- assert(rc != -1);
+ if (rc == -1) {
+ dbg("asprintf: %s\n", strerror(errno));
+ return -1;
+ }
+
line_alloced = 1;
FREE(client->partial_line);
} else {
@@ -136,10 +139,20 @@ static int handle_handshake(struct client *client)
if (!client->finished_headers)
return 1;
- assert(client->version >= 8);
- assert(client->key);
+ if (client->version < 8) {
+ dbg("unsupported websocket version: %d\n", client->version);
+ return -1;
+ }
+ if (!client->key) {
+ dbg("client did not give a key\n");
+ return -1;
+ }
- compute_response(client->key, response);
+ rc = compute_response(client->key, response);
+ if (rc != 0) {
+ dbg("failed to compute response: %d\n", rc);
+ return -1;
+ }
client->out_len =
snprintf(client->out, BUF_SIZE,
@@ -178,7 +191,10 @@ static int handle_websocket_frame(struct client *client,
while (client->in_len > 0) {
/* Expect a finished text frame. */
- assert(in[0] == '\x81');
+ if (in[0] != '\x81') {
+ dbg("unexpected websocket frame: 0x%02x\n", in[0]);
+ return -1;
+ }
packet_length = ((unsigned char) in[1]) & 0x7f;
@@ -192,7 +208,10 @@ static int handle_websocket_frame(struct client *client,
in[6 + i] ^= mask[i % 4];
rc = asprintf(&buffer, "%.*s", packet_length, in + 6);
- assert(rc != -1);
+ if (rc == -1) {
+ perror("asprintf");
+ return -1;
+ }
dbg("fd=%d is relaying: %s\n", client->fd, buffer);
@@ -211,11 +230,9 @@ static int handle_websocket_frame(struct client *client,
static int handle_input(struct client *client, struct list_head *client_list)
{
if (client->finished_headers)
- handle_websocket_frame(client, client_list);
+ return handle_websocket_frame(client, client_list);
else
- handle_handshake(client);
-
- return 0;
+ return handle_handshake(client);
}
static void delete_client(struct client *client)
Please sign in to comment.
Something went wrong with that request. Please try again.