GoAhead2.5移植到ARM教程

1、下载GoAhead2.5

  下载地址:https://github.com/embedthis/goahead/releases?after=v3.1.2

2、编译

  先解压到虚拟机的/opt目录下,cd /opt/goahead-2.5.0/LINUX,修改Makefile文件,在文件的最前面添加如下代码:

  CROSS_COMPILE =/opt/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-
  AS = $(CROSS_COMPILE)as
  LD = $(CROSS_COMPILE)ld
  CC = $(CROSS_COMPILE)gcc
  CPP = $(CC) -E
  AR = $(CROSS_COMPILE)ar
  NM = $(CROSS_COMPILE)nm
  STRIP = $(CROSS_COMPILE)strip
  OBJCOPY = $(CROSS_COMPILE)objcopy
  OBJDUMP = $(CROSS_COMPILE)objdump

  修改输出库名称:

  ARCH = libwebs_arm_linux_gnueabihf.a

  注释掉SSL功能,并保存

  #matrixsslDir:=$(shell ls -d ../matrixssl-3-1*/)

  make

  这时会在LINUX目录下生成libwebs_arm_linux_gnueabihf.a库,/opt/goahead-2.5.0目录下所有头文件(wsInitn.h、websSSL.h、websda.h、webs.h、um.h、uemf.h、md5.h、matrixSSLSocket.h、emfdb.h、ejIntrn.h、ej.h)和libwebs_arm_linux_gnueabihf.a库可以用于开发webserver程序,/opt/goahead-2.5.0/LINUX目录下的main.c文件中webserver启动过程可以用于自己开发webserver程序的一部分,以Qt开发为例,可以单独创建一个线程QWebServerThread。

头文件qwebserverthread.h

class QWebServerThread : public QThread

{

  Q_OBJECT

public:

  QWebServerThread(QString strWeb,QString strIP,int nPort,int nRPort);

  ~QWebServerThread ();

  int initWeb();

  static int websHomePageHandler(webs_t wp, char_t *urlPrefix, char_t *webDir,int arg, char_t *url, char_t *path, char_t *query);

#ifdef B_STATS
  void memLeaks();
  void printMemStats(int handle, char_t *fmt, ...);
#endif

protected:

  void run();

private:

  QString strWeb;

  QString strIP;

  int nPort;

  int nRPort;

}

cpp文件qwebserverthread.cpp

#include "qwebserverthread.h"

WebServerThread::QWebServerThread(QString strWeb,QString strIP,int nPort)

{

  this->strWeb = WebRoot;

  this->strIP = strIP;

  this->nPort = nPort;

  this->nRPort = nRPort;

}

QWebServerThread::~QWebServerThread()

{

}

void QWebServerThread::run()

{ 

  /*
  *首先分配一个大的内存块(60*1024字节),以后只要是以b开头的对内存操作的函数都是在这个已经分好的内存块上的操作,这些操作在Balloc.c中实现。
  */
  bopen(NULL,(60 * 1024),B_USE_MALLOC);

  /*
  *忽略SIGPIPE信号
  */
  signal(SIGPIPE,SIG_IGN);

  /*
  *Initialize the web server
  *初始化用户管理部分,打开web服务器,注册URL处理函数。
  *用户管理部分在um.c中实现,
  *Web服务器的初始化是在default.c和webs.c中实现
  *url处理函数在handler.c中实现
  */
  if(initWeb() < 0)
  {
    printf("initWeb error. ");
    return;
  }

  /*
  *初始化Ssl认证部分
  *注:在这个文档中对ssl认证不做研究
  */
  #ifdef WEBS_SSL_SUPPORT
  websSSLOpen();
  #endif

  /*
  * Basic event loop. SocketReady returns true when a socket is ready for
  * service. SocketSelect will block until an event occurs. SocketProcess
  * will actually do the servicing.
  */
  /*
  *主循环
  */
  while(getRunState())
  {
    /*
    1,socketReady()函数检查是否有准备好的sock事件
    2,socketSelect()函数首先把各个sock感兴趣的事件(sp->handlerMask)注册给三个集合(读,写,例外),然后调用select系统调用,然后更新各个sock的sp->currentEvents,表示各个sock的当前状态。
    这两个函数在sockGen.c中实现,他们主要操作的数据是socket_t变量socketList中的handlerMask和currentEvents,socketList在sock.c中定义并主要由该文件中的socketAlloc,socketFree和socketPtr三个函数维护。
    */
    if(socketReady(-1) || socketSelect(-1,1000))
    {
      /*
      该函数处理具体的sock事件
      1,调用socketReady(sid)对socketList[sid]进行检查,看是否有sock事件
      2,如果有sock事件,则调用socketDoEvent()函数,对事件进行处理
      */
      socketProcess(-1);
    }
    /*
    该函数在cgi.c中实现,检查cgiRec变量cgilist,首先把cgi的结果输出,如果有的话,然后看cgi进程是否已对号束,如果结束,就清理该cgi进程。
    Cgilist在函数websCgiHandler和websCgiCleanup中维护。
    */
    websCgiCleanup();
    /*
    该函数在websuemf.c中实现,功能是检查sched_t变量sched,断开超时的连接,sched变量在emfSchedCallback和emfUnschedCallback中维护
    */
    emfSchedProcess();
  }

  /*
  退出时的清理工作,永远不会执行到这里
  */
#ifdef WEBS_SSL_SUPPORT
  websSSLClose();
#endif

#ifdef USER_MANAGEMENT_SUPPORT
  umClose();
#endif

  //Close the socket module, report memory leaks and close the memory allocator
  websCloseServer();
  socketClose();
#ifdef B_STATS
  memLeaks();
#endif
  bclose();

}

int QWebServerThread::initWeb()
{

  //Initialize the socket subsystem
  socketOpen();

#ifdef USER_MANAGEMENT_SUPPORT
  /*
  * Initialize the User Management database
  */
  umOpen();
  umRestore((char_t*)_T("umconfig.txt"));
#endif

  /*
  * Define the local Ip address, host name, default home page and the
  * root web directory.
  */

  struct in_addr intaddr;

  intaddr.s_addr = inet_addr(strIP .toUtf8().data()); 

  /*
  * Set ../www as the root web
  */

  char *cp;

  char* dir = NULL;

  getcwd(dir, sizeof(dir));
  if ((cp = strrchr(dir, '/')))

   {
    *cp = '';
  }

  char* WebDir = NULL;
  sprintf(WebDir, "%s/%s", dir, strWeb.toUtf8().data());

  /*
  * Configure the web server options before opening the web server
  */
  websSetDefaultDir(pWebDir);

  SAVE_DELETE_ARRAY(dir);
  SAVE_DELETE_ARRAY(WebDir);

  char_t wbuf[128] = {0};  

  char *cp = inet_ntoa(intaddr);
  ascToUni(wbuf, cp, min(strlen(cp) + 1, sizeof(wbuf)));
  websSetIpaddr(wbuf);
  ascToUni(wbuf, host, min(strlen(host) + 1, sizeof(wbuf)));
  websSetHost(wbuf);

  /*
  * Configure the web server options before opening the web server
  */

  websSetDefaultPage("default.asp");
  websSetPassword( (char_t*)_T(""));

  /*
  * Open the web server on the given port. If that port is taken, try
  * the next sequential port for up to "retries" attempts.
  */
  websOpenServer(m_nPort, nRPort);

  /*
  * First create the URL handlers. Note: handlers are called in sorted order
  * with the longest path handler examined first. Here we define the security
  * handler, forms handler and the default web page handler.
  */
  websUrlHandlerDefine((char_t*)_T(""), NULL, 0, websSecurityHandler,WEBS_HANDLER_FIRST);
  websUrlHandlerDefine((char_t*)_T("/goform"), NULL, 0, websFormHandler, 0);
  websUrlHandlerDefine((char_t*)_T("/cgi-bin"), NULL, 0, websCgiHandler, 0);
  websUrlHandlerDefine((char_t*)_T(""), NULL, 0, websDefaultHandler,WEBS_HANDLER_LAST);

  /*
  * Now define two test procedures. Replace these with your application
  * relevant ASP script procedures and form functions.
  */
  registeredHandler(); //注册函数 如asp函数和表单处理函数(自定义函数)
  /*
  * Create the Form handlers for the User Management pages
  */
  #ifdef USER_MANAGEMENT_SUPPORT
  formDefineUserMgmt();
  #endif

  /*
  * Create a handler for the default home page
  */
  websUrlHandlerDefine((char_t*)_T("/"),NULL,0,websHomePageHandler,0);

  return 0;
}

//Home page handler
int QWebServerThread::websHomePageHandler(webs_t wp, char_t *urlPrefix, char_t *webDir,int arg, char_t *url, char_t *path, char_t *query)
{
  (void*)(urlPrefix);
  (void*)(webDir);
  (void*)(arg);
  (void*)(path);
  (void*)(query);

  //If the empty or "/" URL is invoked, redirect default URLs to the home page
  if(*url == '' || gstrcmp(url, (char_t*)_T("/")) == 0)
  {
    websRedirect(wp, (char_t*)WEBS_DEFAULT_HOME);
    return 1;
  }
  return 0;
}

#ifdef B_STATS
void QWebBase::memLeaks()
{
int fd;

if ((fd = gopen(_T("leak.txt"), O_CREAT | O_TRUNC | O_WRONLY, 0666)) >= 0) {
bstats(fd, printMemStats);
close(fd);
}
}

//Print memory usage / leaks
void QWebBase::printMemStats(int handle, char_t *fmt, ...)
{
va_list args;
char_t buf[256];

va_start(args, fmt);
vsprintf(buf, fmt, args);
va_end(args);
write(handle, buf, strlen(buf));
}
#endif

原文地址:https://www.cnblogs.com/zhangnianyong/p/8405570.html