简体   繁体   English

c 中是否有任何类似于反射 API 和注释在 java 中?

[英]Is there anything in c similar to Reflection API and annotations are in java.?

Recently, I've implemented an HTTP Server in c language.最近,我用 c 语言实现了一个 HTTP 服务器。 it serves static resources very well when requesting client request it to serve.当请求客户端请求它服务时,它很好地服务于 static 资源。 Now I want it to serve dynamic resources like jsp in java (tomcat), means some piece of code could execute at server side and response should be sent.现在我希望它为 java(tomcat)中的 jsp 等动态资源提供服务,这意味着可以在服务器端执行一些代码并发送响应。 I've done this kind of things in java using reflection, annotations and using other core libraries but I've never done this in c before.我已经在 java 中使用反射、注释和其他核心库做过这种事情,但我以前从未在 c 做过这种事情。

Can someone guide me to do this in c.Thanks in advance.有人可以指导我在 c 中执行此操作。在此先感谢。

While there are no equivalent facilities similar to Reflection API or Annotation in C, it is still possible to implement what you require, but it will need you to do some heavy lifting.虽然没有类似于反射 API 或 C 中的注释的等效工具,但仍然可以实现您需要的功能,但需要您做一些繁重的工作。

Mainly what I am getting is that you want to:我得到的主要是你想要:

  1. Figure out what the type of the resource is弄清楚资源的类型是什么

  2. Depending on the type, you want to execute it with some script and send its output.根据类型,您想使用一些脚本执行它并发送它的 output。

For part 1 you could base it on the extension of the resource.对于第 1 部分,您可以将其基于资源的扩展。 You could use system MIME types, or you could create your own mapping like this:您可以使用系统 MIME 类型,也可以像这样创建自己的映射:

struct Mapping {
   char *extension;
   char *command;
};

struct Mapping mapping[] = {
                            {"jsp", "<command to run jsp>"},
                            {"sh", "/bin/bash %1"}
};

For part 2, when your server receives a request, it needs to extract the extension of the resource, and check in the mapping to see which entry matches the extension, obtain the command field and execute the command (after substituting arguments if any) using system() call.对于第 2 部分,当您的服务器收到请求时,它需要提取资源的扩展名,并检查映射以查看哪个条目与扩展名匹配,获取command字段并执行命令(如果有,则替换 arguments 后)使用system()调用。

Note that as an assignment this is a great way to learn a lot of things, but if you put this in production, it will be a huge security hole!请注意,作为一项作业,这是学习很多东西的好方法,但如果你把它投入生产,这将是一个巨大的安全漏洞!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM