FORGOT YOUR DETAILS?

CREATE ACCOUNT

Unison Help

  1. Unison Kernel
    1. Pthreads
      1. pthread_create()
      2. pthread_exit()
      3. pthread_self()
      4. pthread_equal()
      5. pthread_join()
      6. pthread_detach()
      7. pthread_setschedparam()
      8. pthread_getschedparam()
      9. pthread_attr_init()
      10. pthread_attr_destroy()
      11. pthread_attr_setstackaddr()
      12. pthread_attr_getstackaddr()
      13. pthread_attr_setstacksize()
      14. pthread_attr_getstacksize()
      15. pthread_attr_setschedparam()
      16. pthread_attr_getschedparam()
      17. pthread_attr_setdetachstate()
      18. pthread_attr_getdetachstate()
      19. pthread_stackinfo()
      20. pthread_setprio()
      21. pthread_getprio()
      22. sched_get_priority_max()
      23. sched_get_priority_min()
      24. sched_yield()
    2. Pthread Cancellation
      1. pthread_cleanup_pop()
      2. pthread_cleanup_push()
      3. pthread_cancel()
      4. pthread_setcanceltype()
      5. pthread_setcancelstate()
      6. pthread_testcancel()
    3. Mutex
      1. pthread_mutex_init()
      2. pthread_mutex_destroy()
      3. pthread_mutex_lock()
      4. pthread_mutex_trylock()
      5. pthread_mutex_unlock()
    4. Semaphores
      1. sem_open()
      2. sem_close()
      3. sem_unlink()
      4. sem_init()
      5. sem_destroy()
      6. sem_wait()
      7. sem_trywait()
      8. sem_timedwait()
      9. sem_post()
      10. sem_getvalue()
    5. Message Queues
      1. mq_open()
      2. mq_close()
      3. mq_unlink()
      4. mq_send()
      5. mq_receive()
      6. mq_notify()
      7. mq_setattr()
      8. mq_getattr()
      9. mq_timedreceive()
      10. mq_timedsend()
    6. Conditional Variables
      1. pthread_cond_init()
      2. pthread_cond_destroy()
      3. pthread_cond_wait()
      4. pthread_cond_timedwait()
      5. pthread_cond_signal()
      6. pthread_cond_broadcast()
      7. pthread_condattr_init()
      8. pthread_condattr_destroy()
    7. Barriers
      1. pthread_barrier_init()
      2. pthread_barrier_destroy()
      3. pthread_barrier_wait()
    8. Timers
      1. timer_create()
      2. timer_delete()
      3. timer_settime()
      4. timer_gettime()
      5. timer_getoverrun()
      6. timer_tick()
      7. nanosleep()
    9. Clocks
      1. time()
      2. uptime()
      3. sleep()
      4. clock_settime()
      5. clock_gettime()
      6. clock_getres()
      7. clock_init()
    10. Memory Allocation
      1. POSIX.1
        1. malloc()
        2. free()
      2. Variable Length (Pools)
        1. pool_create()
        2. pool_destroy()
        3. pool_alloc()
        4. pool_free()
      3. Fixed Length (Partitions)
        1. pt_create()
        2. pt_destroy()
        3. pt_getblock()
        4. pt_freeblock()
    11. Rendezvous
      1. mr_init()
      2. mr_send()
      3. mr_receive()
      4. mr_reply()
      5. mr_sigrecv()
      6. mr_sigpost()
    12. Interrupts
      1. interrupts
      2. i_disable()
      3. i_restore()
    13. Directory Services
      1. dir_register()
      2. dir_deregister()
      3. dir_lookup()
      4. dir_lookup_string()
    14. Miscellaneous
      1. checkIstack()
      2. NanoStart() or DSPexec_Start()
      3. _isrStackFill
      4. Kernel Scaling
      5. kfatal()
      6. kalloc()
      7. kfree()
      8. mpu or mmu
      9. pthreadStackFill
      10. thread_numb()
      11. thread_utilization_start()
      12. thread_utilization_stop()
      13. xprintf()
      14. xputs()
      15. xputchar()
  2. Unison I/O Library
    1. accept()
    2. bind()
    3. chmod()
    4. close()
    5. connect()
    6. creat()
    7. fstat()
    8. getpeername()
    9. getsockname()
    10. getsockopt()
    11. ioctl()
    12. link()
    13. listen()
    14. lseek()
    15. mkdir()
    16. mkfs()
    17. mount()
    18. open()
    19. read()
    20. recv()
    21. recvfrom()
    22. rename()
    23. renameat()
    24. rmdir()
    25. select()
    26. send()
    27. sendto()
    28. setsockopt()
    29. shutdown()
    30. socket()
    31. stat()
    32. sync()
    33. umount()
    34. unlink()
    35. write()
  3. Unison STDIO Library
    1. STDIO Library Calls
      1. clearerr()
      2. dprintf()
      3. fclose()
      4. fdopen()
      5. feof()
      6. ferror()
      7. fileno()
      8. fflush()
      9. fgetc()
      10. fgetpos()
      11. fgets()
      12. fopen()
      13. fprintf()
      14. fputc()
      15. fputs()
      16. fread()
      17. freopen()
      18. fscanf()
      19. fseek()
      20. fseeko()
      21. fsetpos()
      22. ftell()
      23. ftello()
      24. fwrite()
      25. getc()
      26. getc_unlocked()
      27. getchar()
      28. getchar_unlocked()
      29. getdelim()
      30. getline()
      31. gets()
      32. get_stderr_ptr()
      33. get_stdin_ptr()
      34. get_stdout_ptr()
      35. noperprintf()
      36. perprintf()
      37. perror()
      38. posix_compat()
      39. printf()
      40. putc()
      41. putc_unlocked()
      42. putchar()
      43. putchar_unlocked()
      44. puts()
      45. remove()
      46. rewind()
      47. scanf()
      48. setbuf()
      49. setvbuf()
      50. snprintf()
      51. sprintf()
      52. sscanf()
      53. stderr_init()
      54. stderr_close()
      55. stdin_init()
      56. stdin_close()
      57. stdout_init()
      58. stdout_close()
      59. vdprintf()
      60. vscanf()
      61. vsscanf()
      62. vfscanf()
      63. vprintf()
      64. vsnprintf()
      65. vsprintf()
      66. vfprintf()
      67. ungetc()
    2. Do-nothing Stubs
      1. ctermid()
      2. flockfile()
      3. fmemopen()
      4. ftrylockfile()
      5. open_memstream()
      6. pclose()
      7. popen()
      8. tempnam()
      9. tmpfile()
      10. tmpnam()
  4. Unison LIBC Library
    1. LIBC Library Calls
      1. assert()
      2. realloc()
      3. strcasecmp()
      4. strdup()
      5. strncasecmp()
      6. strftime()
    2. Do-nothing Stubs
      1. abort()
      2. execve()
      3. exit()
      4. _Exit()
      5. fork()
      6. getpid()
      7. isatty()
      8. kill()
      9. sbrk()
      10. times()
      11. wait()
    3. Do-nothing Wide-character Stubs
      1. <wchar.h>
      2. <wctype.h>
  5. Unison I/O Servers
    1. File Servers
      1. Multimedia File Server - fsys
      2. FAT File System - fatfs
      3. NAND File Server - nandfsys
      4. NOR File Server - norfsys
      5. Network File Server - nfs
  6. Graphics, Camera, Video, Audio
    1. Vendor Graphics
    2. Prism++ Graphics
    3. ADPCM Services - adpcmd
    4. Camera
  7. Network Protocols
    1. TCP and UDP Server - tcpd
      1. IPv4 only server
      2. IPv4/IPv6 server
    2. DHCP Client Service - dhcp client
    3. DHCP Server - dhcpd
    4. Telnet Server - telnetd
    5. Tiny FTP Server - tftpd
    6. Point to Point - pppd
    7. Network Translation - NAT with PAT
    8. Firewall
      1. Packet filter: pf
      2. Packet filter control: pfctl
      3. Fitler rules: pf.filtering
      4. Translation rules: pf.nat
    9. Tiny HTTP Server - thttpd
    10. Tiny HTTP Server with TLS
    11. POP3 Server
    12. Simple Mail Transfer Protocol Services (SMTP)
    13. Bootp Protocol
    14. File Transfer Protocol Server (FTP)
    15. File Transfer Client Services
    16. RPC / XDR
    17. DNS Client
    18. HTTP/HTTPS Client
    19. REST Client
    20. AutoIP Service - autoip client
    21. mDNS server - mdnsd
    22. SNTP Client
    23. SNMP Agent - Snmpd server
    24. SSL/TLS library
    25. SSH server
    26. IP security
      1. IPsec description
      2. IPsec administration: ipsecadm
      3. Virtual Private Network: VPN
    27. Power Control
      1. Motor and Motion Control Servers
      2. PWM, Encoders
    28. Serial I/O
      1. Asynchronous Serial I/O Server - ttyserver
      2. CAN Server - cand
      3. I2C Server - i2cd
      4. I2S Server - i2sd
    29. System Services
      1. Power Management Servers
      2. Login Service - login_services
      3. XML
      4. POSIX Shell and Login Service - posh
    30. Universal Serial Bus (USB)
      1. USB Server
      2. USB Device Server
      3. USB Embedded Host Server
    31. Wireless
      1. Wireless Servers and Drivers
      2. 802.15.4 Radio Servers
      3. TCP/v6 with 6loWPAN
      4. ZigBee
      5. BlueTooth Server
      6. 802.11 Wi-Fi
      7. GPRS, UHF and GPS Radio Servers
    32. Remedy Tools for Unison
      1. Remedy Data Logging and Event Display Tools
      2. Remedy Diagnostics
      3. Remedy Flash Downloader/Bootloader
      4. Remedy Power On Self Test - POST
      5. Remedy OS Object Viewer
      6. Remedy Remote Control Tools

7.10.Tiny HTTP Server with TLS #

NAME

Tiny HTTP Server – thttpd

SYNOPSIS

#include <sys.h>

#include <stdio.h>

#include <tcpinit.h>

#include <sys/socket.h>

#include <thttpd.h>

 

thttpd_main((char *)httpdconf_name);

DESCRIPTION

thttpd is simple of use and fast solution for adding HTTP support to your application.

The thttpd server provides the capability to provide the following services and support

  • IP V4/V6 support
  • HTTP 0.9, 1.0 and 1.1 support
  • Support for GET, POST, HEAD
  • Support CGI interface for user applications
  • Minimal Footprint in both RAM and Flash
  • Simple C language callout to deal with forms, buttons and login parse by user
  • Optional SSL/TLS support
  • Optional ogin support
  • Chunked file transfers
  • Secure operation
  • Thread only implementation
  • Support for multiple file systems.
  • Error Responses:
  • HTTP/1.X 404 PAGE NOT FOUND
  • HTTP/1.X 500 SERVER ERROR
  • HTTP/1.X 400 BAD REQUEST

 

Thttpd server requires for work TCP and FatFs/Fsys servers. This servers must be started before run thttpd.

Thttpd starting by command:

thttpd_main((char *)httpdconf_name);

Where httpdconf_name is a full configuration file name. This file holds basic configuration parameters for thttp and has another structure:

Pram:value

Pram:value

Pram:value

Param

Description

Sample

server_software

software product name

UnisonHttpd

host_name

host name, used by http protocol

Myhost

ip_ver

Version of IP protocol (4 or 6)

4

http_index

default start page

index.html

http_root

server root folder

/dev/rd/thttp/

http_port

default http port

80

https_index

default start page for secured (https) connections

index.html

https_root

server root folder for secured (https) connections

/dev/rd/thttp/

https_port

default https port for secured connections

443

maxconn

maximum connections avaible simultaneously

3 (default)

data_timeout

watchdog to handle physical link loss (sec.)

20 (default)

keepalive_timeout

timeout to keep socket opened, after handling browser requests (sec.)

30 (default)

cgi_ena

enable cgi handling (0 or 1)

1

SSLEngine

enable/disable ssl suport (on or off)

on

SSLCertificateFile

SSL/TLS sertificate file path

/dev/rd/thttp/ssl/target_ss_cert.der

SSLCertificateKeyFile

SSL/TLS key file path (if need)

/dev/rd/thttp/ssl/target_key.der

SSLCertificateFormat

SSL/TLS sertificate format

(DEFAULT/PKCS8/PKCS12)

DEFAULT

SSLCertificateKeyPasswd

SSL/TLS sertificate key (if need)

1234

Example of configuration file:

“ip_ver:4″\n” //IP protocol version

“host_name:Thttpd server \n” //host name, used by http protocol
“server_software:UnisonHttpd \n” //software product name

“http_index:index.htm\n” //default start page
“http_root:/dev/rd/thttp/html/\n” //server root folder
“http_port:80\n” //default port

“https_index:index.htm\n” //default start page
“https_root:/dev/rd/thttp/html/\n” //server root folder
“https_port:443\n” //default port

“SSLEngine:on\n”
“SSLCertificateFile:/dev/rd/thttp/ssl/target_ss_cert.der\n”
“SSLCertificateKeyFile:/dev/rd/thttp/ssl/target_key.der\n”
“SSLCertificateFormat:DEFAULT\n”

“cgi_ena:1\n” //enable cgi handling

“data_timeout:20\n” //watchdog to handle physical link loss
“keepalive_timeout:30\n” //timeout to keep socket opened, after handling browser requests
“maxconn:5\n”; //maximum connections which can be opened simultaneously

Login and Authentication Support

Thttpd server can provide access to some pages by password.

To setup the login information, users need to do the following:

  • Put the pages in a sub directory of thttpd which has limited access
  • Create a password file and set it up for the thttpd server.
  • Initialize the login table.
  • Add any users that the user requires

Example:

#include <login.h>
set_loginfile((char *)htpasswd_name); //htpasswd_name - full path to .htpasswd file
init_login_table((char *)htpasswd_name);

 

To add or delete new user run:

add_user( "username", "passwd" );
delete_user( "username");

Now if client access to protected files he must enter valid password and username. Server can support many different users

CGI Support

If application need to have CGI getaway support you need:

  • set in “httpdconffile option “cgi_ena” to 1
  • register cgi handler to one or many server folder.

Example:

register_cgi_handler("name", "URL_path", cgi_handler_funk);

Parameters:

  • namehandler name for server. Name could be any “string”. This name is used to register handler in server and deletes. Any existing handler with the same name. Handler names should NOT be duplicated in system!
  • URL_path – address. which should be specified in the input POST to get server automatically call user function for handling this request. Addresses (or path names) should not be duplicated.

  • cgi_handler_func pointer to function-handler which will handle requests any existing handler with the same name.

It is possible to register a few cgi handlers in system. It is also possible to register one handler for different addresses.

Example:

register_cgi_handler ("test1", "cgi-bin/userI1.cgi", UI_cgi_handler);
register_cgi_handler ("test2", "cgi-bin/userI2.cgi", UI_cgi_handler);
register_cgi_handler ("test3", "cgi-bin/userI3.cgi", ABC_cgi_handler);

 

In this way the server will use one handler UI_cgi_handler for addresses

XXX/cgi-bin/userI1.cgi and XXX/cgi-bin/userI2.cgi. It will use a completely different handler ABC_cgi_handler for XXX/cgibin/userI3.cgi

To delete registerd cgi handler run del_cgi_handler

Example:

del_cgi_handler(“name”);
    • namehandler name for server.

To handle all user data which is input from the Browser a next parameters list is passed into the cgi-handler.

int cgi_handler (char * path , char ** argv , char ** envp, int fd);

*path – path for which this handler is called. For instance, “cgi-bin/userI.cgi”

**argv – pointer array to parameters strings (to date it is 2 max)

argv[0] – called file name. For instance “userI.cgi”

argv[1] – If parameters are passed using method GET, then in argv[1] string with this parameters is passed, else (null)

**envp – pointers array to strings, which is containing information, passed by Browser using http headers (according to CGI standard). This array can contain variable numbers of elements, with the array ended by ‘\0’.

Example:

HTTP_USER_AGENT=Opera/9.80 (Windows NT 5.1; U; ru) Presto/2.2.15 Version/10.10
HTTP_HOST=192.168.16.201
HTTP_ACCEPT=text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/jpeg, image/gif, image/xxbitmap,*/*;q=0.1
HTTP_ACCEPT_LANGUAGE=ru-RU,ru;q=0.9,en;q=0.8
HTTP_ACCEPT_CHARSET=iso-8859-1, utf-8, utf-16, *;q=0.1
HTTP_ACCEPT_ENCODING=deflate, gzip, x-gzip, identity, *;q=0
HTTP_REFERER=http://192.168.16.201/
HTTP_CONNECTION=Keep-Alive, TE
HTTP_TE=deflate, gzip, chunked, identity, trailers
HTTP_CONTENT_LENGTH=43
HTTP_CONTENT_TYPE=application/x-www-form-urlencoded
SERVER_NAME=Myhost
GATEWAY_INTERFACE=CGI/1.1
SERVER_PROTOCOL=HTTP/1.1
REQUEST_METHOD=POST
SCRIPT_NAME=/cgi-bin/userI.cgi
SCRIPT_FILENAME=cgi-bin/userI.cgi
SERVER_SOFTWARE=UnisonHttpd
REQUEST_URI=cgi-bin/userI.cgi
CONTENT_TYPE=application/x-www-form-urlencoded
CONTENT_LENGTH=43
SERVER_PORT=1568
REMOTE_ADDR=192.168.18.1

fd socket ID, from which the handler will read data and where the handler will write the newly created page

In summary, the cgi_handler function inputs the parameters, processes the input arguments and creates a new page which is streamed back to the browser to update the user.

You can parse input parameters **envp by use function httpgetenv(“header name”, envp); and getparam(str, “field=”);

Example:

env = httpgetenv("REQUEST_METHOD", envp);
if (strcmp(env, "POST")==0) {                           //metod == "POST"
        size = atoi(httpgetenv("CONTENT_LENGTH", envp));    //getting received data size
        pos = recv(fd, str, size, 0);                           //receiving data from stream
        str[pos] = '\0';
}else { //metod == "GET"
        env = httpgetenv("QUERY_STRING", envp); //Reading query string sended by GET metod
        strcpy(str,env);
}
name = getparam(str,"name_field="); //get entered "user Name"
age = getparam(str,"age_field="); //get entered "user Age"

 

Note: These CGI handlers must be compiled into the executable as part of the image. This means that changes to the web pages create a need to update the CGI handlers if the updates are more than cosmetic, or in other words, new variables are added.

 

SPECIFIC OPTIONS (Compile time)

Option Default Description Values
TCPRIO 5 Priority for TCP thread Dec: 016
THTTPPRIO 8 Priority for HTTP thread Dec: 016
FSYSPRIO 9 Priority for FSYS thread Dec: 016
TCPSTACKSIZE 2560 Stack size for TCP thread Decimal
THTTPSTACKSIZE 3072 Stack size for HTTP thread Decimal
FSYSSTACKSIZE 2048 Stack size for FSYS thread Decimal

 

EXAMPLE

pthread_t tid;
pthread_attr_t  attr;
struct fsysinit  fsysinit;
struct sched_param myNewPriority;
int mkfs_status, mount_status;
int fd;

xprintf("Start Main\n");

/*FSYS Initialization*/
fsysinit.fsi_msgsize = sizeof(struct fsysinit);
fsysinit.fsi_nclones = 1;
fsysinit.fsi_datalog = 0x3;
fsysinit.fsi_nbuffers = 4; /* num of disk buffers (cache size) */
fsysinit.fsi_maxopen = 2; /* max num of open files */
fsysinit.fsi_blocksize = 256;

// set up stack size and priority for main fsys threads and all clones
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, FSYSSTACKSIZE);
myNewPriority.sched_priority = FSYSPRIO;
pthread_attr_setschedparam(&attr, &myNewPriority);

if(!pthread_create(&tid, &attr,(void*(*)(void *))&_file_server, (char*)&fsysinit))
{
        if(!dir_register(FSYS_DIRECTORY, tid, TYPE_SERVER))
        {
                xprintf("..Unable to register name of File Server\n..");
                xprintf("..Required resource not present - aborting..\n");

                pthread_exit((void*)1);
        }
}
else
{
        xprintf("..Unable to create File Server..\n");
        xprintf("..Required resource for program not present - aborting..\n");
        pthread_exit((void*)1);
}
xprintf("..File server is now created.\n..");
xprintf("\n..Attempting to mount it...\n");

if((mount(DEVICE_NO, FSYS_MOUNT, 0)) == -1) {
        xprintf("..Unable to mount flash disk. errno=%d\n", errno);
        xprintf("..Please fix me\n..");
        pthread_exit((void*)1);
}

xprintf("..done fsys initialization..\n");

xprintf("\n--- Prepare  files ---\n");
//preparing configuration file
fd = open(httpdconf_name, O_CREAT | O_RDWR, 0666);
write(fd, &httpdconf_str, strlen(httpdconf_str));
close(fd);

//preparing html and other files to work http server
fd = open(page_name1, O_CREAT | O_RDWR, 0666);		//put to disk index.html
write(fd, page_str1, strlen(page_str1));
close(fd);
fd = open(page_name2, O_CREAT | O_RDWR, 0666);		//put to disk page1.htm
write(fd, page_str2, strlen(page_str2));
close(fd);
fd = open(image_name, O_CREAT | O_RDWR, 0666);		//put to disk OK.gif
write(fd, image, sizeof(image));
close(fd);


mkdir(secret_dir_name,  0777);					//make directory with limited access
fd = open(page_name4, O_CREAT | O_RDWR, 0666);	//put to disk page with limited access
write(fd, page_str4, sizeof(page_str4));
close(fd);

set_loginfile((char *)htpasswd_name);    //set login and pass to access to files in this dir
init_login_table((char *)htpasswd_name);
add_user( "root", "root" );

/* make virtual link to user handler call address
 * http://xxx.xxx.xxx.xxx/cgi-bin/userI.cgi
 * will run UI_cgi_handler();
 */
register_cgi_handler("test", "cgi-bin/userI.cgi", UI_cgi_handler);

/* Start tcpd server*/
xprintf("\n--- Start tcpd ---\n");

myNewPriority.sched_priority = TCPRIO;
pthread_attr_setschedparam(&attr, &myNewPriority);
pthread_attr_setstacksize(&attr, TCPSTACKSIZE);
if(pthread_create(&tid, &attr, &tcp_shell, 0)!=0)
{
        xprintf("pthread_create = %d\n", errno);
        pthread_exit(0);
}

if(dir_register("/dev/tcpd", tid, TYPE_SERVER)==0)
{
        xprintf("dir_register = %d\n", errno);
        pthread_exit(0);
}

/* Start THTTP server */
xprintf("\n--- Start HTTP server ---\n");
// set up stack size and priority for main THTTP thread
pthread_attr_setstacksize(&attr, THTTPSTACKSIZE);
myNewPriority.sched_priority = THTTPPRIO;
pthread_attr_setschedparam(&attr, &myNewPriority);

if(pthread_create(&tid, &attr,(void*(*)(void *))&thttp_shell, NULL)!=0)
{
        xprintf("..Unable to create THTTP Server..\n");
        pthread_exit((void*)1);
}

...

THREAD tcp_shell(void * args)
{
        tcpd(&etherinit);
        return (THREAD)0;
}


THREAD thttp_shell(void * args)
{
        thttpd_main((char *)httpdconf_name);
        return (THREAD)0;       
}

 

NOTES

There is demo available for the Unison and DSPnano thttpd which are found in installdir/demos which provide examples of using the server with simple page, password protected and CGI generated pages.

SEE ALSO

ppp server FatFS server login

Suggest Edit

CONTACT US

TO GET YOUR PROJECT STARTED

TOP