owntone-server/src/main.c

362 lines
8.8 KiB
C
Raw Normal View History

2003-10-13 11:03:14 -04:00
/*
* $Id$
* Driver for multi-threaded daap server
*
* Copyright (C) 2003 Ron Pedde (ron@corbey.com)
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
2003-11-03 14:02:00 -05:00
#include <errno.h>
#include <fcntl.h>
2003-11-03 14:02:00 -05:00
#include <limits.h>
2003-10-13 11:03:14 -04:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2003-10-13 11:03:14 -04:00
#include <pthread.h>
2003-11-03 14:02:00 -05:00
#include <unistd.h>
2003-10-13 11:03:14 -04:00
#include <sys/stat.h>
#include <sys/types.h>
#include "configfile.h"
2003-11-04 01:11:00 -05:00
#include "db-memory.h"
2003-10-30 17:42:11 -05:00
#include "daap.h"
#include "daap-proto.h"
2003-10-13 11:03:14 -04:00
#include "err.h"
2003-11-04 01:11:00 -05:00
#include "mp3-scanner.h"
2003-10-23 17:43:01 -04:00
#include "rend.h"
2003-10-13 11:03:14 -04:00
#include "webserver.h"
// 3689
/*
* Globals
*/
CONFIG config;
2003-10-30 17:42:11 -05:00
/*
* daap_handler
*
* Handle daap-related web pages
*/
void daap_handler(WS_CONNINFO *pwsc) {
int len;
int close;
DAAP_BLOCK *root,*error;
int compress=0;
2003-11-04 01:11:00 -05:00
int clientrev;
2003-10-30 17:42:11 -05:00
2003-11-11 21:59:45 -05:00
/* for the /databases URI */
char *uri;
int db_index;
int playlist_index;
int item;
char *first, *last;
int streaming=0;
MP3FILE *pmp3;
int file_fd;
2003-11-13 23:56:20 -05:00
int session_id=0;
2003-11-11 21:59:45 -05:00
2003-10-30 17:42:11 -05:00
close=pwsc->close;
2003-11-11 21:59:45 -05:00
pwsc->close=1; /* in case we have any errors */
root=NULL;
2003-10-30 17:42:11 -05:00
ws_addresponseheader(pwsc,"Accept-Ranges","bytes");
ws_addresponseheader(pwsc,"DAAP-Server","iTunes/4.1 (Mac OS X)");
ws_addresponseheader(pwsc,"Content-Type","application/x-dmap-tagged");
2003-11-13 23:56:20 -05:00
if(ws_getvar(pwsc,"session-id")) {
session_id=atoi(ws_getvar(pwsc,"session-id"));
}
2003-10-30 17:42:11 -05:00
if(!strcasecmp(pwsc->uri,"/server-info")) {
2003-11-13 23:56:20 -05:00
config_set_status(pwsc,session_id,"Sending server info");
2003-11-17 11:38:44 -05:00
root=daap_response_server_info(config.servername);
2003-10-30 17:42:11 -05:00
} else if (!strcasecmp(pwsc->uri,"/content-codes")) {
2003-11-13 23:56:20 -05:00
config_set_status(pwsc,session_id,"Sending content codes");
2003-10-30 17:42:11 -05:00
root=daap_response_content_codes();
} else if (!strcasecmp(pwsc->uri,"/login")) {
2003-11-13 23:56:20 -05:00
config_set_status(pwsc,session_id,"Logging in");
2003-10-30 17:42:11 -05:00
root=daap_response_login();
} else if (!strcasecmp(pwsc->uri,"/update")) {
if(!ws_getvar(pwsc,"delta")) { /* first check */
2003-11-04 01:11:00 -05:00
clientrev=db_version() - 1;
2003-11-13 23:56:20 -05:00
config_set_status(pwsc,session_id,"Sending database");
2003-11-04 01:11:00 -05:00
} else {
clientrev=atoi(ws_getvar(pwsc,"delta"));
2003-11-13 23:56:20 -05:00
config_set_status(pwsc,session_id,"Waiting for DB updates");
2003-11-04 01:11:00 -05:00
}
root=daap_response_update(clientrev);
2003-10-30 17:42:11 -05:00
} else if (!strcasecmp(pwsc->uri,"/logout")) {
2003-11-13 23:56:20 -05:00
config_set_status(pwsc,session_id,NULL);
2003-10-30 17:42:11 -05:00
ws_returnerror(pwsc,204,"Logout Successful");
return;
2003-11-11 21:59:45 -05:00
} else if(strcmp(pwsc->uri,"/databases")==0) {
2003-11-13 23:56:20 -05:00
config_set_status(pwsc,session_id,"Sending database info");
2003-11-17 11:38:44 -05:00
root=daap_response_dbinfo(config.servername);
2003-11-11 21:59:45 -05:00
} else if(strncmp(pwsc->uri,"/databases/",11) == 0) {
/* the /databases/ uri will either be:
*
* /databases/id/items, which returns items in a db
* /databases/id/containers, which returns a container
* /databases/id/containers/id/items, which returns playlist elements
* /databases/id/items/id.mp3, to spool an mp3
*/
uri = strdup(pwsc->uri);
first=(char*)&uri[11];
last=first;
while((*last) && (*last != '/')) {
last++;
}
if(*last) {
*last='\0';
db_index=atoi(first);
last++;
if(strncasecmp(last,"items/",6)==0) {
/* streaming */
first=last+6;
while((*last) && (*last != '.'))
last++;
if(*last == '.') {
*last='\0';
item=atoi(first);
streaming=1;
}
free(uri);
} else if (strncasecmp(last,"items",5)==0) {
/* songlist */
free(uri);
root=daap_response_songlist();
2003-11-13 23:56:20 -05:00
config_set_status(pwsc,session_id,"Sending songlist");
2003-11-11 21:59:45 -05:00
} else if (strncasecmp(last,"containers/",11)==0) {
/* playlist elements */
first=last + 11;
last=first;
while((*last) && (*last != '/')) {
last++;
}
if(*last) {
*last='\0';
playlist_index=atoi(first);
root=daap_response_playlist_items(playlist_index);
}
free(uri);
2003-11-13 23:56:20 -05:00
config_set_status(pwsc,session_id,"Sending playlist info");
2003-11-11 21:59:45 -05:00
} else if (strncasecmp(last,"containers",10)==0) {
/* list of playlists */
free(uri);
2003-11-17 11:38:44 -05:00
root=daap_response_playlists(config.servername);
2003-11-13 23:56:20 -05:00
config_set_status(pwsc,session_id,"Sending playlist info");
2003-11-11 21:59:45 -05:00
}
}
2003-10-30 17:42:11 -05:00
}
2003-11-11 21:59:45 -05:00
if((!root)&&(!streaming)) {
2003-11-14 14:19:18 -05:00
DPRINTF(ERR_DEBUG,"Bad request -- root=%x, streaming=%d\n",root,streaming);
2003-11-03 15:34:18 -05:00
ws_returnerror(pwsc,400,"Invalid Request");
2003-11-13 23:56:20 -05:00
config_set_status(pwsc,session_id,NULL);
2003-10-30 17:42:11 -05:00
return;
2003-11-03 15:34:18 -05:00
}
2003-10-30 17:42:11 -05:00
pwsc->close=close;
2003-11-11 21:59:45 -05:00
if(!streaming) {
ws_addresponseheader(pwsc,"Content-Length","%d",root->reported_size + 8);
ws_writefd(pwsc,"HTTP/1.1 200 OK\r\n");
ws_emitheaders(pwsc);
2003-10-30 17:42:11 -05:00
2003-11-11 21:59:45 -05:00
/*
if(ws_testrequestheader(pwsc,"Accept-Encoding","gzip")) {
ws_addresponseheader(pwsc,"Content-Encoding","gzip");
compress=1;
}
*/
2003-10-30 17:42:11 -05:00
2003-11-11 21:59:45 -05:00
daap_serialize(root,pwsc->fd,0);
} else {
/* stream out the song */
pwsc->close=1;
2003-10-30 17:42:11 -05:00
2003-11-11 21:59:45 -05:00
pmp3=db_find(item);
if(!pmp3) {
ws_returnerror(pwsc,404,"File Not Found");
} else {
/* got the file, let's open and serve it */
file_fd=r_open2(pmp3->path,O_RDONLY);
if(file_fd == -1) {
pwsc->error=errno;
DPRINTF(ERR_WARN,"Thread %d: Error opening %s: %s\n",
pwsc->threadno,pmp3->path,strerror(errno));
ws_returnerror(pwsc,404,"Not found");
2003-11-13 23:56:20 -05:00
config_set_status(pwsc,session_id,NULL);
2003-11-11 21:59:45 -05:00
} else {
ws_writefd(pwsc,"HTTP/1.1 200 OK\r\n");
ws_addresponseheader(pwsc,"Connection","Close");
ws_emitheaders(pwsc);
2003-11-13 23:56:20 -05:00
config_set_status(pwsc,session_id,"Streaming file '%s'",pmp3->fname);
2003-11-11 21:59:45 -05:00
copyfile(file_fd,pwsc->fd);
2003-11-13 23:56:20 -05:00
config_set_status(pwsc,session_id,NULL);
2003-11-11 21:59:45 -05:00
r_close(file_fd);
}
}
}
2003-10-30 17:42:11 -05:00
DPRINTF(ERR_DEBUG,"Finished serving DAAP response\n");
2003-10-30 17:42:11 -05:00
return;
}
/*
* config_handler
*
* Handle config web pages
*/
void usage(char *program) {
printf("Usage: %s [options]\n\n",program);
printf("Options:\n");
#ifdef DEBUG
printf(" -d <number> Debuglevel (0-9)\n");
#endif
2003-10-30 17:42:11 -05:00
printf(" -m Use mDNS\n");
printf(" -c <file> Use configfile specified");
printf("\n\n");
}
2003-10-13 11:03:14 -04:00
int main(int argc, char *argv[]) {
int option;
char *configfile=NULL;
2003-10-13 11:03:14 -04:00
WSCONFIG ws_config;
WSHANDLE server;
2003-10-23 17:43:01 -04:00
pid_t rendezvous_pid;
int use_mdns=0;
2003-11-04 01:11:00 -05:00
ENUMHANDLE handle;
MP3FILE *pmp3;
2003-10-23 17:43:01 -04:00
#ifdef DEBUG
2003-10-30 17:42:11 -05:00
char *optval="d:c:m";
#else
2003-10-30 17:42:11 -05:00
char *optval="c:m";
#endif /* DEBUG */
2003-10-13 11:03:14 -04:00
printf("mt-daapd: version $Revision$\n");
printf("Copyright (c) 2003 Ron Pedde. All rights reserved\n");
printf("Portions Copyright (c) 1999-2001 Apple Computer, Inc. All rights Reserved.\n\n");
while((option=getopt(argc,argv,optval)) != -1) {
switch(option) {
#ifdef DEBUG
case 'd':
err_debuglevel=atoi(optarg);
break;
#endif
case 'c':
configfile=optarg;
break;
2003-10-30 17:42:11 -05:00
case 'm':
use_mdns=1;
break;
default:
usage(argv[0]);
exit(EXIT_FAILURE);
break;
}
}
/* read the configfile, if specified, otherwise
* try defaults */
if(!configfile) {
if(config_read("/etc/mt-daapd.conf"))
if(config_read("./mt-daapd.conf")) {
perror("configfile_read");
exit(EXIT_FAILURE);
}
} else {
if(config_read(configfile)) {
perror("config_read");
exit(EXIT_FAILURE);
}
}
DPRINTF(ERR_DEBUG,"Initializing database\n");
2003-11-04 01:11:00 -05:00
/* Initialize the database before starting */
if(db_init("none")) {
perror("db_init");
exit(EXIT_FAILURE);
}
2003-11-09 23:00:13 -05:00
printf("Scanning MP3s\n");
2003-11-04 01:11:00 -05:00
if(scan_init(config.mp3dir)) {
perror("scan_init");
exit(EXIT_FAILURE);
}
2003-11-09 23:00:13 -05:00
printf("Done... starting server\n");
/* start up the web server */
ws_config.web_root=config.web_root;
ws_config.port=config.port;
2003-10-13 11:03:14 -04:00
server=ws_start(&ws_config);
if(!server) {
perror("ws_start");
return EXIT_FAILURE;
}
2003-10-30 17:42:11 -05:00
ws_registerhandler(server, "^.*$",config_handler,config_auth,1);
ws_registerhandler(server, "^/server-info$",daap_handler,NULL,0);
ws_registerhandler(server, "^/content-codes$",daap_handler,NULL,0);
ws_registerhandler(server,"^/login$",daap_handler,NULL,0);
ws_registerhandler(server,"^/update$",daap_handler,NULL,0);
ws_registerhandler(server,"^/databases$",daap_handler,NULL,0);
ws_registerhandler(server,"^/logout$",daap_handler,NULL,0);
2003-11-03 15:34:18 -05:00
ws_registerhandler(server,"^/databases/.*",daap_handler,NULL,0);
2003-10-30 17:42:11 -05:00
if(use_mdns)
2003-11-17 11:38:44 -05:00
rend_init(&rendezvous_pid,config.servername, config.port);
2003-10-23 17:43:01 -04:00
2003-10-13 11:03:14 -04:00
while(1) {
2003-11-23 01:10:25 -05:00
sleep(100);
2003-10-13 11:03:14 -04:00
}
2003-11-23 01:10:25 -05:00
#ifdef DEBUG
err_leakcheck();
#endif
2003-10-13 11:03:14 -04:00
return EXIT_SUCCESS;
}