/*
webchat, an HTML5/websocket chat platform
Copyright (C) 2015
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, version 3 of the License.
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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
// A workaround for MediaSource not being widely supported yet
#include
#include
#include
#include
#include
#include
#include
#include "users.h"
#include "httpcam.h"
struct httpcamkey
{
struct user* user;
const char* key;
};
struct httpcamkey* httpcam_keys=0;
unsigned int httpcam_keycount=0;
struct user* httpcam_cb_cam=0;
char httpcam_cb(const char* path, const char* host)
{
httpcam_cb_cam=0;
// if(!host || strcmp(host, HOSTNAME)){return 0;}
if(path && !strncmp(path, "/cam/", 5))
{
unsigned int i;
for(i=0; ihttpcam=1;
#ifdef use_tls
gnutls_record_send(user->socket, "HTTP/1.1 200 Ok\r\nContent-type: video/webm\r\nTransfer-encoding: chunked\r\n\r\n", 73);
#else
write(*user->socket, "HTTP/1.1 200 Ok\r\nContent-type: video/webm\r\nTransfer-encoding: chunked\r\n\r\n", 73);
#endif
user_addrelation(httpcam_cb_cam, user, relation_media);
httpcam_cb_cam=0;
// TODO: is it safe to assume that httpcam users only have one relation?
if(user->relations[0].user->firstmedia)
{
httpcam_sendchunk(user->socket, user->relations[0].user->firstmedia, user->relations[0].user->firstmedialength);
}
}
const char* httpcam_getkey(struct user* user)
{
// Create a key to match this user
char* key=malloc(17);
int f=open("/dev/urandom", O_RDONLY);
unsigned int i=0;
char buf;
while(i<16)
{
read(f, &buf, 1);
if(isalnum(buf))
{
key[i]=buf;
++i;
}
}
close(f);
key[16]=0;
++httpcam_keycount;
httpcam_keys=realloc(httpcam_keys, sizeof(struct httpcamkey)*httpcam_keycount);
httpcam_keys[httpcam_keycount-1].user=user;
httpcam_keys[httpcam_keycount-1].key=key;
return key;
}
#ifdef use_tls
void httpcam_sendchunk(gnutls_session_t socket, void* data, unsigned int size)
{
char buf[snprintf(0,0,"%x\r\n", size)+1];
sprintf(buf, "%x\r\n", size);
gnutls_record_send(socket, buf, strlen(buf));
int w;
while(size>0 && (w=gnutls_record_send(socket, data, size))>0)
{
data+=w;
size-=w;
}
gnutls_record_send(socket, "\r\n", 2);
}
#else
void httpcam_sendchunk(int* socket, void* data, unsigned int size)
{
dprintf(*socket, "%x\r\n", size);
int w;
while(size>0 && (w=write(*socket, data, size))>0)
{
data+=w;
size-=w;
}
dprintf(*socket, "\r\n", 2);
}
#endif