⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpserv.c

📁 Internet Radio Internet Radio Internet Radio Internet Radio Internet Radio
💻 C
📖 第 1 页 / 共 2 页
字号:
    char *ip = NULL;
    char *mask = NULL;
    char *gateway = NULL;
    char *dns0 = NULL;
    char *dns1 = NULL;
    char *func = NULL;
    u_long addr;
    int i;
    int upd_os = 0;
    int upd_net = 0;
    int upd_boot = 0;
    int upd_radio = 0;

    count = NutHttpGetParameterCount(req);
    for (i = 0; i < count; i++) {
        name = NutHttpGetParameterName(req, i);
        if (strcmp(name, "value") == 0) {
            value = NutHttpGetParameterValue(req, i);
            break;
        } else if (strcmp(name, "hostname") == 0) {
            hostname = NutHttpGetParameterValue(req, i);
        } else if (strcmp(name, "proxy") == 0) {
            prox = NutHttpGetParameterValue(req, i);
        } else if (strcmp(name, "dhcp") == 0) {
            dhcp = NutHttpGetParameterValue(req, i);
        } else if (strcmp(name, "ip") == 0) {
            ip = NutHttpGetParameterValue(req, i);
        } else if (strcmp(name, "mask") == 0) {
            mask = NutHttpGetParameterValue(req, i);
        } else if (strcmp(name, "gateway") == 0) {
            gateway = NutHttpGetParameterValue(req, i);
        } else if (strcmp(name, "dns0") == 0) {
            dns0 = NutHttpGetParameterValue(req, i);
        } else if (strcmp(name, "dns1") == 0) {
            dns1 = NutHttpGetParameterValue(req, i);
        } else if (strcmp(name, "func") == 0) {
            func = NutHttpGetParameterValue(req, i);
        }
    }
    if (func) {
        if (strcmp(func, "Apply") == 0) {
            if (hostname) {
                strncpy(confos.hostname, hostname, sizeof(confos.hostname) - 1);
                upd_os = 1;
            }
            if (prox) {
                char *cp = prox;

                while (*cp && *cp <= ' ') {
                    cp++;
                }
                if (strlen(cp)) {
                    strncpy(proxy.proxy_host, cp, sizeof(proxy.proxy_host) - 1);
                    if ((cp = strrchr(proxy.proxy_host, ':')) != NULL) {
                        *cp++ = 0;
                        proxy.proxy_port = atoi(cp);
                    }
                }
                else {
                    memset(&proxy, 0, sizeof(proxy));
                }
                upd_radio = 1;
            }
            if (dhcp && strcmp(dhcp, "on") == 0) {
                confnet.cdn_cip_addr = 0;
                upd_net = 1;
                upd_boot = 1;
            }
            else {
                if (ip) {
                    addr = inet_addr(ip);
                    if ((long)addr != -1) {
                        confnet.cdn_cip_addr = addr;
                        upd_net = 1;
                        upd_boot = 1;
                    }
                }
                if (mask) {
                    addr = inet_addr(mask);
                    if ((long)addr != -1) {
                        confnet.cdn_ip_mask = addr;
                        upd_net = 1;
                        upd_boot = 1;
                    }
                }
                if (gateway) {
                    addr = inet_addr(gateway);
                    if ((long)addr != -1) {
                        confnet.cdn_gateway = addr;
                        upd_net = 1;
                        upd_boot = 1;
                    }
                }
                if (dns0) {
                    addr = inet_addr(dns0);
                    if ((long)addr != -1) {
                        webradio.wr_pridns = addr;
                        upd_radio = 1;
                        upd_boot = 1;
                    }
                }
                if (dns1) {
                    addr = inet_addr(dns1);
                    if ((long)addr != -1) {
                        webradio.wr_secdns = addr;
                        upd_radio = 1;
                        upd_boot = 1;
                    }
                }
            }
            if (upd_os) {
                LogMsg(LOG_HTTPD, "Saving OS\n");
                NutSaveConfig();
            }
            if (upd_net) {
                LogMsg(LOG_HTTPD, "Saving Net\n");
                NutNetSaveConfig();
            }
            if (upd_radio) {
                ConfigSave();
            }
            if (upd_boot) {
                LogMsg(LOG_HTTPD, "Reboot\n");
                webradio.wr_reboot = 3;
            }
        } 
        else if (strcmp(func, "Erase") == 0) {
            u_char mac[6];

            memset(&confos, 0, sizeof(confos));
            NutSaveConfig();
            memcpy(mac, confnet.cdn_mac, sizeof(mac));
            memset(&confnet, 0, sizeof(confnet));
            memcpy(confnet.cdn_mac, mac, sizeof(confnet.cdn_mac));
            NutNetSaveConfig();
            ConfigResetFactory();
            FavListResetFactory();
            ConfigSave();
            FavListSave();
            webradio.wr_reboot = 2;
        }
    }
    else if (value) {
        if (strcmp(value, "hostname") == 0) {
            fprintf(stream, "%s", confos.hostname);
        } else if (strcmp(value, "dhcp") == 0) {
            if (confnet.cdn_cip_addr == 0) {
                fputs(" checked=\"checked\"", stream);
            }
        } else if (strcmp(value, "proxy") == 0) {
            if (proxy.proxy_port) {
                fputs(proxy.proxy_host, stream);
                if (proxy.proxy_port != 80) {
                    fprintf(stream, ":%u", proxy.proxy_port);
                }
            }
        } else if (strcmp(value, "ip") == 0) {
            if (confnet.cdn_cip_addr) {
                fputs(inet_ntoa(confnet.cdn_cip_addr), stream);
            } else {
                fputs(inet_ntoa(confnet.cdn_ip_addr), stream);
            }
        } else if (strcmp(value, "mask") == 0) {
            fputs(inet_ntoa(confnet.cdn_ip_mask), stream);
        } else if (strcmp(value, "gateway") == 0) {
            fputs(inet_ntoa(confnet.cdn_gateway), stream);
        } else if (strcmp(value, "dns0") == 0) {
            NutDnsGetConfig2(NULL, NULL, &addr, NULL);
            fputs(inet_ntoa(addr), stream);
        } else if (strcmp(value, "dns1") == 0) {
            NutDnsGetConfig2(NULL, NULL, NULL, &addr);
            fputs(inet_ntoa(addr), stream);
        }
    }
    return 0;
}

static int CgiVars(FILE * stream, REQUEST * req)
{
    int count;
    char *name;
    char *cgivar = NULL;
    int i;

    count = NutHttpGetParameterCount(req);
    for (i = 0; i < count; i++) {
        name = NutHttpGetParameterName(req, i);
        if (strcmp(name, "var") == 0) {
            cgivar = NutHttpGetParameterValue(req, i);
            break;
        }
    }

    if (cgivar == NULL) {
        fputs("Error", stream);
    } else if (strcmp(cgivar, "MetaTitle") == 0 || strcmp(cgivar, "StreamTitle") == 0) {
        if (webradio.wr_rip) {
            SHOUTCASTINFO *sci = (SHOUTCASTINFO *) webradio.wr_rip->ri_bcast;
            if (sci && sci->sci_metatitle) {
                fputs(sci->sci_metatitle, stream);
            }
        }
    } else if (strcmp(cgivar, "CPUClock") == 0) {
        fprintf(stream, "%lu kHz", NutGetCpuClock() / 1000);
    } else if (strcmp(cgivar, "MasterClock") == 0) {
#if defined(AT91_PLL_MAINCK)
        fprintf(stream, "%lu kHz", At91GetMasterClock() / 1000);
#endif
    } else if (strcmp(cgivar, "TotalMemoryUsed") == 0) {
        fprintf(stream, "%u kBytes", (u_int) (NUTMEM_SIZE - NutHeapAvailable()) / 1024);
    } else if (strcmp(cgivar, "StationName") == 0) {
        if (webradio.wr_sip) {
            fputs(webradio.wr_sip->si_name, stream);
        }
    } else if (strcmp(cgivar, "StationGenre") == 0) {
        if (webradio.wr_sip && webradio.wr_sip->si_genre) {
            fputs(webradio.wr_sip->si_genre, stream);
        }
    } else if (strcmp(cgivar, "StationBitrate") == 0) {
        if (webradio.wr_sip) {
            fprintf(stream, "%u kBit", webradio.wr_sip->si_bitrate);
        }
    } else if (strcmp(cgivar, "BufferContents") == 0) {
        fprintf(stream, "%lu Bytes", NutSegBufUsed());
    } else if (strcmp(cgivar, "StreamURL") == 0) {
        if (webradio.wr_rip) {
            SHOUTCASTINFO *sci = (SHOUTCASTINFO *) webradio.wr_rip->ri_bcast;
            if (sci->sci_metaurl) {
                fprintf(stream, "%s", sci->sci_metaurl);
            }
        }
    } else if (strcmp(cgivar, "MetadataInterval") == 0) {
        if (webradio.wr_rip) {
            SHOUTCASTINFO *sci = (SHOUTCASTINFO *) webradio.wr_rip->ri_bcast;
            fprintf(stream, "%lu Bytes", sci->sci_metaint);
        }
    } else if (strcmp(cgivar, "NutVersion") == 0) {
        fputs(NutVersionString(), stream);
    } else if (strcmp(cgivar, "AppVersion") == 0) {
        fputs(VERSION, stream);
    } else {
        fputs("Unknown", stream);
    }
    fflush(stream);
    return 0;
}

/*! \fn Service(void *arg)
 * \brief HTTP service thread.
 *
 * The endless loop in this thread waits for a client connect,
 * processes the HTTP request and disconnects. Nut/Net doesn't
 * support a server backlog. If one client has established a
 * connection, further connect attempts will be rejected.
 * Typically browsers open more than one connection in order
 * to load images concurrently. So we run this routine by
 * several threads.
 *
 */
THREAD(Service, arg)
{
    TCPSOCKET *sock;
    FILE *stream;

    LogMsg(LOG_HTTPD, "Started HTTP daemon\n");
    for (;;) {
        /* Create a socket. */
        if ((sock = NutTcpCreateSocket()) == 0) {
            LogMsg(LOG_WARN, "No sockets\n");
            NutSleep(1000);
            continue;
        }

        /* Set socket options. Silently ignore any error. */
#ifdef HTTPD_MAX_SEGSIZE
        {
            u_short mss = HTTPD_MAX_SEGSIZE;
            NutTcpSetSockOpt(sock, TCP_MAXSEG, &mss, sizeof(mss));
        }
#endif
#ifdef HTTPD_TCP_BUFSIZE
        {
            u_short tcpbufsiz = HTTPD_TCP_BUFSIZE;
            NutTcpSetSockOpt(sock, SO_RCVBUF, &tcpbufsiz, sizeof(tcpbufsiz));
        }
#endif
#ifdef HTTPD_TCP_TIMEOUT
        {
            u_long tmo = HTTPD_TCP_TIMEOUT;
            NutTcpSetSockOpt(sock, SO_RCVTIMEO, &tmo, sizeof(tmo));
        }
#endif

        LogMsg(LOG_HTTPD, "Waiting for HTTP client\n");
        stream = TcpStreamAccept(sock, HTTPD_TCP_PORT, "r+b");
        if (stream) {
            LogMsg(LOG_HTTPD, "Connected HTTP client\n");
#ifdef USE_DYNAMIC_THREADS
            /* Start a new thread and let it run. */
            StartServiceThread();
            NutSleep(1);
#endif
            /* Let the Nut/OS library process the client's requests. */
            NutHttpProcessRequest(stream);
            LogMsg(LOG_HTTPD, "Disconnecting HTTP client\n");
            fclose(stream);
        }
        NutTcpCloseSocket(sock);

#ifdef USE_DYNAMIC_THREADS
        /* If enough threads are running, stop this one. */
        if (httpd_tc >= HTTPD_MIN_THREADS) {
            httpd_tc--;
            LogMsg(LOG_HTTPD, "Stop HTTP daemon\n");
            NutThreadExit();
        }
#endif
    }
}

/*
 * Start a HTTP daemon thread.
 */
static int StartServiceThread(void)
{
#ifdef USE_DYNAMIC_THREADS
    if (httpd_tc >= HTTPD_MAX_THREADS) {
        return 0;
    }
#endif

    if (NutThreadCreate("httpd", Service, NULL, HTTPD_SERVICE_STACK) == NULL) {
        return -1;
    }
    httpd_tc++;

    return 0;
}

/*!
 * \brief HTTP Server Start.
 */
int HttpServerStart(void)
{
    int i;

    /*
     * Register our device for the file system.
     */
    if (NutRegisterDevice(&MY_FSDEV, 0, 0)) {
        LogMsg(LOG_ERROR, "No HTTP mount\n");
        return -1;
    }

#ifdef MY_HTTPROOT
    /* Register root directory. */
    LogMsg(LOG_HTTPD, "Register root dir '" MY_HTTPROOT "'\n");
    if (NutRegisterHttpRoot(MY_HTTPROOT)) {
        LogMsg(LOG_ERROR, "No HTTP root\n");
        return -1;
    }
#endif
    /*
     * Register SSI handler
     */
    NutRegisterSsi();

    /*
     * Register our CGIs.
     */
    NutRegisterCgi("f_control.cgi", CgiFavoritesControl);
    NutRegisterCgi("favorites.cgi", CgiStationFavorites);
    NutRegisterCgi("genres.cgi", CgiShoutCastGenres);
    NutRegisterCgi("stations.cgi", CgiShoutCastStations);
    NutRegisterCgi("sc_control.cgi", CgiShoutCastControl);
    NutRegisterCgi("vars.cgi", CgiVars);
    NutRegisterCgi("settings.cgi", CgiSettings);

    /*
     * Protect the admin directory with user and password.
     */
    NutRegisterAuth("admin", "admin:admin");

    /*
     * Start four server threads.
     */
    LogMsg(LOG_HTTPD, "Starting %d HTTP daemons\n", HTTPD_MIN_THREADS);
    for (i = 0; i < HTTPD_MIN_THREADS; i++) {
        if (StartServiceThread()) {
            LogMsg(LOG_ERROR, "Thread start failed\n");
            return -1;
        }
    }

    /* Let started threads take over. */
    NutSleep(1);

    return 0;
}

#endif /* USE_HTTPSERVER */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -