web_socket_misc.c File Reference

Helper functions for WebSockets. More...

#include <stdlib.h>
#include "core/net.h"
#include "web_socket/web_socket.h"
#include "web_socket/web_socket_auth.h"
#include "web_socket/web_socket_frame.h"
#include "web_socket/web_socket_transport.h"
#include "web_socket/web_socket_misc.h"
#include "encoding/base64.h"
#include "hash/sha1.h"
#include "str.h"
#include "debug.h"

Go to the source code of this file.

Macros

#define TRACE_LEVEL   WEB_SOCKET_TRACE_LEVEL
 

Functions

void webSocketChangeState (WebSocket *webSocket, WebSocketState newState)
 Update WebSocket state. More...
 
error_t webSocketParseHandshake (WebSocket *webSocket)
 Parse client or server handshake. More...
 
error_t webSocketParseRequestLine (WebSocket *webSocket, char_t *line)
 Parse the Request-Line of the client's handshake. More...
 
error_t webSocketParseStatusLine (WebSocket *webSocket, char_t *line)
 Parse the Status-Line of the server's handshake. More...
 
error_t webSocketParseHeaderField (WebSocket *webSocket, char_t *line)
 Parse a header field. More...
 
void webSocketParseConnectionField (WebSocket *webSocket, char_t *value)
 Parse Connection header field. More...
 
error_t webSocketFormatClientHandshake (WebSocket *webSocket, uint16_t serverPort)
 Format client's handshake. More...
 
error_t webSocketFormatServerHandshake (WebSocket *webSocket)
 Format server's handshake. More...
 
error_t webSocketFormatErrorResponse (WebSocket *webSocket, uint_t statusCode, const char_t *message)
 Format HTTP error response. More...
 
error_t webSocketVerifyClientHandshake (WebSocket *webSocket)
 Verify client's handshake. More...
 
error_t webSocketVerifyServerHandshake (WebSocket *webSocket)
 Verify server's handshake. More...
 
error_t webSocketGenerateClientKey (WebSocket *webSocket)
 Generate client's key. More...
 
error_t webSocketGenerateServerKey (WebSocket *webSocket)
 Generate server's key. More...
 
error_t webSocketVerifyClientKey (WebSocket *webSocket)
 Verify client's key. More...
 
error_t webSocketVerifyServerKey (WebSocket *webSocket)
 Verify server's key. More...
 
bool_t webSocketCheckStatusCode (uint16_t statusCode)
 Check whether a status code is valid. More...
 
error_t webSocketDecodePercentEncodedString (const char_t *input, char_t *output, size_t outputSize)
 Decode a percent-encoded string. More...
 
bool_t webSocketCheckUtf8Stream (WebSocketUtf8Context *context, const uint8_t *data, size_t length, size_t remaining)
 Check whether a an UTF-8 stream is valid. More...
 

Variables

const char_t webSocketGuid [] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
 

Detailed Description

Helper functions for WebSockets.

License

Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.

This file is part of CycloneTCP Open.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Author
Oryx Embedded SARL (www.oryx-embedded.com)
Version
1.9.0

Definition in file web_socket_misc.c.

Macro Definition Documentation

◆ TRACE_LEVEL

#define TRACE_LEVEL   WEB_SOCKET_TRACE_LEVEL

Definition at line 30 of file web_socket_misc.c.

Function Documentation

◆ webSocketChangeState()

void webSocketChangeState ( WebSocket webSocket,
WebSocketState  newState 
)

Update WebSocket state.

Parameters
[in]webSocketHandle to a WebSocket
[in]newStateNew state to switch to

Definition at line 86 of file web_socket_misc.c.

◆ webSocketCheckStatusCode()

bool_t webSocketCheckStatusCode ( uint16_t  statusCode)

Check whether a status code is valid.

Parameters
[in]statusCodeStatus code
Returns
The function returns TRUE is the specified status code is valid. Otherwise, FALSE is returned

Definition at line 1138 of file web_socket_misc.c.

◆ webSocketCheckUtf8Stream()

bool_t webSocketCheckUtf8Stream ( WebSocketUtf8Context context,
const uint8_t *  data,
size_t  length,
size_t  remaining 
)

Check whether a an UTF-8 stream is valid.

Parameters
[in]contextUTF-8 decoding context
[in]dataPointer to the chunk of data to be processed
[in]lengthData chunk length
[in]remainingnumber of remaining bytes in the UTF-8 stream
Returns
The function returns TRUE is the specified UTF-8 stream is valid. Otherwise, FALSE is returned

Definition at line 1239 of file web_socket_misc.c.

◆ webSocketDecodePercentEncodedString()

error_t webSocketDecodePercentEncodedString ( const char_t input,
char_t output,
size_t  outputSize 
)

Decode a percent-encoded string.

Parameters
[in]inputNULL-terminated string to be decoded
[out]outputNULL-terminated string resulting from the decoding process
[in]outputSizeSize of the output buffer in bytes
Returns
Error code

Definition at line 1177 of file web_socket_misc.c.

◆ webSocketFormatClientHandshake()

error_t webSocketFormatClientHandshake ( WebSocket webSocket,
uint16_t  serverPort 
)

Format client's handshake.

Parameters
[in]webSocketHandle to a WebSocket
[in]serverPortTCP port number used to establish the connection
Returns
Error code

Definition at line 641 of file web_socket_misc.c.

◆ webSocketFormatErrorResponse()

error_t webSocketFormatErrorResponse ( WebSocket webSocket,
uint_t  statusCode,
const char_t message 
)

Format HTTP error response.

Parameters
[in]webSocketHandle to a WebSocket
[in]statusCodeHTTP status code
[in]messageUser message
Returns
Error code

Definition at line 775 of file web_socket_misc.c.

◆ webSocketFormatServerHandshake()

error_t webSocketFormatServerHandshake ( WebSocket webSocket)

Format server's handshake.

Parameters
[in]webSocketHandle to a WebSocket
Returns
Error code

Definition at line 723 of file web_socket_misc.c.

◆ webSocketGenerateClientKey()

error_t webSocketGenerateClientKey ( WebSocket webSocket)

Generate client's key.

Parameters
[in]webSocketHandle to a WebSocket
Returns
Error code

Definition at line 963 of file web_socket_misc.c.

◆ webSocketGenerateServerKey()

error_t webSocketGenerateServerKey ( WebSocket webSocket)

Generate server's key.

Parameters
[in]webSocketHandle to a WebSocket
Returns
Error code

Definition at line 1007 of file web_socket_misc.c.

◆ webSocketParseConnectionField()

void webSocketParseConnectionField ( WebSocket webSocket,
char_t value 
)

Parse Connection header field.

Parameters
[in]webSocketHandle to a WebSocket
[in]valueNULL-terminated string that contains the value of header field

Definition at line 597 of file web_socket_misc.c.

◆ webSocketParseHandshake()

error_t webSocketParseHandshake ( WebSocket webSocket)

Parse client or server handshake.

Parameters
[in]webSocketHandle to a WebSocket
Returns
Error code

Definition at line 105 of file web_socket_misc.c.

◆ webSocketParseHeaderField()

error_t webSocketParseHeaderField ( WebSocket webSocket,
char_t line 
)

Parse a header field.

Parameters
[in]webSocketHandle to a WebSocket
[in]lineNULL-terminated string that contains the header field
Returns
Error code

Definition at line 510 of file web_socket_misc.c.

◆ webSocketParseRequestLine()

error_t webSocketParseRequestLine ( WebSocket webSocket,
char_t line 
)

Parse the Request-Line of the client's handshake.

Parameters
[in]webSocketHandle to a WebSocket
[in]lineNULL-terminated string that contains the Request-Line
Returns
Error code

Definition at line 364 of file web_socket_misc.c.

◆ webSocketParseStatusLine()

error_t webSocketParseStatusLine ( WebSocket webSocket,
char_t line 
)

Parse the Status-Line of the server's handshake.

Parameters
[in]webSocketHandle to a WebSocket
[in]lineNULL-terminated string that contains the Status-Line
Returns
Error code

Definition at line 472 of file web_socket_misc.c.

◆ webSocketVerifyClientHandshake()

error_t webSocketVerifyClientHandshake ( WebSocket webSocket)

Verify client's handshake.

Parameters
[in]webSocketHandle to a WebSocket
Returns
Error code

Definition at line 851 of file web_socket_misc.c.

◆ webSocketVerifyClientKey()

error_t webSocketVerifyClientKey ( WebSocket webSocket)

Verify client's key.

Parameters
[in]webSocketHandle to a WebSocket
Returns
Error code

Definition at line 1047 of file web_socket_misc.c.

◆ webSocketVerifyServerHandshake()

error_t webSocketVerifyServerHandshake ( WebSocket webSocket)

Verify server's handshake.

Parameters
[in]webSocketHandle to a WebSocket
Returns
Error code

Definition at line 900 of file web_socket_misc.c.

◆ webSocketVerifyServerKey()

error_t webSocketVerifyServerKey ( WebSocket webSocket)

Verify server's key.

Parameters
[in]webSocketHandle to a WebSocket
Returns
Error code

Definition at line 1088 of file web_socket_misc.c.

Variable Documentation

◆ webSocketGuid

const char_t webSocketGuid[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"

Definition at line 49 of file web_socket_misc.c.