Skip to content

Commit

Permalink
Fix git ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
aidancrowther committed Oct 25, 2023
1 parent 70039de commit 6092dee
Show file tree
Hide file tree
Showing 11 changed files with 4,139 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
buildroot/
*.o
webserver
helloworld
/examples/hello-world/helloworld
/examples/webserver/webserver
207 changes: 207 additions & 0 deletions examples/webserver/FileServer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
/*******************************************************************************
* FILENAME: FileServer.c
*
* PROJECT:
* Bitty HTTP
*
* FILE DESCRIPTION:
* Hello World Example
*
* COPYRIGHT:
* Copyright (c) 2019 Paul Hutchinson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
******************************************************************************/

/*** HEADER FILES TO INCLUDE ***/
#include "WebServer.h"
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

/*** DEFINES ***/

/*** MACROS ***/

/*** TYPE DEFINITIONS ***/
struct FileInfo
{
const char *Filename; // With Path
bool Dynamic;
const char **Cookies;
const char **Gets;
const char **Posts;
void (*WriteFile)(struct WebServer *Web);
};

/*** FUNCTION PROTOTYPES ***/
void File_Root(struct WebServer *Web);

/*** VARIABLE DEFINITIONS ***/
struct FileInfo m_Files[]=
{
/* Filename, Dynamic, Cookies, Gets, Posts, Callback */
{"/",false,NULL,NULL,NULL,File_Root},
};

/*******************************************************************************
* NAME:
* FS_GetFileProperties
*
* SYNOPSIS:
* bool FS_GetFileProperties(const char *Filename,
* struct WSPageProp *PageProp);
*
* PARAMETERS:
* Filename [I] -- The filename from the URL that is being requested.
* PageProp [O] -- This is filled in with info about the page.
* FileID -- The ID of the page. This has no meaning
* to the web server it is just passed back
* to FS_SendFile(). It can hold a pointer.
* DynamicFile -- If this is true then the file will
* no be cached. false will set the
* ETAG to 'DOCVER' where it will not
* resent to the browser until 'DOCVER'
* changes.
* Cookies -- A pointer to the list of cookies that this
* page accepts.
* Gets -- A pointer to the list of GET vars that this
* page accepts.
* Posts -- A pointer to the list of POST vars that this
* page accepts.
*
* FUNCTION:
* This function is called when a new request comes in for a file. This
* is before the headers for this request have been processed and the
* web server is not yet ready to send a reply.
*
* The web server does need to know if this is a valid file and some info
* about the file. That is what this function provides.
*
* RETURNS:
* true -- File known and can be sent
* false -- File is known. Will produce a 404 reply.
*
* SEE ALSO:
* FS_SendFile()
******************************************************************************/
bool FS_GetFileProperties(const char *Filename,struct WSPageProp *PageProp)
{
int r;

for(r=0;r<sizeof(m_Files)/sizeof(struct FileInfo);r++)
{
if(strcmp(Filename,m_Files[r].Filename)==0)
{
PageProp->FileID=(uintptr_t)&m_Files[r];
PageProp->DynamicFile=m_Files[r].Dynamic;
PageProp->Cookies=m_Files[r].Cookies;
PageProp->Gets=m_Files[r].Gets;
PageProp->Posts=m_Files[r].Posts;
return true;
}
}
return false;
}

/*******************************************************************************
* NAME:
* FS_SendFile
*
* SYNOPSIS:
* void FS_SendFile(struct WebServer *Web,uintptr_t FileID);
*
* PARAMETERS:
* Web [I] -- The web context for this web connection.
* FileID [I] -- The number that was setup in FS_GetFileProperties().
* This has no meaning to the web server and is just passed
* to this function. It can be a pointer to some object
* of your defining.
*
* FUNCTION:
* This function is called from the web server when it is time to send the
* contents of a "file". You may also set headers using WS_Header() as
* long as you haven't sent anything else yet.
*
* RETURNS:
* NONE
*
* SEE ALSO:
* WS_Header(), WS_WriteWhole(), WS_WriteChunk(), WS_GET(), WS_COOKIE(),
* WS_POST(), WS_SetCookie()
******************************************************************************/
void FS_SendFile(struct WebServer *Web,uintptr_t FileID)
{
struct FileInfo *File=(struct FileInfo *)FileID;

/* Not needed but I always check... */
if(File==NULL)
return;

File->WriteFile(Web);
}

////////////////////////////////////////////////////////////////////////////////
const char HelloWorldHTML[]=
"<html>"
"<body>"
"<center><h1><u>Hello World From Milk V Duo!</h1></center>"
"<p align='center'>"
"<img src='https://milkv.io/assets/images/duo-v1.2-9bf1d36ef7632ffba032796978cda903.png' width='300'>"
"<br>"
"<i>Don't let your Milk go spoiled!</i>"
"</p>"
"</body>"
"</html>";

// Added a file reader to generate the sent character array from a sourced HTML file
char* load_file(char const* path)
{
char* buffer = 0;
long length;
FILE * f = fopen (path, "rb"); //was "rb"

if (f)
{
fseek (f, 0, SEEK_END);
length = ftell (f);
fseek (f, 0, SEEK_SET);
buffer = (char*)malloc ((length+1)*sizeof(char));
if (buffer)
{
fread (buffer, sizeof(char), length, f);
}
fclose (f);
}
buffer[length] = '\0';
// for (int i = 0; i < length; i++) {
// printf("buffer[%d] == %c\n", i, buffer[i]);
// }
//printf("buffer = %s\n", buffer);

return buffer;
}

void File_Root(struct WebServer *Web)
{
char* index = load_file("index.html");
WS_WriteWhole(Web,index,strlen(index)-1);
}
35 changes: 35 additions & 0 deletions examples/webserver/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
TARGET=webserver

ifeq (,$(TOOLCHAIN_PREFIX))
$(error TOOLCHAIN_PREFIX is not set)
endif

ifeq (,$(CFLAGS))
$(error CFLAGS is not set)
endif

ifeq (,$(LDFLAGS))
$(error LDFLAGS is not set)
endif

CC = $(TOOLCHAIN_PREFIX)gcc

CFLAGS += -I$(SYSROOT)/usr/include

LDFLAGS += -L$(SYSROOT)/lib
LDFLAGS += -L$(SYSROOT)/usr/lib

SOURCE = $(wildcard *.c)
OBJS = $(patsubst %.c,%.o,$(SOURCE))

$(TARGET): $(OBJS)
$(CC) -o $@ $(OBJS) $(LDFLAGS) -static

%.o: %.c
$(CC) $(CFLAGS) -o $@ -c $<

.PHONY: clean
clean:
@rm *.o -rf
@rm $(OBJS) -rf
@rm $(TARGET)
56 changes: 56 additions & 0 deletions examples/webserver/Options.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*******************************************************************************
* FILENAME: Options.h
*
* PROJECT:
* Bitty HTTP
*
* FILE DESCRIPTION:
* This file has things that you might want to change to config the web
* server in it.
*
* COPYRIGHT:
* Copyright (c) 2019 Paul Hutchinson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*******************************************************************************/
#ifndef __OPTIONS_H_
#define __OPTIONS_H_

/*** HEADER FILES TO INCLUDE ***/

/*** DEFINES ***/
#define DOCVER "1.0.0.0"

#define WS_OPT_MAX_CONNECTIONS 16 // The max number of connections we can handle at the same time (this will include buffers needed for each connection)
#define WS_OPT_ARG_MEMORY_SIZE 100 // The memory block to use to store the cookies, get args, and post args
#define WS_SECONDS_UNTIL_CONNECTION_RELEASE 10 // How many seconds to wait after a connection stops sending to us before we hang up
#define WS_LINE_BUFFER_SIZE 256 // The max number of bytes we can handle a single header line can be (including the GET line). This is normally in the order of 16K - 128K (we default to a lot less)

/*** MACROS ***/

/*** TYPE DEFINITIONS ***/

/*** CLASS DEFINITIONS ***/

/*** GLOBAL VARIABLE DEFINITIONS ***/

/*** EXTERNAL FUNCTION PROTOTYPES ***/

#endif
13 changes: 13 additions & 0 deletions examples/webserver/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
This example is made using the work of the following project (https://github.com/TheBeef/BittyHTTP). I have added a simple file loader to parse an HTML file called index.html in the same directory as the output
server binary. This file will have to be loaded into memory to be served, so don't make it too large.

As this server will only work with HTTP1.1, passing it through a reverse proxy like Nginx may require that you enforce a specific implementation of HTTP.

build with

```
docker run -it \
-v $(pwd):/home/milkv/buildroot \
ghcr.io/aidancrowther/milkvduocompile:latest \
"cd examples/webserver && make"
```
Loading

0 comments on commit 6092dee

Please sign in to comment.